So could you take me through your comment "You cannot assign its value
to
a
string variable if nothing's been selected: string variables cannot be
set
to
Null (the only variable type that can is Variant)" I'm afraid I just
don't
understand that.
There are many different types of variables in Access. Numeric variables
can
only store numbers (Byte, Integer and Long can only store integer values,
Single and Double can store real values). String variables can store
alphanumeric data. Variant is another type of variable. Variables
declared
as the Variant data type can contain string, date, time, Boolean, or
numeric
values, and can convert the values they contain automatically. Numeric
Variant values require 16 bytes of memory (which is significant only in
large procedures or complex modules) and they are slower to access than
explicitly typed variables of any other type.
The other problem is the one of dates. I'm not sure I understand the
problem
[quoted text clipped - 7 lines]
just put in a date that's when I get the Invalid use of Null' So I'm
beginning to feel lost.
How the data is stored and how it's presented to the user are two very
different things.
If txtmonthlabel is a date field in your table, then it contains a date,
which is a specific point in time (a day, month and year). Using Format,
you
can display that date as simply a year, or a month and year, or simply a
day
of the week if you want.
Under the covers, a date field is an eight byte floating point number
where
the integer portion represents the date as the number of days relative to
30
Dec, 1899, and the decimal portion represents the time as a fraction of a
day. Hopefully that gives you an idea of why December, 2009 isn't a date:
what number would you store to represent how many days it's been since 30
Dec, 1899?
I realise that this is a lot to ask but could you rewrite my code to
exactly
[quoted text clipped - 33 lines]
What is actually typed into Me.txtmontha: a full date, or only a month and
year?
One problem with your code is the line
strtxtcompany = Me.cmbselectcompany
There is no need to assign the value of the combo box to a variable: just
refer to the combo box in your code.
If me.txtmontha contains a full date, you could use something like:
Private Sub Command35_Click()
On Error GoTo Err_Command35_Click
Dim dtmFirstDay As Date
Dim dtmLastDay As Date
Dim strsql As String
If IsNull(Me.txtmontha) Then
Msgbox "You must supply a date."
Else
dtmFirstDay = DateSerial(Year(Me.txtmontha), Month(Me.txtmontha), 1)
dtmLastDay = DateSerial(Year(Me.txtmontha), Month(Me.txtmontha) + 1,
0)
strsql = "SELECT * FROM [tblmaintabs] " & _
"WHERE ([txtmonthlabel] Between " & _
Format(dtmFirstDay, "\#yyyy\-mm\-dd\#") & _
" AND " & Format(dtmLastDay, "\#yyyy\-mm\-dd\#") & ") "
If Len(Me.cmbselectcompany & vbNullString) > 0 Then
strsql = strsql & _
"AND [txtcompany] = '" & Me.cmbselectcompany & "'"
End If
Debug.Print strsql
Forms!frmMain!SubForm1.SourceObject = "subformFDA"
Forms!frmMain!SubForm1.Form.RecordSource = strsql
End If
Exit_Command35_Click:
Exit Sub
Err_Command35_Click:
MsgBox Err.Description
Resume Exit_Command35_Click
End Sub