There are a couple of issues I see.
The first is that you have crossed control structures. I took out everything except the control syntax:
Code:
With CurrentDb.OpenRecordset(strSQL)
If .RecordCount > 0 Then
Else
End With
End If
See how the "End With" and the "End If" are swapped?
And the error you are getting (usually) means the SQL is expecting a parameter. You need to concatenate variables (values) in the SQL string.
I changed you code a little. Try executing this and look in the immediate window.
Code:
Sub test11()
Dim strUsername As String
Dim strSQL As String
' strUsername = "Mighty Mouse" ' for my testing - I don't have fOSUserName()
strUsername = fOSUserName()
strSQL = "SELECT DISTINCT IDAgenda "
strSQL = strSQL & " FROM tblAgenda "
strSQL = strSQL & " WHERE AfspraakGeexporteerd = 'No' "
strSQL = strSQL & " AND Date() <= DatumAgenda "
strSQL = strSQL & " AND time() < TijdAgenda "
strSQL = strSQL & " AND ChoosePostBox = strUsername"
'Comment out or delete after debugging ----------------
Debug.Print strSQL
Stop
'end debugging ------------------------
With CurrentDb.OpenRecordset(strSQL)
If .RecordCount > 0 Then
'do stuff
Else
'Outta here
Exit Sub
End If
End With
End Sub
Is the SQL what you would expect?
Here is the revised sub, modified as John_G suggested (and I agree):
Code:
Sub test11()
Dim strUsername As String
Dim strSQL As String
' strUsername = "Mighty Mouse" ' for my testing - I don't have fOSUserName()
strUsername = fOSUserName()
strSQL = "SELECT DISTINCT IDAgenda "
strSQL = strSQL & " FROM tblAgenda "
strSQL = strSQL & " WHERE AfspraakGeexporteerd = 'No'"
strSQL = strSQL & " AND DatumAgenda >= #" & Date & "#"
strSQL = strSQL & " ANDTijdAgenda > #" & Time() & "#"
strSQL = strSQL & " AND ChoosePostBox = '" & strUsername & "'"
'Comment out or delete after debugging ----------------
Debug.Print strSQL
Stop
'end debugging ------------------------
With CurrentDb.OpenRecordset(strSQL)
If .RecordCount > 0 Then
'do stuff
Else
'Outta here
Exit Sub
End If
End With
End Sub
Compare the other SQL string to this SQL string.