Data entry to table from a form

  • Thread starter Thread starter haas.chaudhry
  • Start date Start date
H

haas.chaudhry

OK, to avoid confusion, all i want is the code for a procedure where if I press a button on the form it will call up a VBA procedure which would essentially update/add a frecord to the table. I KNOW THIS CAN BE DONE WITHOUT CODE, but I'm looking for VBA code for other purposes:

I have a table called tblContacts
There are two fields in this table: CompanyName and PhoneNumber

I created a form where the user can see if a company name exists through a Combo Box with the source being the list of entries in the tblContacts field - if it doesn't, he enters a new company into the a txtCompanyName box and a phone number into the txtPhoneNumber box on the form. Next, user presses a button on the form which would call a VBA procedure which runs and takes the values from txtCompanyName and txtPhoneNumber and adds to the tblContacts table

I know that you can do this easily through Form Wizard OR a RECORDSOURCE (thanks Larry), but I wanted to know how to program this into a VBA procedure.. This is a simple example to learn the technique so I cna apply it to one with multiple fields, etc.

Thank you for your help in advance!
 
Docmd.RunSQL "INSERT INTO tblYourTable (Fld1,Fld2,Fld3,Fld4) VALUES (1,2,3,4);"

Insert your fields values in the same order you list your fields. For text you must surround the value with single quotes ' '. For dates you must surround the value with has tags # #. For integers don't surround with anything. If you don't want to include a field just don't list it in your fields.
 
Back
Top