control data to tables from forms in access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to add a record to a table from a form. The form is for data
entry only and has no record selecters. I would like the form to add the
record to the table only after all the fields are filled in and a command
button "Are sure the data is correct and do you want to add this record?" is
clicked. But have not been able to find a way to make happen.
 
Hi rlivings,

Start with an unbound form, and use a Save button to input it to the table.
Say you had 3 fields in the table (we'll call them A, B and C), and 3
textboxes on the form, (we'll call them txtA, txtB and txtC):

Private Sub YourSaveButton_Click()

Dim strA as String
Dim strB as String
Dim strC as String
Dim strSQL as String

strA = Nz(Me.txtA,"")
strB = Nz(Me.txtB,"")
strC = Nz(Me.txtC,"")

If Me.txtA = "" OR Me.txtB = "" OR Me.txtC = "" Then
Exit Sub
Else
strSQL = ""INSERT INTO YourTableName (A,B,C) " _
& "VALUES ('" & strA & "', '" & strB & "', '" & strC & "')"
DoCmd.RunSQL strSQL

End Sub


Be sure to replace your table, field and control names!
 
Forgot the End if. Code should be:

Private Sub YourSaveButton_Click()

Dim strA as String
Dim strB as String
Dim strC as String
Dim strSQL as String

strA = Nz(Me.txtA,"")
strB = Nz(Me.txtB,"")
strC = Nz(Me.txtC,"")

If Me.txtA = "" OR Me.txtB = "" OR Me.txtC = "" Then
Exit Sub
Else
strSQL = ""INSERT INTO YourTableName (A,B,C) " _
& "VALUES ('" & strA & "', '" & strB & "', '" & strC & "')"
DoCmd.RunSQL strSQL
End If

End Sub
 
Back
Top