QueryDef and recordset using parameter on forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brent78
    New Member
    • Mar 2007
    • 13

    QueryDef and recordset using parameter on forms

    I am trying to pass a parameter value from a query to use in my recordset. The parameter value comes from a form but I am getting a datatype conversion error when when assigning the recordset to use the querydef param. Below is the code I have and a screenshot of the query is attached.

    The main idea is to scroll through the results of the parameter query and store values to a temporary table

    Code:
    Option Explicit
    
    Public Sub PumpOverTimes()
    
    Dim MyDB As Dao.Database
    Dim qdfTimesQuery As Dao.QueryDef
    Dim rstPOTimes As Dao.Recordset 'Temporary Table
    Dim rstTimes As Dao.Recordset
    
    Dim lFrequency As Integer
    Dim dtStart As Date
    Dim dtEnd As Date
    Dim sTank As String
    Dim dtTempDate As Date
    Dim dtFormDate As Date
    
    Set MyDB = CurrentDb
    
    Set qdfTimesQuery = MyDB.QueryDefs("qry_HardData")
    
    qdfTimesQuery.Parameters("[forms]![frm_MainMenu].[dtDate]") = [Forms]![frm_MainMenu].[dtDate]
    
    Set rstTimes = qdfTimesQuery.OpenRecordset("qry_HardData", dbOpenSnapshot)' Datatype Conversion error occurs here
    Set rstPOTimes = MyDB.OpenRecordset("tblPOTimes", dbOpenDynaset) 
    CurrentDb.Execute "delete * from tblPOTimes;", dbFailOnError
    
    
    Do While Not rstTimes.EOF
        dtStart = rstTimes!dtStart
        dtEnd = rstTimes!dtExpire
        sTank = rstTimes!tankID
        lFrequency = 0
        dtTempDate = dtStart
            
        Do While dtEnd >= dtTempDate
           
        With rstPOTimes
            .AddNew
            !dtDate = dtTempDate
            !lDuration = lFrequency
            !sTankID = sTank
            .Update
        End With
        lFrequency = rstTimes!aValue
        dtTempDate = DateAdd("h", lFrequency, dtTempDate)
        Loop
        
        rstTimes.MoveNext
           
        Loop
        
        rstTimes.Close
        rstPOTimes.Close
        Set rstTimes = Nothing
        Set rstPOTimes = Nothing
    
    End Sub
    [IMGNOTHUMB]http://bytes.com/attachments/attachment/5535d1318961127/qry_param.jpg[/IMGNOTHUMB]
    Attached Files
    Last edited by NeoPa; Oct 19 '11, 03:21 PM. Reason: Made picture viewable
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    @Brent78:
    Here is the general idea for a Parameterized Query named qryTest:
    Code:
    Dim MyDB As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
    Dim rstTest As DAO.Recordset
    
    Set MyDB = CurrentDb()
    Set qdf = MyDB.QueryDefs("qdfTest")
    
    'Resolve the Parameters beforehand
    For Each prm In qdf.Parameters
      prm.Value = Eval(prm.Name)
    Next
      
    Set rstTest = qdfTest.OpenRecordset(dbOpenSnapshot)

    Comment

    • brent78
      New Member
      • Mar 2007
      • 13

      #3
      Thanks. I actually just discovered that I had one parameter too many on line 24. I have the following code which works fine now but I have one other problem.
      from line 24
      Code:
      Set rstTimes = qdfTimesQuery.OpenRecordset(dbOpenDynaset)
      My parameter query occurs in a nested query and I'm not sure how to pass this data to querydef to use in my main recordset. Is it possible to use a parameter from a nested query?

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Retrieve the Value of the Parameter from the Nested Query, then created a Recordset based on a SQL String from the Main Query. This is Theory only.

        Comment

        Working...