Recordset Based on Select Query

G

Guest

Hi
Im writing RS on Query to update records to new table but it gives me error,
can some one help me to write this recrod set pls.
dim db as DAO.Database
dim rst as DAO.Recordset
dim rTarget as DAO.Recordset

Set rst = CurrentDb.OpenRecordset("QryCustCurrTransactions",dbOpenDynaset)
Set rTarget = CurrentDb.OpenRecordset("LocalTB", dbOpenDynaset)

Do Until rst.EOF
rTarget.AddNew
rTarget!TrnDate = DateSerial(Year(rst!TrnDate),
Month(rst!TrnDate) + 1, 1 - 1)
rTarget!CustomerID = rst!CustomerID
rTarget!Debit = rst!DrKD
rTarget!Credit = rst!CrKD
rTarget.Update
rst.MoveNext
Loop
rst.Close
rTarget.Close


Thanks in advance
 
J

John Spencer

Try using the Db database object. I suspect that using
currentdb().OpenRecordset means that rst and rTarget are both going out of
scope as soon as the line has finished executing.

By the way, it is helpful to tell people the exact error you are getting and
on which line it occurs. Makes trouble-shooting a lot easier.

dim db as DAO.Database
dim rst as DAO.Recordset
dim rTarget as DAO.Recordset

Set db = CurrentDB() '<<< -----

'Note the change in the next two lines currentdb to db
Set rst = db.OpenRecordset("QryCustCurrTransactions",dbOpenDynaset)
Set rTarget = db.OpenRecordset("LocalTB", dbOpenDynaset)

Do Until rst.EOF
rTarget.AddNew
rTarget!TrnDate = DateSerial(Year(rst!TrnDate),
Month(rst!TrnDate) + 1, 1 - 1)
rTarget!CustomerID = rst!CustomerID
rTarget!Debit = rst!DrKD
rTarget!Credit = rst!CrKD
rTarget.Update
rst.MoveNext
Loop
rst.Close
rTarget.Close

I would think you could do the same thing with an append query and it would
be more efficient

Dim StrSQL

StrSQL = "Insert into LocalTb(TrnDate, CustomerID, Debit, Credit)" & _
" SELECT DateSerial(Year(TrnDate),Month(TrnDate),0)" & _
", CustomerID, DrKD, CrKD" & _
" FROM qryCustCurrTransactions"

CurrentDb().Execute StrSQL, DbFailonError


--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
D

Dirk Goldgar

In
Wahab said:
Hi
Im writing RS on Query to update records to new table but it gives me
error, can some one help me to write this recrod set pls.
dim db as DAO.Database
dim rst as DAO.Recordset
dim rTarget as DAO.Recordset

Set rst =
CurrentDb.OpenRecordset("QryCustCurrTransactions",dbOpenDynaset) Set
rTarget = CurrentDb.OpenRecordset("LocalTB", dbOpenDynaset)

Do Until rst.EOF
rTarget.AddNew
rTarget!TrnDate = DateSerial(Year(rst!TrnDate),
Month(rst!TrnDate) + 1, 1 - 1)
rTarget!CustomerID = rst!CustomerID
rTarget!Debit = rst!DrKD
rTarget!Credit = rst!CrKD
rTarget.Update
rst.MoveNext
Loop
rst.Close
rTarget.Close


Thanks in advance

What error number and message are you getting?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top