Displaying Recordset values in a Subform

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nilanjangm
    New Member
    • Mar 2010
    • 11

    Displaying Recordset values in a Subform

    I have a subform in the main form. I have a recordset as well (Access 2007).

    I want to display the records in the recordset in the subform. So I wrote this.
    Code:
    Set subfrmResourceProposedInProject.Form.Recordset = rst
    (The recordset is fetching value correctly, I have checked that) However, this throws run-time error (
    run-time error '2467': The expression you entered refers to an object that is closed or doesn't exist
    )

    I had another way which worked. The code was like this.
    Code:
    strQueryName = "SELECT * FROM tbl_Emp_Master;"
    Set dbs = CurrentDb()
    Set qdf = dbs.QueryDefs("qry_All_Emp_Detail")
    qdf.SQL = strQueryName
        
    subfrmCustomerExpense.SourceObject = "Query.qry_All_Emp_Detail"
    In this case, I also had to define an Access query in the name of
    qry_All_Emp_Det ail
    , though I had to define that once again in the code here (don't know why!!).

    Therefore, I wanted to know if there is any way to do it in the first way (using recordset object).
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    I have no idea where, or even if, rst is Declared and Initialized. Try syntax similar to:
    Code:
    Dim MyDB As DAO.Database
    Dim rst As DAO.Recordset
    Dim strQueryName As String
    
    strQueryName = "SELECT * FROM tbl_Emp_Master;"
    
    Set MyDB = CurrentDb
    Set rst = MyDB.OpenRecordset(strQueryName, dbOpenDynaset)
    
    Set Me![subfrmCustomerExpense.].Form.Recordset = rst

    Comment

    • nilanjangm
      New Member
      • Mar 2010
      • 11

      #3
      I had declared "rst" as
      Code:
      Dim rst As Recordset
      along with
      Code:
      Dim dbs As Database
      Dim qdf As QueryDef
      Now, I have tried with your code. The line
      Code:
      Set Me![subfrmCustomerExpense.].Form.Recordset = rst
      produced the error
      Run-time error '2465': Microsoft Office Access can't find the field 'subfrmResource ProposedInProje ct.' reffered to in your expression.
      . I removed the "." from the end of the 'subfrmResource ProposedInProje ct.' and tried again. This produced the same error as before
      Quote:
      run-time error '2467': The expression you entered refers to an object that is closed or doesn't exist

      Comment

      • nilanjangm
        New Member
        • Mar 2010
        • 11

        #4
        I also checked whether the recordset holds the values using
        Code:
        Debug.Print rst.Fields(0) & ":"; rst.Fields(1) & ":" & rst.Fields(2) & ":" & rst.Fields(3)
        after
        Code:
        Set rst = qdf.OpenRecordset()
        This displayed the the values properly.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Sounds like a Syntax Error on the actual SubForm Name. Remember also that you are referencing the Name of the SubForm Control and not the Name of the SubForm itself.

          Comment

          Working...