I hope someone reading this can help! I have a form that is used for creating new records, editing existing records and removing records from a table (named "Contacts"). When I click the save button after editing an existing record it work like it should. If I click my "Add Contact" button the form clears (as it should) but the data entered into the fields does not populate. I stepped through the code, and found the IF statement following .FindFirst "[ID]=" & Me.ID is getting skipped, and I'm not sure why. I am including the code from my "Add Contact" button and the "Save" button below. Any help is greatly appreciated!!!!!!!!!!!!!!!!!!!!!!!
Option Compare Database
Private Sub AddContact_Click()
With Me
.ID = -1
.ID.Visible = False
' .ID_Label.Visible = False
.Company = ""
.Address = ""
.Address_2 = ""
.Contact = ""
.City = ""
.Address_3 = ""
.State = ""
.Zip_Code = ""
.Country = ""
End With
End Sub
Private Sub cmdSave_Click()
Dim db As Database
Dim rs As Recordset
Dim msg As String
msg = "You are about to save a record." & vbNewLine & vbNewLine _
& "Are you sure you want to continue?"
If MsgBox(msg, vbYesNo) = vbNo Then
MsgBox "Record not updated."
Exit Sub
End If
Set db = CurrentDb
Set rs = db.OpenRecordset("Contacts", dbOpenDynaset)
If Me.ID > 0 Then
With rs
'Writes data to table
.FindFirst "[ID]=" & Me.ID
If Not .NoMatch Then
.Edit
.Fields("Company") = Me.Company
.Fields("Address") = Me.Address
.Fields("Address_2") = Me.Address_2
.Fields("Contact") = Me.Contact
.Fields("City") = Me.City
.Fields("Address_3") = Me.Address_3
.Fields("State") = Me.State
.Fields("Zip_Code") = Me.Zip_Code
.Fields("Country") = Me.Country
.Update
Else
'Writes data to table
.AddNew
.Fields("Company") = Me.Company
.Fields("Address") = Me.Address
.Fields("Address_2") = Me.Address_2
.Fields("Contact") = Me.Contact
.Fields("City") = Me.City
.Fields("Address_3") = Me.Address_3
.Fields("State") = Me.State
.Fields("Zip_Code") = Me.Zip_Code
.Fields("Country") = Me.Country
.Update
Me.ID = DLast("[ID]", "Contacts")
End If
End With
MsgBox "Record Saved"
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub