I always create an unbound form and use the SQL INSERT INTO query to put the data from the fields in my form into the table. I add the INSERT query to the Click event of a command button, usually called 'Save' or 'Submit'.
Here's an example of a Click event with an INSERT query:
-
Private Sub cmdSave_Click()
-
-
Dim strSQL As String
-
-
'Assuming that TableFields are fields in your table
-
'Assuming that FormFields are fields on your form
-
'Assumes that FormField1 is numeric, FormField2 is a string, and FormField3 is a date
-
strSQL = "INSERT INTO YourTable([TableField1], [TableField2], [TableField3]) VALUES (" & Me.FormField1 & ", '" & Me.FormField2 & ", #" & Me.FormField3 & "#);"
-
-
Msgbox "Save Complete"
-
-
End Sub
-
To cancel, just create a command button similar to the above, except instead of using the SQL query, just use the following:
-
Private Sub Cancel_Click()
-
-
Dim userSelection
-
-
userSelection = MsgBox("Are you sure you want to close the form without saving?", vbExclamation + vbYesNo, "Cancel")
-
-
If userSelection = vbYes Then
-
DoCmd.Close acForm, Me.Name
-
MsgBox "You will now be returned to the Main Menu", vbOKOnly + vbInformation, "Return to Main Menu"
-
DoCmd.OpenForm "Switchboard"
-
ElseIf userSelection = vbNo Then
-
Exit Sub
-
Else
-
MsgBox "An error has occurred. You will be returned to the form.", vbCritical + vbOKOnly, "Error"
-
Exit Sub
-
End If
-
End Sub
-
Hope this helps,
beacon