Results 1 to 3 of 3
  1. #1
    jsw2179 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    2

    IF statement getting skipped on button click

    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

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Instead of searching recordset, why don't you just open it filtered to the desired record?

    Set rs = db.OpenRecordset("SELECT * FROM Contacts WHERE ID=" & Me.ID & ";", dbOpenDynaset)
    If Not rs.EOF Then
    'edit record
    Else
    'add record
    End If

    Why are you using code to create record? Is this an unbound form? Why not bound form?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    jsw2179 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    2
    Correct, the form is unbound. I could bind the form, but at this point my only hold up is saving new records.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Click button to open a new image
    By tia in forum Forms
    Replies: 2
    Last Post: 10-10-2011, 11:03 AM
  2. Trying to click a button in vba
    By boywonder in forum Programming
    Replies: 8
    Last Post: 05-02-2011, 04:34 AM
  3. +1 on button click
    By 10 Gauge in forum Forms
    Replies: 4
    Last Post: 02-14-2011, 06:51 AM
  4. Command button click event
    By R_jang in forum Programming
    Replies: 10
    Last Post: 10-29-2010, 10:13 PM
  5. Increment a value on button click
    By michaeljohnh in forum Programming
    Replies: 9
    Last Post: 08-25-2010, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums