Insert a New Record

A

AccessVBANewbie

HI all out there, can anyone give an example of VBA codings in Access to for
me to insert a new record into a table with the source data from multiple
textboxes? thanks
 
V

Van T. Dinh

You can either use the Execute Method to execute an Append Query / SQL
String

or you can use the AddNew Method of the Recordset object.

Check Access VB Help on Execute or AddNew.
 
A

AccessVBANewbie via AccessMonster.com

ermm Van, I'm not sure I know what u mean, could u suggest an example? That
would help very much, thanks.
 
A

AccessVBANewbie via AccessMonster.com

ok i figured out how to insert a new record to a table through a text box by
using Me.txtCustomer.SetFocus, den typing in the relevant SQL statements. But
how can i insert multiple values from multiple textboxes into a new row of
record in a table? i've tried using the same method to set focus to all the
controls but the error message" You can't reference a control or property
without giving it focus" keeps occuring? anyone has any idea on how i can
solve this? some sample coding would help me greatly. thanx!
 
V

Van T. Dinh

Let say, you want to insert values from 2 TextBoxes into a new Record with 2
Fields and assume Field1 is numeric & Field2 is Text, you can use code
something like:

****Untested****
Dim strSQL as String
strSQL = "INSERT INTO [MyTable] (Field1, Field2) " & _
" VALUES (" & Me.TextBox1 & ", '" & Me.TextBox2 & "')"
Debug.Print strSQL ' This prints the constructed SQL in the Debug window.
' Comment the above out when your code works
CurrentDb.Execute strSQL, dbFailOnError
********

Check Access Help on "INSERT INTO Statement" and Access VB Help on the
Execute method.

--
HTH
Van T. Dinh
MVP (Access)



AccessVBANewbie via AccessMonster.com said:
ok i figured out how to insert a new record to a table through a text box
by
using Me.txtCustomer.SetFocus, den typing in the relevant SQL statements.
But
how can i insert multiple values from multiple textboxes into a new row of
record in a table? i've tried using the same method to set focus to all
the
controls but the error message" You can't reference a control or property
without giving it focus" keeps occuring? anyone has any idea on how i can
solve this? some sample coding would help me greatly. thanx!
http://www.accessmonster.com
 
A

AccessVBANewbie via AccessMonster.com

thanx Vin, I found out that all the whole I was using Me.Textbox2.Text,
instead of Me.Textbox2, which gave me all that problems... thanx dude! =)

Let say, you want to insert values from 2 TextBoxes into a new Record with 2
Fields and assume Field1 is numeric & Field2 is Text, you can use code
something like:

****Untested****
Dim strSQL as String
strSQL = "INSERT INTO [MyTable] (Field1, Field2) " & _
" VALUES (" & Me.TextBox1 & ", '" & Me.TextBox2 & "')"
Debug.Print strSQL ' This prints the constructed SQL in the Debug window.
' Comment the above out when your code works
CurrentDb.Execute strSQL, dbFailOnError
********

Check Access Help on "INSERT INTO Statement" and Access VB Help on the
Execute method.
ok i figured out how to insert a new record to a table through a text box
by
[quoted text clipped - 6 lines]
without giving it focus" keeps occuring? anyone has any idea on how i can
solve this? some sample coding would help me greatly. thanx!

http://www.accessmonster.com
 
Top