Custom Message if Parameter Value Not Found

pjmatelli

Board Regular
Joined
Oct 13, 2005
Messages
206
Hi All,
I am looking for a way to create a custom message to use when a user enters a value in a parameter box that simply doesn't exist.

e.g. I have a parameter looking for a PO#. if the PO# is found, a form opens with that PO#. If the user types an incorrect PO# (doesn't exist), I would like a message to come up saying that the PO does not exist.

I can't seem to figure this one out. Any help, as always, is greatly appreciated!

TIA
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Are you doing this directly through a Parameter Query, or do you have some sort of Selection Form?

If you are doing this through a Selection Form where you are dynamically building the query on the fly, you can count the records in your resulting query set and return in a message if the count is zero. I don't know that you can do it directly through a Parameter Query, I think VBA would have to be involved somewhere along the way.

If you are intersted in learning more about doing the form way, let me know. If involves building the SQL code for the query on the fly through the selection form.
 
Upvote 0
Joe,
Thanks for the reply...I am trying this directly from the query. When a button is clicked, it runs a query that opens a form, and then another form is opened depending on the result of the first form. The first form opens blank if the PO# is not valid and then the database locks up. Not sure if this helps, but I am always open to other ways to make this work! Thanks again.
 
Upvote 0
Here is a simple example of the method I was talking about.

Instead of having a paramter query, you can use a form to enter your criteria and build you query dynamically ("on-the-fly"). So let's assume that we have a simple form with a Combo Box (that is named "cboCriteria" in this example) where the user select's the criteria (could also use a Text Box for free form entry), and a Command Button (named "cmdProcess") that they click after entering the criteria to run.

This first block of code (which is attached to the Click event of the Command Button) builds the SQL code for the query, assigns that code to a query, counts the number of records that query will return and will open a form (if it returns more than zero) or return a Message Box (if it returns nothing).
Code:
Private Sub cmdProcess_Click()

'   Build SQL code based on criteria entered in combo box on form (cboCriteria)
    Dim mySQL As String
        
    mySQL = "SELECT [Table1].* FROM [Table1] WHERE [Table1].Field1=" & Chr(34) & cboCriteria & Chr(34)  & ";"

'   Assign SQL code to designated query (Query1)
    CurrentDb.QueryDefs("Query1").SQL = mySQL

'   Check count of records that query returns and opens form or return MsgBox
    If MyRecordCount("Query1)>0 Then
	DoCmd.OpenForm "Form1"
    Else
	MsgBox "No records match the criteria entered"
    End If

End Sub
This second block is simply a User Defined Function that the first block of VBA code uses to count the records in any given query.
Code:
Function MyRecordCount(myQuery As String) As Long
'   Counts records in a query

    Dim myRecords As Long
    Dim myRS As DAO.Recordset
    Set myRS = CurrentDb.OpenRecordset(myQuery)
    If Not myRS.EOF Then
        myRS.MoveLast
        MyRecordCount = myRS.RecordCount
    Else
        MyRecordCount = 0
    End If

Note: You must select the "Microsoft x.x DAO Reference Library" to use this code (where x.x is some numeric code - just pick whichever one is present).

Also, this is just a very simplified example. In reality you may want to add more conditions or sort options to your SQL code that is being built, and you may want to add some verifications to make sure they are making valid entries.

I also assumed that the Criteria is a text value. If it is nermic or boolean, you do not need the Chr(34) code (which just returns double-quotes).

Hope this helps!
 
Upvote 0
Thanks...I'll give it a try and let you know. It just needs to verify that the PO# exists...don't think it needs to be more complicated than that. Thanks so much!
 
Upvote 0
It just needs to verify that the PO# exists
You can use a Combo Box to restrict them just to values that appear in the table (for your Control Source, use an aggregate query on the PO# field from your data table).
 
Upvote 0
Code:
Private Sub cmdProcess_Click()
 
'   Build SQL code based on criteria entered in combo box on form (cboCriteria)
    Dim mySQL As String
 
    mySQL = "SELECT [Table1].* FROM [Table1] WHERE [Table1].Field1=" & Chr(34) & cboCriteria & Chr(34)  & ";"
 
'   Assign SQL code to designated query (Query1)
    CurrentDb.QueryDefs("Query1").SQL = mySQL
 
'   Check count of records that query returns and opens form or return MsgBox
    If MyRecordCount("Query1)>0 Then
    DoCmd.OpenForm "Form1"
    Else
    MsgBox "No records match the criteria entered"
    End If
 
End Sub
QUOTE]

In the above code, which part actually specified that the criteria entered in combo box on form (cboCriteria). I don't see any referance to cboCriteria in the code. I just want to know what the proper syntax is to refer to a value of a form object
 
Upvote 0
I don't see any referance to cboCriteria in the code. I just want to know what the proper syntax is to refer to a value of a form object

Look at the end of the line that begins:
mySQL = ...

Note: You'll need to scroll to the right within the code window to see it!<!-- / message -->
 
Upvote 0
Hello Joe4, thanks for the response once again...that scroll thing always gets me. as soon as the info is not on the immediate screen, i'm lost.

Anyway, as a follow up question, I noticed that the standard SQL code that gets automatically written when you create a standard query has a few lines, separated by carriage return. If I need to re-create this whole sql code using vb code like the example that you gave above, would I need to put the carriage returns between each line, and if so, do I use some ascii code for it?

Thanks again for your help so far.
 
Upvote 0
The carriage returns are not necessary. They are usually only added for readability sake in VBA. (You actually do NOT want any carriage returns located within your SQL code -- SQL won't like it).
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,691
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top