How create a recordset to insert records?

R

Ryan

I have a need to programatically insert records to a
table. How do I do that?
This is being done in a subform event procedure, so should
I add them directly to the table or add them to the
subform? I'm confused.
 
S

Sandra Daigle

Hi Ryan,

You can insert records using DAO or by running an append query - it just all
depends on your situation. If the recordsource of your subform already has
the correct fields and you are adding records in response to some subform
event then it makes sense to use the subform's recordset (or recordsetclone)
since it is already open. Since you didn't give much detail on your
particular situation, here's a tiny bit of sample code that might help you
get started - this might be attached to a command button on the subform.
Note that this code is intended to be in the subform's class module so
references to 'me' are to the subform.

with me.recordsetclone
.addnew
.fields("Field1")=333
.fields("Field2")='SampleCode"
.update
end with

Change 'Field1' and 'Field2' to actual fieldnames and change the values to
whatever you need.
 
R

Ryan

This worked like a charm. Thanks! I wish I knew how you
learn this stuff as there is no mention of it in the
Access help files or any of the Access books I have.
 
S

Sandra Daigle

Hi Ryan,

Glad it worked for you . . .

I learned a considerable amount of what I know from these newsgroups and
from the Access Developer's Handbooks (Litwin, Getz and Gilberet). There are
also scads of websites which are full of great examples. A great starting
point is the Access Web (www.mvps.org/access).
 

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