Access: Using query in VBA for recordset

后端 未结 1 1579
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 14:14

I have been accustomed to do recordssets in the following format:

Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = \"Select field1, field2 from myTable whe         


        
相关标签:
1条回答
  • 2021-02-03 14:56

    You can either

    • Use a query that has parameters and specify values for parameters provided that the query uses parameters.

      Dim dbs As DAO.Database
      Dim qdf As DAO.QueryDef
      Dim prm As DAO.Parameter
      Dim rst As DAO.Recordset
      
      Set qdf = CurrentDb.QueryDefs("qry_SomeQueryWithParameters")
      
      qdf.Parameters("SomeParam").Value = "whatever"
      
      Set rst = qdf.OpenRecordset
      

    or

    • Specify a query name as the command and use the Filter property on the recordset
    
        Dim rs As DAO.Recordset
        Dim rsFiltered As DAO.Recordset
    
    
        Set rs = CurrentDb.OpenRecordset(qry_SomeQueryWithoutParameters)
    
        rs.Filter = "field1 > 30"
        set rsFiltered  = rs.OpenRecordset
    
    
    
    0 讨论(0)
提交回复
热议问题