Dlookup from inputbox - if exists, open form

Rather than using a form based on a query with criteria, could I use something like this:

Dim myJobRef As Variant
    myJobRef = DLookup("[T-cardnumber]", "[tblMain]")
    InputBox "Enter Job Number", "Job Number Required", myJobRef

    If IsNull(myJobRef) Then
 
    MsgBox "Job not there"
   
        Else

        DoCmd.OpenForm "frmMain", acNormal, , "[T-cardnumber] = " & CLng(myJobRef)

    End If
   
End Sub

That asks the user to enter a job number.  If that job number exists in tblMain it will open a form called frmMain and display that record.  If the record does not exist in tblMain the user will get a messagebox "does not exist".

Thanks
Lapchien
LapchienDirectorAsked:
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.

Bat17Commented:
Yes you can, You might need something other than dlookup to get the default jobRef though, possibly DMax To get the last Job ID and then Dlookup to check they have given a valid ID

Dim myJobRef As Variant
    myJobRef = DMax("[T-cardnumber]", "[tblMain]")
    myJobRef = InputBox("Enter Job Number", "Job Number Required", myJobRef)
   
    If IsNull(DLookup("[T-cardnumber]", "[tblMain]", "[T-cardnumber] =" & CLng(myJobRef))) Then
        MsgBox "Job not there"
    Else
        DoCmd.OpenForm "frmMain", acNormal, , "[T-cardnumber] = " & CLng(myJobRef)
    End If
End Sub

0
tbsgadiCommented:
Hi Lapchen
Why not use a combo box on a form.
The combo box will query the T-cardnumber from tblMain & then the user will only be able to choose an existing Job
0
LapchienDirectorAuthor Commented:
Yeah, that's what I want (i think!)!  If the user clicks Cancel on the input box I get an error though - can I trap this?
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.

tbsgadiCommented:
You don't use an input box you use a form..
If you want you can use the same form as the data.
You can use the combo box wizard to help.
0
LapchienDirectorAuthor Commented:
Yes, I see that.  I have amended the forms query to use a new parameter based on whatever is entered in the combo box.  But how do I then open the frmMain with the corect record?
0
Bat17Commented:
Just check for an empty string before doing the lookup

Dim myJobRef As Variant
    myJobRef = DMax("[ID]", "[capital]")
    myJobRef = InputBox("Enter Job Number", "Job Number Required", myJobRef)
    If myJobRef = "" Then Exit Sub
    If IsNull(DLookup("[id]", "[capital]", "[id] =" & CLng(myJobRef))) Then
        MsgBox "Job not there"
    Else
        DoCmd.OpenForm "frmMain", acNormal, , "[T-cardnumber] = " & CLng(myJobRef)
    End If
End Sub

Peter
0
Bat17Commented:
oops

Dim myJobRef As Variant
    myJobRef = DMax("[T-cardnumber]", "[tblMain]")
    myJobRef = InputBox("Enter Job Number", "Job Number Required", myJobRef)
    If myJobRef = "" Then Exit Sub
    If IsNull(DLookup("[T-cardnumber]", "[tblMain]", "[T-cardnumber] =" & CLng(myJobRef))) Then
        MsgBox "Job not there"
    Else
        DoCmd.OpenForm "frmMain", acNormal, , "[T-cardnumber] = " & CLng(myJobRef)
    End If
End Sub

Peter
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
tbsgadiCommented:
If your'e using a separate form just use DoCmd.OpenForm "frmMain" & hide the present form.
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.