Open Form, only if matching records are available, +

I use the code below to open a single record view form. What additional code can I add to do two things.
1. If there are no matching records, a message pops up saying "No Matching Records", and the form doesn't open.
and
2.  If there are multiple matching records, a message pops up saying "Multiple Matching Records", and then the form opens upon aknowledgement .

.....................................
Private Sub Command602_Click()
On Error GoTo Err_Command602_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Keys1Form"
   
    stLinkCriteria = "[LastName]like" & "'" & "*" & Forms![Main1]![KeyFind] & "*" & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
       
Exit_Command602_Click:
    Exit Sub

Err_Command602_Click:
    MsgBox Err.Description
    Resume Exit_Command602_Click
   
End Sub
...........................................................

Thanks,
Mark

wesellnowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shanesuebsahakarnCommented:
Did the dcount in your previous not do the trick? You can modify it as follows:

If DCount("*","qryCancelContact",strLinkCriteria)=0 Then
   MsgBox "No matching records!"
ElseIf DCount("*","qryCancelContact",strLinkCriteria)=1 Then
   DoCmd.OpenForm "CancelContact1", , ,strLinkCriteria
Else
   MsgBox "Multiple matching records!"
   DoCmd.OpenForm "CancelContact1", , ,strLinkCriteria
End If
0
wesellnowAuthor Commented:
The DCount worked fine.

The issue is slightly different. Sometimes the name we search on when opening "Keys1Form" does not exist in the customer database. We need to not open the form in this case. The count we are working on now pertains to this form rather than the "CancelContact1" form.

Thanks,

<email address removed by OzzMod>

 
0
wesellnowAuthor Commented:
Reread the original question above and it will come clear to you.
Thanks,

<email address removed by OzzMod>

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

shanesuebsahakarnCommented:
If DCount("*","qryKeys1Form",strLinkCriteria)=0 Then
   MsgBox "No matching records!"
ElseIf DCount("*","qryKeys1Form",strLinkCriteria)=1 Then
   DoCmd.OpenForm "Keys1Form", , ,strLinkCriteria
Else
   MsgBox "Multiple matching records!"
   DoCmd.OpenForm "Keys1Form", , ,strLinkCriteria
End If

Replacing qryKeys1Form with the name of the query upon which Keys1Form is based.
0
wesellnowAuthor Commented:
I am having trouble integrating the code you have given me with what is in place now:

Private Sub Command602_Click()
On Error GoTo Err_Command602_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Keys1Form"
   
    stLinkCriteria = "[LastName]like" & "'" & "*" & Forms![Main1]![KeyFind] & "*" & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
       
Exit_Command602_Click:
    Exit Sub

Err_Command602_Click:
    MsgBox Err.Description
    Resume Exit_Command602_Click
   
End Sub
0
wesellnowAuthor Commented:
The underlying query for "Keys1Form" is Keys1
0
shanesuebsahakarnCommented:
Replace this:
    DoCmd.OpenForm stDocName, , , stLinkCriteria

with:
If DCount("*","Keys1",strLinkCriteria)=0 Then
   MsgBox "No matching records!"
ElseIf DCount("*","Keys1",strLinkCriteria)=1 Then
   DoCmd.OpenForm stDocName, , ,strLinkCriteria
Else
   MsgBox "Multiple matching records!"
   DoCmd.OpenForm stDocName, , ,strLinkCriteria
End If
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wesellnowAuthor Commented:
I am getting this segment highlighted:   strLinkCriteria

with error "variable not defined"

Thanks,

<email address removed by OzzMod>

0
wesellnowAuthor Commented:
I got it, just needed to remove the r from the strLinkCriteria

Works perfectly
0
wesellnowAuthor Commented:
Thanks for sticking with me on this. Busy day.

Mark
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.