Results 1 to 7 of 7
  1. #1
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368

    error 3601 too few parameters, expected 1


    Can anyone look at my SQL and look if i made a mistake ?

    Code:
    Dim strUsername As String
    strUsername = fOSUserName()
    
    
       Dim strSQL As String
        strSQL = "SELECT DISTINCT IDAgenda FROM tblAgenda " _
         & "WHERE AfspraakGeexporteerd = 'No' AND Date() <= DatumAgenda AND time() < TijdAgenda AND ChoosePostBox = strUsername"
          With CurrentDb.OpenRecordset(strSQL)
           If .RecordCount > 0 Then
    
    'do stuff
    
           else
    'Outta here
           exit sub
    end with
    end if

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    not sure if you can
    With CurrentDb.OpenRecordset(strSQL)
    (it may work,tho never tried)

    try
    set rst = CurrentDb.OpenRecordset(strSQL)
    if rst.recordcount > 0 then

    BUT, ChoosePostBox = strUsername": is inside the quote...it must be outside, and have quotes around it ...STRINGS.

    ... and ChoosePostBox ='" & strUsername & "'"

  3. #3
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Yes, that was one of the issues.. (i should have known that pff).
    Still doesnt work though. the .recordcount method is correct though, i use it all the time

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The error message you are getting suggests that one or more of the fields referenced in your where clause do not appear in the table tblAgenda.

    You might also try changing Date() <= DatumAgenda to DatumAgenda >= date() and time() < TijdAgenda to TijdAgenda > time(). I have a hunch (unconfirmed!) that MS Access sometimes assumes the value on the left of the comparison is a table/query field.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    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.

  6. #6
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Hey guys and thanks for the replies.

    After a few tweaks i could get the code working. However i have discovered that my code lacks the correct Date / time check.
    When you think about it, it is to be expected really.
    The SQL doesnt count records that is before the time, On whatever day.

    That is obviously not my goal :P

    I tried this code (notice the blue '+' sign between date and time)
    But SQL doesnt get that obviously.

    Code:
        Dim strUsername As String
        Dim strSQL As String
    
    
        strUsername = fOSUserName()
    
    
        strSQL = "SELECT DISTINCT IDAgenda "
        strSQL = strSQL & " FROM tblAgenda "
        strSQL = strSQL & " WHERE AfspraakGeexporteerd = False "
        strSQL = strSQL & " AND DatumAgenda >= #" & Date & "#"
        strSQL = strSQL & " + TijdAgenda > #" & Time & "#"
        strSQL = strSQL & " AND ChoosePostBox = '" & strUsername & "'"
    
    
        With CurrentDb.OpenRecordset(strSQL)
            If .RecordCount > 0 Then
            
            MsgBox .RecordCount
            Else
            MsgBox "No records"
            
            End If
        End With
    Any thoughts on this ?

  7. #7
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Right i figured it out on my own :

    Code:
        Dim strUsername As String
        Dim strSQL As String
        Dim strDateTime As Date
        
        strUsername = fOSUserName()
        
        strSQL = "SELECT DISTINCT IDAgenda "
        strSQL = strSQL & " FROM tblAgenda "
        strSQL = strSQL & " WHERE AfspraakGeexporteerd = False "
        strSQL = strSQL & " AND DatumAgenda + TijdAgenda > #" & strDateTime & "#"
        strSQL = strSQL & " AND ChoosePostBox = '" & strUsername & "'"

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error 3061. Too few parameters. Expected 1.
    By Glenn_Suggs in forum Programming
    Replies: 5
    Last Post: 02-03-2015, 12:03 PM
  2. Runtime Error 3061. Too few parameters, expected 2
    By Gina Maylone in forum Programming
    Replies: 35
    Last Post: 01-13-2014, 02:37 PM
  3. Replies: 3
    Last Post: 04-26-2013, 01:37 PM
  4. Error on too few parameters expected 3
    By haishuoBB in forum Programming
    Replies: 7
    Last Post: 02-27-2013, 03:26 PM
  5. 3061 Error. Too few parameters. Expected 1.
    By rghollenbeck in forum Queries
    Replies: 5
    Last Post: 09-28-2011, 12:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums