I would do something like this for the RecordSource (using a somewhat
modified version of the code Douglas provided in his most recent posting):
Dim strSQL as String
Dim strtxtcompany As String
Dim dattxtdate As Date
dattxtdate = Me.txtmontha
strtxtcompany = Me.cmbselectcompany
strSQL = "SELECT * FROM [tblmaintabs] " & _
"WHERE ([txtmonthlabel] >= " & _
DateSerial(Year(dattxtdate), Month(dattxtdate), 1) & _
" AND [txtmonthlabel] <= " & _
DateSerial(Year(dattxtdate), Month(dattxtdate) + 1, 0) & _
") AND [txtcompany] = ' " & strtxtcompany & " ' "
Debug.Print strSQL
Me.RecordSource = strSQL
I would use a different prefix for different types of variables: str for
String, dat for Date, var for Variant, and so forth. I have shown that with
dattxtdate. Makes it easier to tell things apart, IMO, but it's your choice.
Anyhow, after running the code, open the VBA editor immediate window by
pressing Ctrl + G (there are other ways, but that is probably the simplest).
In the immediate window you will see the string being used for the record
source. I was incorrect about doubling the quotes, by the way. I should
have tested, but I was a bit low on time.
Another use of the immediate window is to test code. For instance, type the
following (including the question mark) and press Enter:
?Date() = Format(Date(),"\#mmmm/yyyy\#)
or
?Date() = Format(Date(),"dd/mm/yy")
It will return False in either case. If you substitute the values in
txtmonthlabel (assuming txtmonthlabel is a date) for Date(), likewise it will
return False. The point is that comparing a date value to a date formatted
with the Format function will result in False, even if you and I can see it
is True. Access is very literal in that way.
Hi Bruce, here is the actual code behind the command button:
Private Sub cmdopenrecord_Click()
[quoted text clipped - 39 lines]