CODE 2 ADD A NEW RECORD

  • Thread starter Faraz Ahmed Qureshi
  • Start date
F

Faraz Ahmed Qureshi

Could someone pls provide a sample code for BUTTON1 on FORM1 which onclick
adds the entries in TEXTBOX1, TEXTBOX2, TEXTBOX3, TEXTBOX4 to TABLE1's
FIELD1, FIELD2, FIELD3 & FIELD4?
 
J

John W. Vinson

Could someone pls provide a sample code for BUTTON1 on FORM1 which onclick
adds the entries in TEXTBOX1, TEXTBOX2, TEXTBOX3, TEXTBOX4 to TABLE1's
FIELD1, FIELD2, FIELD3 & FIELD4?

Sure... but that's a Really Bad Way to do things, unless you have some very
specific reasons to do so. Why not just use a bound form?

Select the button's Click event; click the ... icon by it and choose Code
Builder. I'll assume that the four fields are of Text datatype (see below).
Edit the code to

Private Sub BUTTON1_Click()
Dim strSQL As String
On Error GoTo Proc_Err
strSQL = "INSERT INTO mytablename (FIELD1, FIELD2, FIELD3, FIELD4) " _
& "VALUES(""" & Me!TEXTBOX1 & """, """ & Me!TEXTBOX2 & _
""", """ & Me!TEXTBOX3 & """, """ & Me!TEXTBOX4 & "");"
Currentdb.Execute strSQL, dbFailOnError
Proc_Exit:
Exit Sub
Proc_Err:
MsgBox "Error " & Err.Number & " in BUTTON1_Click" & vbCrLf & Err.Description
Resume Proc_Exit
End Sub
 
F

Faraz Ahmed Qureshi

Thanx a lot John,

However, upon pasting the record the msgbox pops up "Expected: end of
statement" with the closing bracket selected.
 
F

Faraz Ahmed Qureshi

I think it should be:
strSQL = "INSERT INTO mytablename (FIELD1, FIELD2, FIELD3, FIELD4) " _
& "VALUES(""" & Me!TEXTBOX1 & """, """ & Me!TEXTBOX2 & _
""", """ & Me!TEXTBOX3 & """, """ & Me!TEXTBOX4 & """);"

However, any idea how to have the same form be empty as well in the end?
 
A

Al Campagna

Faraz,
You could do a...
DoCmd.GoToRecord , , acNewRec
after your Update code runs.

Note: John Vinson is correct. Unless you have some specific reason
why you're updating your table this way, it's really not the right way to
go.
You should have a form, based on your table, (or most usually...
a query of that table) and be updating the field values directly.
--
hth
Al Campagna
Microsoft Access MVP 2007-2009
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love... and you'll never work a day in your life."
 
J

John W. Vinson

I think it should be:
strSQL = "INSERT INTO mytablename (FIELD1, FIELD2, FIELD3, FIELD4) " _
& "VALUES(""" & Me!TEXTBOX1 & """, """ & Me!TEXTBOX2 & _
""", """ & Me!TEXTBOX3 & """, """ & Me!TEXTBOX4 & """);"

quite correct... sorry, miscounted quotes.
However, any idea how to have the same form be empty as well in the end?

If it's an unbound form just set each textbox to Null:

Me!TEXTBOX1 = Null

and so on.

You still haven't explained why you are doing this job the hard way. NONE of
this code is needed.
 
D

De Jager

Faraz Ahmed Qureshi said:
Could someone pls provide a sample code for BUTTON1 on FORM1 which onclick
adds the entries in TEXTBOX1, TEXTBOX2, TEXTBOX3, TEXTBOX4 to TABLE1's
FIELD1, FIELD2, FIELD3 & FIELD4?
 

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