Why can't I use a variable to pass a date to this sub to set parameter values?

Anonymous
2025-05-23T12:20:01+00:00

Hello. I'm using the sub from this thread https://answers.microsoft.com/en-us/msoffice/forum/all/can-you-use-a-public-function-sub-to-set-a/1043226e-ae37-450a-a045-3261256a9a29  to set a forms recordset. It works fine on the first form I set up. I couldn't get it to work with a second. After a LOT of pulling my hair out trying to figure out why it wouldn't work, I finally found the culprit. It seems like it doesn't like me sending dates to it via variable. I've tried all the combos below. For lines 820, the bottom 3 work perfectly. The top 3 don't return anything. I've tried the top 3 with the various lines 790 and 800. NONE work. Any advice why? (For testing purposes I just comment what I tried last, and uncomment what I'm trying this go around.)

          Dim sLocation As String, sCO As String

          Dim iEquipType As Integer, iExamFreq As Integer

          Dim dExamDateStart As Date, dExamDateEnd As Date

          Dim sExamDateStart As String, sExamDateEnd As String

'790       dExamDateStart = #5/1/2025#

'790       dExamDateStart = Format(#5/1/2025#, "mm/dd/yyyy")

790       sExamDateStart = "5/1/2025"

'800       dExamDateEnd = #5/23/2025#

'800       dExamDateEnd = Format(#5/23/2025#, "mm/dd/yyyy")

800       sExamDateEnd = "5/1/2025"

810       sCO = Nz(Me.Parent!txtCOFilter.Value, "")

'820       Call SetRecordset(Me, "qryRecentExams_Primary", "parEquipmentLocation", sLocation, "parEquipmentTypeID", iEquipType, "parExamFrequencyID", iExamFreq, "parExamDateStart", dExamDateStart, "parExamDateEnd", dExamDateStart, "parCO#", sCO)

'820       Call SetRecordset(Me, "qryRecentExams_Primary", "parEquipmentLocation", sLocation, "parEquipmentTypeID", iEquipType, "parExamFrequencyID", iExamFreq, "parExamDateStart", Format(dExamDateStart, "mm/dd/yyyy"), "parExamDateEnd", Format(dExamDateStart, "mm/dd/yyyy"), "parCO#", sCO)

820       Call SetRecordset(Me, "qryRecentExams_Primary", "parEquipmentLocation", sLocation, "parEquipmentTypeID", iEquipType, "parExamFrequencyID", iExamFreq, "parExamDateStart", Format(sExamDateStart, "mm/dd/yyyy"), "parExamDateEnd", Format(sExamDateStart, "mm/dd/yyyy"), "parCO#", sCO)

'820       Call SetRecordset(Me, "qryRecentExams_Primary", "parEquipmentLocation", "", "parEquipmentTypeID", 0, "parExamFrequencyID", 0, "parExamDateStart", #5/1/2025#, "parExamDateEnd", #5/23/2025#, "parCO#", "")

'820       Call SetRecordset(Me, "qryRecentExams_Primary", "parEquipmentLocation", "", "parEquipmentTypeID", 0, "parExamFrequencyID", 0, "parExamDateStart", Format(#5/1/2025#, "mm/dd/yyyy"), "parExamDateEnd", Format(#5/23/2025#, "mm/dd/yyyy"), "parCO#", "")

'820       Call SetRecordset(Me, "qryRecentExams_Primary", "parEquipmentLocation", sLocation, "parEquipmentTypeID", iEquipType, "parExamFrequencyID", iExamFreq, "parExamDateStart", #5/1/2025#, "parExamDateEnd", #5/23/2025#, "parCO#", sCO)
Microsoft 365 and Office | Access | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Anonymous
    2025-05-23T13:03:24+00:00

    In case it's relevant, this is the full code of the sub I used with the post above as basis.

    Function SetRecordset(frm As Form, q As String, ParamArray aParam() As Variant)
    
    1590      If gcfHandleErrors Then On Error GoTo Err_Handler
    
              Dim db As DAO.Database
    
              Dim qdf As DAO.QueryDef
    
              Dim rs As DAO.Recordset
    
              Dim i As Integer, c As Integer
    
    1600      c = UBound(aParam())
    
    1610      Debug.Print frm.Name
    
    1620      Set db = CurrentDb
    
    1630      Set qdf = db.QueryDefs(q)
    
    1640      Select Case q
    
              Case "qryNoExamsOOSAssigned"
    
    1650      Case Else
    
    1660          For i = 0 To c
    
    1670              qdf.Parameters(aParam(i)) = aParam(i + 1)
    
    1680              i = i + 1
    
    1690          Next i
    
    1700      End Select
    
    1710      Set rs = qdf.OpenRecordset(dbOpenDynaset)
    
    1720      Set frm.Recordset = rs
    
    Exit_Handler:
    
    1730      Exit Function
    
    Err_Handler:
    
    1740      clsErrorHandler.HandleError "Exam", "SetRecordset", True
    
    1750      Resume Exit_Handler
    
    End Function
    
    0 comments No comments
  2. DBG 11,381 Reputation points Volunteer Moderator
    2025-05-23T16:18:37+00:00

    Just in case it matters, since we're talking about dates, what is your Regional Settings?

    0 comments No comments
  3. Anonymous
    2025-05-23T17:20:21+00:00

    As a general observation, when establishing a recordset object one should wherever possible concatenate the values into the string expression for the SQL statement, rather than assign the values to parameters.  All that's then necessary is to ensure that the values concatenated into the string expression are correctly delimited, where necessary for the data type in question.  In the case of strings I would recommend representing the literal quotes delimiters by means of contiguous pairs of quotes characters, i.e. """" rather than "'".  It is more likely that a string expression will contain an apostrophe than a double quotes character, particularly in the case of personal names.  Date literals must be delimited by the # character, and be in US or an otherwise internationally unambiguous  format such as the ISO standard for date notation of YYYY-MM-DD hh mm ss (note that the standard uses case to differentiate months and minutes, whereas Access uses n for the latter). 

    In the case of a saved querydef object which might include parameters, these should be references to an accessible object, usually a control in an open form.  The code can then loop through the query's Parameters collection and evaluate each parameter's Name property with the Eval function.  There is no need to know the names of the parameters, or whether the query has parameters or not.

    0 comments No comments
  4. Anonymous
    2025-05-23T17:27:42+00:00

    Standard US settings.

    0 comments No comments
  5. Anonymous
    2025-05-23T17:36:25+00:00

    So, you're saying I should be using the SQL and not the saved queries for these forms? That seems annoying if I'm using the same query in multiple forms and have to make an edit. But I guess there's the argument that I probably won't be changing these very much, if ever, once I get it all working...

    I'm afraid I'm just not advanced enough to really understand the second paragraph there. I have no grasp of how I'd assign values to something that may or may not exist, and I don't give the name of it if it does exist. Maybe when I do another revision of this project in the future I will be.

    0 comments No comments