Offer choice if .NoMatch = True (1 Viewer)

tabitha

Registered User.
Local time
Today, 06:03
Joined
Apr 24, 2015
Messages
62
So I have a form for addresses that checks against a table of US zip codes, and throws up a warning if the City/State/Zip entered isn't found on the table. This works great. But I was wondering if there was a way to list the available combinations based on the zip code entered. I want the program to tell the user that the city is wrong, but here is a list of what it could be.

Is it possible for a msgbox to list all records where a field = value?
 

Cronk

Registered User.
Local time
Today, 23:03
Joined
Jul 4, 2013
Messages
2,772
Open up a recordset of cities with the zip code, loop through it and append each value to a string that is displayed in the message box.

Do what you can with that and come back if you want help with anying specific.
 

tabitha

Registered User.
Local time
Today, 06:03
Joined
Apr 24, 2015
Messages
62
This is my code:

Code:
Function CheckZipMain() As Byte

Dim dbs As DAO.Database
Dim rstZip As DAO.Recordset

Dim stCurrent As String
Dim stCorrect As String
Dim stCurrentZip As String
Dim stCurrentState As String
Dim stCurrentCity As String
Dim stMsg As String
Dim stCorrectCity As String
Dim stCorrectState As String
Dim stZipFound As String
Dim varBookmark As Variant

'Gathers the data from the open Form [Employees Main Form]
stCurrentZip = [Forms]![Employees Main Form]![Employee Zip]
stCurrentState = [Forms]![Employees Main Form]![Employee State]
stCurrentCity = [Forms]![Employees Main Form]![Employee City]
stCurrent = stCurrentCity & ", " & stCurrentState & " " & stCurrentZip

Set dbs = CurrentDb
Set rstZip = dbs.OpenRecordset("ZipCodes", dbOpenTable)

'Verifies if the zip code entered on the form is found in the ZipCodes table
stZipFound = Nz(DLookup("[Zip]", "ZipCodes", "[Zip] = '" & stCurrentZip & "'"), False)

If stZipFound = False Then
    MsgBox ("Invalid zip. Please correct.")
Else
    
    'Index created on ZipCodes table of Zip, then City, then State
    rstZip.Index = "ZCS"
    
    'Checking to see if the City/State/Zip combination is present on the ZipCodes table
    rstZip.Seek "=", stCurrentZip, stCurrentCity, stCurrentState

    If rstZip.NoMatch Then
        Do While rstZip.EOF = False
            
            'Gathers the Zip from [Employees Main Form], to populate the correct City/State
            rstZip.Seek "=", stCurrentZip
            stCorrectCity = rstZip!City
            stCorrectState = rstZip!State
            stCorrect = stCorrectCity & ", " & stCorrectState & " " & stCurrentZip
            
            'Asks if this is the City/State/Zip required
            stMsg = MsgBox("Invalid city/state/zip combination. Is this the correct combo? " & _
                           stCorrect, vbYesNoCancel)
            
            'If it is, then it updates the field to match
            If stMsg = vbYes Then
                [Forms]![Employees Main Form]![Employee City] = stCorrectCity
                [Forms]![Employees Main Form]![Employee State] = stCorrectState
                Exit Do
            ElseIf stMsg = vbCancel Then
                Exit Do
            End If
        
        rstZip.MoveNext
        Loop
    Else
    End If
End If

rstZip.Close
Set rstZip = Nothing
End Function

Currently, it checks the zip and prompts correctly, with it exiting right when I want it to. However, it just keeps pulling the first record it finds for that zip code. I have a rstZip.MoveNext, but it must be in the wrong place?? I've tried many different places, all with the same result. What am I missing?
 

Solo712

Registered User.
Local time
Today, 09:03
Joined
Oct 19, 2012
Messages
828
This is my code:

Code:
Function CheckZipMain() As Byte
 
Dim dbs As DAO.Database
Dim rstZip As DAO.Recordset
 
Dim stCurrent As String
Dim stCorrect As String
Dim stCurrentZip As String
Dim stCurrentState As String
Dim stCurrentCity As String
Dim stMsg As String
Dim stCorrectCity As String
Dim stCorrectState As String
Dim stZipFound As String
Dim varBookmark As Variant
 
'Gathers the data from the open Form [Employees Main Form]
stCurrentZip = [Forms]![Employees Main Form]![Employee Zip]
stCurrentState = [Forms]![Employees Main Form]![Employee State]
stCurrentCity = [Forms]![Employees Main Form]![Employee City]
stCurrent = stCurrentCity & ", " & stCurrentState & " " & stCurrentZip
 
