Link to home
Start Free TrialLog in
Avatar of SweetingA
SweetingA

asked on

Recordset not working

Hello Experts,

The following recordset code is not working corrrectly.

The first record is copied multiple times instead of stepping through the records.

Can you help?

Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM qry_PDM_Questions WHERE " & Me.Filter)
    rst.MoveFirst
   
    Dim rstADD As DAO.Recordset
    Set rstADD = CurrentDb.OpenRecordset("tbl_PDM_Results")

    Do Until rst.EOF
        strSaveCriteria = Me.txtPeriod & Me.txtSite & Me.txtFunction & Me.txtCell & Me.Question
        rstADD.AddNew
        rstADD("Period") = Me.txtPeriod
        rstADD("Site") = Me.txtSite
        rstADD("Function") = Me.txtFunction
        rstADD("Cell") = Me.txtCell
        rstADD("Question") = Me.Question
        rstADD("Answer") = Me.Answer
        rstADD("SaveCriteria") = strSaveCriteria
        rstADD.Update
        rst.MoveNext
    Loop

    rst.Close
    rstADD.Close

Thanks
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

you are not using any of the field from the recordset  rst..

what do you want to happen?
As I told you in the other question:

You cannot refer to the FORM fields - you must refer to the "rst" recordset object fields:

 rstADD("Period") = rst("Period")
        rstADD("Site") = rst("Site")

and so on. Obviously you'd have to change the values of the Fields to match the ones in your recordset.
Avatar of SweetingA
SweetingA

ASKER

When i do this i always get "item not found in this collection"....see below....i was a bit too hasty earlier, did not realise it was copying 23 identical records.

what do you mean by matching the values in the fields to the values in the recordset?

Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM qry_PDM_Questions WHERE " & Me.Filter)
    rst.MoveFirst
   
    Dim rstADD As DAO.Recordset
    Set rstADD = CurrentDb.OpenRecordset("tbl_PDM_Results")

    Do Until rst.EOF
        strSaveCriteria = Me.txtPeriod & Me.txtSite & Me.txtFunction & Me.txtCell & Me.Question
        rstADD.AddNew
        rstADD("Period") = rst("txtPeriod")
        rstADD("Site") = rst("txtSite")
        rstADD("Function") = rst("txtFunction")
        rstADD("Cell") = rst("txtCell")
        rstADD("Question") = rst("Question")
        rstADD("Answer") = rst("Answer")
        rstADD("SaveCriteria") = strSaveCriteria
        rstADD.Update
        rst.MoveNext
    Loop

    rst.Close
    rstADD.Close
what are the fields from query "qry_PDM_Questions "

you will be using those fields in your codes to add records to recordset rstAdd,
NOT the name of the textboxes..
can you upload a copy of your db..

or post the SQL statement of query "qry_PDM_Questions"
Hi Capricorn1,

Attached is a copy of relevant records - very rough but you'll get the idea

thanks
pdm---Copy.mdb
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Does not add any records to table?
My apologies works great, forgot i changed the name of the button.

Thankyou....and LSMConsulting for their earlier help
it added 23 records to table tbl_PDM_Results
tblPDMResults.txt