Option Compare Database
Option Explicit
Private Sub cmdUpdate_Click()
On Error GoTo Err_cmdUpdate_Click
Dim rs As DAO.Recordset
Dim strFind As String, strMsg As String
Set rs = CurrentDb.OpenRecordset("MyTable", dbOpenDynaset)
strFind = "[LastName] = '" & Me.txtLastName & "' AND [FirstName] = '" & Me.txtFirstName & "'"
rs.FindFirst strFind
If Not rs.NoMatch Then
strMsg = "Record exists for " & Me.txtLastName & ", " & Me.txtFirstName & ". RecordID = " & rs("RecordID")
Else
strMsg = "Record does not exist for " & Me.txtLastName & ", " & Me.txtFirstName
End If
MsgBox strMsg, vbOKOnly, "Person Exists?"
Exit_cmdUpdate_Click:
'destroy object variable
Set rs = Nothing
Exit Sub
Err_cmdUpdate_Click:
MsgBox Err.Number & ", " & Err.Description, vbCritical, "Error in cmdUpdate procedure"
Resume Exit_cmdUpdate_Click
End Sub
Your steps will be
1) Does a record exist in the destination table for [FirstName] = txtFirstName AND [LastName] = txtLastName. You can use a DLookup function, a DAO RecordSet Find method in VBA, etc.
2) If True/Yes, run and update query: UPDATE MyTable SET Address = txtAddress, City = txtCity, State = txtState, Zip = txtZip, Phone = txtPhone WHERE [FirstName] = txtFirstName AND [LastName] = txtLastName
3) if False/No, run an insert query: INSERT INTO MyTable FirstName, LastName, Address, City, State, Zip, Phone VALUES (txtFirstName, txtLastName, txtAddress, etc.)
OM Gang