Set dbs = CurrentDb
Set rstZip = dbs.OpenRecordset("ZipCodes", dbOpenTable)
 
'Verifies if the zip code entered on the form is found in the ZipCodes table
stZipFound = Nz(DLookup("[Zip]", "ZipCodes", "[Zip] = '" & stCurrentZip & "'"), False)
 
If stZipFound = False Then
    MsgBox ("Invalid zip. Please correct.")
Else
 
    'Index created on ZipCodes table of Zip, then City, then State
    rstZip.Index = "ZCS"
 
    'Checking to see if the City/State/Zip combination is present on the ZipCodes table
    rstZip.Seek "=", stCurrentZip, stCurrentCity, stCurrentState
 
    If rstZip.NoMatch Then
        Do While rstZip.EOF = False
 
            'Gathers the Zip from [Employees Main Form], to populate the correct City/State
           [COLOR=red]rstZip.Seek "=", stCurrentZip[/COLOR]
            stCorrectCity = rstZip!City
            stCorrectState = rstZip!State
            stCorrect = stCorrectCity & ", " & stCorrectState & " " & stCurrentZip
 
            'Asks if this is the City/State/Zip required
            stMsg = MsgBox("Invalid city/state/zip combination. Is this the correct combo? " & _
                           stCorrect, vbYesNoCancel)
 
            'If it is, then it updates the field to match
            If stMsg = vbYes Then
                [Forms]![Employees Main Form]![Employee City] = stCorrectCity
                [Forms]![Employees Main Form]![Employee State] = stCorrectState
                Exit Do
            ElseIf stMsg = vbCancel Then
                Exit Do
            End If
 
        rstZip.MoveNext
        Loop
    Else
    End If
End If
 
rstZip.Close
Set rstZip = Nothing
End Function

Currently, it checks the zip and prompts correctly, with it exiting right when I want it to. However, it just keeps pulling the first record it finds for that zip code. I have a rstZip.MoveNext, but it must be in the wrong place?? I've tried many different places, all with the same result. What am I missing?

The line marked red pulls the recordset to the same record. MoveNext has no effect. Youd could use dbOpenDynaset with the "rstZip =" as Criteria and do rst.Zip FindNext Criteria to move through the loop.

Best,
Jiri
 

tabitha

Registered User.
Local time
Today, 06:03
Joined
Apr 24, 2015
Messages
62
I just tried the .FindFirst, and I had actually tried that, but I need to find where the City, State, and Zip together are on the table, and it seems to me like I can search a FindFirst with only one argument?

As in:

Code:
rstZip.FindFirst "Zip =  '" & stCurrentZip & "'"

Works, but only searches for the zip. I would need:

Code:
rstZip.FindFirst "Zip =  '" & stCurrentZip & "And City = '" & stCurrentCity & _
                     "And State = '" & stCurrentState & "'"

To verify that the combination of the three is present, but I'm getting a run-time error 3077 when I type that in.
 

tabitha

Registered User.
Local time
Today, 06:03
Joined
Apr 24, 2015
Messages
62
Okay, I got the syntax for the .FindFirst with multiple arguments, but that didn't solve anything. Here's where I'm confused.
Code:
Do While rstZip.EOF = False
            
            [COLOR="Red"]rstZip.FindFirst "Zip =  '" & stCurrentZip & "'"[/COLOR]
            stCorrectCity = rstZip!City
            stCorrectState = rstZip!State
            stCorrect = stCorrectCity & ", " & stCorrectState & " " & stCurrentZip
            
            'Asks if this is the City/State/Zip required
            stMsg = MsgBox("Invalid city/state/zip combination. Is this the correct combo? " & _
                           stCorrect, vbYesNoCancel)
            
            'If it is, then it updates the field to match
            If stMsg = vbYes Then
                [Forms]![Employees Main Form]![Employee City] = stCorrectCity
                [Forms]![Employees Main Form]![Employee State] = stCorrectState
                Exit Do
            ElseIf stMsg = vbCancel Then
                Exit Do
            End If
        
        rstZip.MoveNext
        Loop

I put the .FindFirst in red. I still need it to search based off the zip code, and I need it to start with the first one on the list, I just need it to cycle through all records that has that zip code, and only that zip code.
 

Solo712

Registered User.
Local time
Today, 09:03
Joined
Oct 19, 2012
Messages
828
I just tried the .FindFirst, and I had actually tried that, but I need to find where the City, State, and Zip together are on the table, and it seems to me like I can search a FindFirst with only one argument?

As in:

Code:
rstZip.FindFirst "Zip =  '" & stCurrentZip & "'"

Works, but only searches for the zip. I would need:

