Solved

Confirming a record exists on load event of a form

Posted on 2008-10-07
6
283 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel 2007 Comparing Cell Contents with IF Statements 12 36
IIF in access query 19 23
Copying an open file 3 17
query sort by digit 5 7
Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

809 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