Silly question

AndyShuter

Registered User.
Local time
Today, 20:18
Joined
Mar 3, 2003
Messages
151
Erm, can someone tell me how to add a new record to a table pls?

I cant find how to do it in the heklp section

Many Thanks

Andy
 
Add a new record from where? A form?

If so, create a new record or add one complete with details?
 
sorry, from a form (ie a command button), then using vba

Thanks
 
Code:
DoCmd.GotoRecord, , acNewRec
 
Sorry, didnt explain well at all did I?!!!

Sorry mate, having a crap day - here's what I want to do

From a form, on the click event, I would like to create a record in another table that does not form part of the underlying query or table

the table name is "tblHirerComments"

What I want to do is create a new record and set the "HirerCode" value to Me.HirerCode.

Currently I am doing this by openning a seperate form (ie open & closing it)

Thanks

Andy
 
you can do a DoCmd.OpenForm from VBA to trigger the form to open. you would have to still close the form yourself. when the form closes, you would also have to trap the gotfocus event showing that you returned to the first form. you would also have to do a requery if the new record contributes to the data displayed on the first form.
 
I would just like to add the record without opennind any forms please (is it known as ADO???)
 
Look into Recordset operations in VBA. I'll summarize the sequence. Before you start, look up any terms that you don't feel that you know.

1. You set up a variable of type Database object. Set it to the current database object. (Shortcut is CurrentDB, you could use that if you wanted.)

2. You open a recordset object (also a variable) in that DB to your targeted table in the DB. Look at the options on the .OpenRecordSet method in order to see the mode you want for the "open" to use. Probably table-type recordset, but that IS a guess on my part.

3. You do an .addnew on the recordset.

4. You populate the fields of the record. This is pretty simple. The syntax could be as simple as a linear series of

recset.Fields("Fieldname") = value

(where value is numeric or quoted string as appropriate to the field.)

5. You do a .update on the recordset.

6. You do a .close on the recordset. (Always, ALWAYS, ALWAYS close what you opened.)

If the new record impinges on the form you have open now, you still need to do a requery and maybe a refresh.

In VBA, this is not very difficult. You can use OnError GoTo address-label if you want to trap errors to protect the operation. (In fact, it is strongly suggested.)
 

Users who are viewing this thread

Back
Top Bottom