Code:
rstZip.FindFirst "Zip =  '" & stCurrentZip & "And City = '" & stCurrentCity & _
                     "And State = '" & stCurrentState & "'"

To verify that the combination of the three is present, but I'm getting a run-time error 3077 when I type that in.

You are getting an error because of missing spaces before the 'And' .. should be

Code:
rstZip.FindFirst "Zip = '" & stCurrentZip & " And City = '" & stCurrentCity & _
"[COLOR=red]' [/COLOR]And State = '" & stCurrentState & "'"

But there is another issue. Inside the DO loop you need another .NoMatch test, this time on stCurrentZip alone, like

Code:
If rstZip.NoMatch Then
      rstZip.FindFirst "Zip = '" & stCurrentZip & "'"
      Do While Not rstZip.NoMatch
      :
      :
          rstZip.FindNext "Zip = '" & stCurrentZip & "'" 
       Loop

Hope it works..

Best,
Jiri

ETA, In the future kindly let me respond before you write another post. Much less messy that way !
 

tabitha

Registered User.
Local time
Today, 06:03
Joined
Apr 24, 2015
Messages
62
Thank you for the typo with the And's, you were right, I did need a space. Instead of putting a .FindNext inside the Do, I put the .FindFirst just outside of it, and it's looping correctly now! This is what I have now that works perfectly (I think!):
Code:
Option Compare Database
Option Explicit

Function CheckZipMain() As Byte

Dim dbs As DAO.Database
Dim rstZip As DAO.Recordset

Dim stCurrent As String
Dim stCorrect As String
Dim stCurrentZip As String
Dim stCurrentState As String
Dim stCurrentCity As String
Dim stMsg As String
Dim stCorrectCity As String
Dim stCorrectState As String
Dim stZipFound As String
Dim stSearch As String


'Gathers the data from the open Form [Employees Main Form]
stCurrentZip = [Forms]![Employees Main Form]![Employee Zip]
stCurrentState = [Forms]![Employees Main Form]![Employee State]
stCurrentCity = [Forms]![Employees Main Form]![Employee City]
stCurrent = stCurrentCity & ", " & stCurrentState & " " & stCurrentZip

Set dbs = CurrentDb
Set rstZip = dbs.OpenRecordset("ZipCodes", dbOpenDynaset)

'Verifies if the zip code entered on the form is found in the ZipCodes table
stZipFound = Nz(DLookup("[Zip]", "ZipCodes", "[Zip] = '" & stCurrentZip & "'"), False)

If stZipFound = False Then
    MsgBox ("Invalid zip. Please correct.")
Else
    'Checking to see if the City/State/Zip combination is present on the ZipCodes table
    stSearch = "Zip =  '" & stCurrentZip & "'" & " And City = '" & stCurrentCity & "'" & _
                     " And State = '" & stCurrentState & "'"
    rstZip.FindFirst stSearch
    
    If rstZip.NoMatch Then
        
        rstZip.FindFirst "Zip =  '" & stCurrentZip & "'"
        
        Do While Not rstZip.NoMatch
            
            'Populates the correct City/State
            stCorrectCity = rstZip!City
            stCorrectState = rstZip!State
            stCorrect = stCorrectCity & ", " & stCorrectState & " " & stCurrentZip
            
            'Asks if this is the City/State/Zip required
            stMsg = MsgBox("Invalid city/state/zip combination. Is this the correct combo? " & _
                           stCorrect, vbYesNoCancel)
            
            'If it is, then it updates the field to match
            If stMsg = vbYes Then
                [Forms]![Employees Main Form]![Employee City] = stCorrectCity
                [Forms]![Employees Main Form]![Employee State] = stCorrectState
                Exit Do
            ElseIf stMsg = vbCancel Then
                Exit Do
            End If
        
        rstZip.MoveNext
        Loop
    Else
    End If
End If

rstZip.Close
Set rstZip = Nothing
End Function

Thank you for your help, and sorry I jumped the gun a little! I was just going with my train of thought :p
 

Solo712

Registered User.
Local time
Today, 09:03
Joined
Oct 19, 2012
Messages
828
Thank you for the typo with the And's, you were right, I did need a space. Instead of putting a .FindNext inside the Do, I put the .FindFirst just outside of it, and it's looping correctly now! This is what I have now that works perfectly (I think!):
Code:
Option Compare Database
Option Explicit
 
Function CheckZipMain() As Byte
 
Dim dbs As DAO.Database
Dim rstZip As DAO.Recordset
 
