Using a query with a form reference in DAO

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • birdDBheadache
    New Member
    • Mar 2014
    • 16

    Using a query with a form reference in DAO

    I am trying to use a query with a form reference in DAO, but it does not return any records. Here is my code:

    Code:
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim rs1 As DAO.Recordset
    
        Set db = CurrentDb()
        Set qdf = db.QueryDefs("qry_Species_by_park_by_year")
        qdf.Parameters(0) = Forms!Occu_formatting!Park
        qdf.Parameters(1) = Year(Forms!Occu_formatting!survey_begin_date)
        qdf.Parameters(2) = Forms!Occu_formatting!obs_inits
        Set rs1 = qdf.OpenRecordset
        
        If rs1.RecordCount = 0 Then
            MsgBox ("No records in query")
            Exit Sub
        End If
    I have a suspicion it might be because I am trying to use the Year function.

    Also, when I run the query with the same parameters in Access I get exactly what I need.
  • birdDBheadache
    New Member
    • Mar 2014
    • 16

    #2
    I solved my own problem!

    The answer is that the Year function was causing the problem. To get around this issue, I created an invisible unbound text box in the form that got the year from the date field. Everything else stayed the same except removing the year function from the qdf.paramters(1 ) line and reassigning it to my new invisible text box.

    Comment

    Working...