Solved

Confirming a record exists on load event of a form

Posted on 2008-10-07
6
277 Views
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()

Form.OnLoad

End Sub
0
Comment
Question by:Zerofun
6 Comments
 
LVL 20

Expert Comment

by:clarkscott
ID: 22663084
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"
       docmd.close
end if
 
Scott C
0
 

Author Comment

by:Zerofun
ID: 22663137
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.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22663494
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?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 38

Accepted Solution

by:
Jim P. earned 500 total points
ID: 22663526
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

        Else

            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

Loop
 

End Sub

Open in new window

0
 

Author Closing Comment

by:Zerofun
ID: 31503981
Thanks, you went above and beyond. this helped me a lot.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 22664255
Glad to be of assistance. May all your days get brighter and brighter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

914 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

17 Experts available now in Live!

Get 1:1 Help Now