If record doesn't exist

supmktg

Registered User.
Local time
Today, 08:12
Joined
Mar 25, 2002
Messages
360
I'm using the following code to autofill the city and state on my form.
I can't figure out how to capture if there is no matching record in the table.
If there is no matching record, I will:

1) inform the user with a msgbox
then if the user wants
2) open a form to add the record to the table

Code:
    Dim rst As DAO.Recordset
    Dim db As DAO.Database
    
    Set db = CurrentDb
    Set rst = db.OpenRecordset("tblZipcodes", dbOpenTable)



    rst.MoveLast
    rst.MoveFirst

    Do Until rst.EOF

    If strZipLookup = rst!ZipCode Then
    Me.txtCoCity = rst![ZipCity]
    Me.cboCoSt = rst![ZipState]
    
    End If

    rst.MoveNext
    
Loop
    
rst.Close
End If

How can I capture if there is no matching record in the table?

Thanks,

Sup
 
Did you try with:

If strZipLookup = Null Then...
 
ssoltic,

That didn't work, but thanks. I changed my code slightly to use the findfirst method and that worked. Here's the new code:

Code:
    Dim rst As DAO.Recordset
    Dim db As DAO.Database
    
    Set db = CurrentDb
    Set rst = db.OpenRecordset("tblZipcodes", dbOpenDynaset)

    rst.FindFirst "[Zipcode] = '" & strZipLookup & "'"
   
    If strZipLookup = rst!ZipCode Then
    Me.txtCoCity = rst![ZipCity]
    Me.cboCoSt = rst![ZipState]
    Else
    If MsgBox("     Zipcode " & strZipLookup & " is not in the autofill list." &     vbCrLf & "Would you like to add " & strZipLookup & " to the Autofill list?", vbQuestion + vbYesNo, "Add to Zipcode List?") = vbYes Then
    DoCmd.OpenForm "frmAddZipcode", OpenArgs:=Me.txtCoZip
    End If
    End If
 
    rst.Close
 

Users who are viewing this thread

Back
Top Bottom