Confirming a record exists on load event of a form

Posted on 2008-10-07
Last Modified: 2012-05-05
I know nothing about VBA.. but i use access regularly. (my VBA class is soon thank god)

here is my issue..

I have a report and a form that ask for "Lot_Number" when they are launched.

once the user enters that "Lot_Number" it launches the approite report or form.

If the user enters an incorrect lot number it still launches the form to the first "lot_number" record.

I want to return an error if the lot number is not in the system when they input it.

This is all i have so far..

Private Sub Form_Load()


End Sub
Question by:Zerofun
LVL 20

Expert Comment

Comment Utility
In your first form... open the second form with
docmd.openform "yourform",,,,"Lot_Number = " & me.YourLotNumberField
in the Load event of the second form...
If isnull(me.YourLotNumber) then
       msgbox "no data"
end if
Scott C

Author Comment

Comment Utility
i need it to return the error even if they enter data... if the number they enter is not matched to a lot number in the database i need it to let them know and stop.
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
then, you have to check the existence of the record first before opening the form

how are you opening the form right now?
how is the data entered?
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

LVL 38

Accepted Solution

Jim P. earned 500 total points
Comment Utility
Try this:
Private Sub Form_Load()

Dim Lookup As String

Dim ValidLot As Boolean

Dim Msg As String

Dim Response As Integer

Lookup = ""

ValidLot = False

Do Until Lookup <> "" And ValidLot = True

    Lookup = InputBox("Please enter the lot number", "Lot Number", Lookup)


    If Lookup = "" Then

        MsgBox "Nothing entered. Please enter a valid lot number.", _

                vbOKOnly + vbCritical, "No Selection"

        ValidLot = False

    End If

    If Lookup <> "" Then

        If Lookup = DLookup("Lot_Number", "MyTable", "Lot_Number = '" & Trim(Lookup) & "'") Then

            Me.Filter = "Lot_Number = '" & Trim(Lookup) & "'"

            Me.FilterOn = True

            ValidLot = True


            Msg = "The lot number " & Lookup & " does not " & _

                "appear to be valid. " & _

                Chr(13) & _

                "Please enter a valid lot number " & Chr(13) & _

                "or cancel to exit."


            Response = MsgBox(Msg, vbCritical + vbOKCancel, "Invalid lot number")

            If Response = vbCancel Then

                ValidLot = False

                DoCmd.Close acForm, Me.NAME, acSaveYes

                Exit Sub

            End If

        End If

    End If


End Sub

Open in new window


Author Closing Comment

Comment Utility
Thanks, you went above and beyond. this helped me a lot.
LVL 38

Expert Comment

by:Jim P.
Comment Utility
Glad to be of assistance. May all your days get brighter and brighter.

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now