Dim stCurrent As String
Dim stCorrect As String
Dim stCurrentZip As String
Dim stCurrentState As String
Dim stCurrentCity As String
Dim stMsg As String
Dim stCorrectCity As String
Dim stCorrectState As String
Dim stZipFound As String
Dim stSearch As String
 
 
'Gathers the data from the open Form [Employees Main Form]
stCurrentZip = [Forms]![Employees Main Form]![Employee Zip]
stCurrentState = [Forms]![Employees Main Form]![Employee State]
stCurrentCity = [Forms]![Employees Main Form]![Employee City]
stCurrent = stCurrentCity & ", " & stCurrentState & " " & stCurrentZip
 
Set dbs = CurrentDb
Set rstZip = dbs.OpenRecordset("ZipCodes", dbOpenDynaset)
 
'Verifies if the zip code entered on the form is found in the ZipCodes table
stZipFound = Nz(DLookup("[Zip]", "ZipCodes", "[Zip] = '" & stCurrentZip & "'"), False)
 
If stZipFound = False Then
    MsgBox ("Invalid zip. Please correct.")
Else
    'Checking to see if the City/State/Zip combination is present on the ZipCodes table
    stSearch = "Zip =  '" & stCurrentZip & "'" & " And City = '" & stCurrentCity & "'" & _
                     " And State = '" & stCurrentState & "'"
    rstZip.FindFirst stSearch
 
    If rstZip.NoMatch Then
 
        rstZip.FindFirst "Zip =  '" & stCurrentZip & "'"
 
        Do While Not rstZip.NoMatch
 
            'Populates the correct City/State
            stCorrectCity = rstZip!City
            stCorrectState = rstZip!State
            stCorrect = stCorrectCity & ", " & stCorrectState & " " & stCurrentZip
 
            'Asks if this is the City/State/Zip required
            stMsg = MsgBox("Invalid city/state/zip combination. Is this the correct combo? " & _
                           stCorrect, vbYesNoCancel)
 
            'If it is, then it updates the field to match
            If stMsg = vbYes Then
                [Forms]![Employees Main Form]![Employee City] = stCorrectCity
                [Forms]![Employees Main Form]![Employee State] = stCorrectState
                Exit Do
            ElseIf stMsg = vbCancel Then
                Exit Do
            End If
 
        rstZip.MoveNext
        Loop
    Else
    End If
End If
 
rstZip.Close
Set rstZip = Nothing
End Function

Thank you for your help, and sorry I jumped the gun a little! I was just going with my train of thought :p

You are welcome. tabitha, your loop may work now but the looping is incorrect. You are relying on the user either answering 'Yes' or 'Cancel' to exit the loop. If the answer is 'No' at the last Zip then the routine will bomb. Also, the loop with MoveNext instead of FindNext will go through zips which <> stCurrentZip which I'm not sure you really intended. The NoMatch in the Do While statement then relates to the FindFirst and will always evaluate the same way.

Best,
Jii
 

Cronk

Registered User.
Local time
Today, 23:03
Joined
Jul 4, 2013
Messages
2,772
Searching a recordset of the whole zip table is inefficient.

Open the recordset on matching zip codes
eg
Code:
set rstZip =db.openrecordset(Select * from ZipCodes where zip = '" & stCurrentZip & "'")

Test if there are any records returned, then loop through the recordset which contains only cities with the entered zip.
Code:
with rstzip
  if .count >0 then
     .movefirst
     do while not .eof
        stCorrect = !City & ", " & !State & " " & !Zip
        '--Note msgbox() returns an integer not a string
        intChoice = msgbox("....." & stCorrect,vbyesnocancel,"This city?")
        if intChoice = vbyes then
            exit do
        elseif intchoice = vbcancel
           stCorrect =""
        end if
        .movenext
    loop
  endif
end with

Note there is less code this way.
 

tabitha

Registered User.
Local time
Today, 06:03
Joined
Apr 24, 2015
Messages
62
Cronk, thanks for the suggestion, that cut my code way down, and looped correctly.
Solo, it won't let me change the .MoveNext to .FindNext; it says that "Argument is not available". I have no idea why, but since I used Cronk's method of filtering the recordset before doing any loops, that should prevent it from looping to <>stCurrentZip, right?
 

Solo712

Registered User.
Local time
Today, 09:03
Joined
Oct 19, 2012
Messages
828
Cronk, thanks for the suggestion, that cut my code way down, and looped correctly.
Solo, it won't let me change the .MoveNext to .FindNext; it says that "Argument is not available". I have no idea why, but since I used Cronk's method of filtering the recordset before doing any loops, that should prevent it from looping to <>stCurrentZip, right?

It probably said "Argument not optional" because you forgot to include the argument. At any rate, with restricting the recordset to the current zip, MoveNext will do fine.

Best,
Jiri
 

Users who are viewing this thread

Top Bottom