I have the following code which uses a form that has (two date boxes and two comboboxes) to allow the user to select criteria for a query result. It all works fine except when no records are found with that criteria. The query still opens with no records.
Question is, how can I insert a msgbox saying "no matching records found" prior to the query/and eventually a report, opening? - Private Sub cmdSearch_Click()
-
If IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
-
MsgBox "No Search Criteria Selected", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
-
Else
-
If Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
-
MsgBox "You must select a date range or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
-
Else:
-
If IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
-
MsgBox "You must select a VIP or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
-
Else
-
If Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
-
DoCmd.OpenQuery "qryWalkthrough", acViewNormal, acViewNormal
-
Else
-
If Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And Not IsNull([cboIssue]) Then
-
DoCmd.OpenQuery "qryWalkthrough", acViewNormal
-
Else
-
If IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
-
DoCmd.OpenQuery "qryWalkthrough", acViewNormal
-
Else
-
If Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
-
MsgBox "Please remove one search criteria", vbDefaultButton1 = vbOKOnly, "To many Critera fields chosen"
-
End If
-
End If
-
End If
-
End If
-
End If
-
End If
-
End If
-
-
End Sub
-
6 1586 zmbd 5,501
Recognized Expert Moderator Expert
You can use a Dcount() function against the query. YOu can F1 help on it or Google.
However, with the structure of your code, we'll have to jump thru hoops to get this to work properly.
I'm on my way out the door with kids to school or I'd take a stab...
Normally, I'd be using a DAO recordset and drastically different code to do what you're doing here.... stacked IF.THEN are not really the easiest/cleanest to work with.
Thanks, I'm all for easier/better ways of doing things but guess I went with what I know.
ADezii 8,834
Recognized Expert Expert zmbd pretty much answered your question for you. You need to first check for the existence of any Return Records for each Query using the DCount() Function before you actually Open the Query: -
Private Sub cmdSearch_Click()
-
On Error GoTo Err_cmdSearch_Click
-
Dim strCriteria As String
-
-
If IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
-
MsgBox "No Search Criteria Selected", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
-
Exit Sub
-
ElseIf Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
-
MsgBox "You must select a date range or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
-
Exit Sub
-
ElseIf IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
-
MsgBox "You must select a VIP or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
-
Exit Sub
-
ElseIf Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
-
strCriteria = "Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue])"
-
If DCount("*", "<Your Table>", strCriteria) <> 0 Then
-
DoCmd.OpenQuery "qryWalkthrough", acViewNormal, acViewNormal
-
End If
-
ElseIf Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And Not IsNull([cboIssue]) Then
-
strCriteria = "Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And Not IsNull([cboIssue])"
-
If DCount("*", "<Your Table>", strCriteria) <> 0 Then
-
DoCmd.OpenQuery "qryWalkthrough", acViewNormal
-
End If
-
ElseIf IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
-
strCriteria = "IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue])"
-
If DCount("*", "<Your Table>", strCriteria) <> 0 Then
-
DoCmd.OpenQuery "qryWalkthrough", acViewNormal
-
End If
-
ElseIf Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
-
MsgBox "Please remove one search criteria", vbDefaultButton1 = vbOKOnly, "To many Critera fields chosen"
-
Exit Sub
-
End If
-
End Sub
-
P.S. - The Exit Subs are for illustration purposes only.
ADezii, It needed the query name instead of the table name, but other than that it works perfect.
Thanks again
NeoPa 32,557
Recognized Expert Moderator MVP
There's a NoData event for reports. I expect that's all you need to handle this (unless I misread your intention).
Bear in mind, when Cancel is set in this event the code that calls the Report to Open fails (So this should also be handled for smooth operation).
ADezii 8,834
Recognized Expert Expert
Question is, how can I insert a msgbox saying "no matching records found" prior to the query/and eventually a report, opening?
I totally missed the ' eventually a Report' part. NeoPa's Reply in Post# 6 appears to be the way to go in this light.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bob Murdoch |
last post by:
I've got an intranet application that presents a list of files in sort of a
'central repository' web page. Each file is an href in the form <a
href=file://server/share/path/filename.ext>.
When...
|
by: PM |
last post by:
Has anyone found a way to open a file exclusively where it
will fail if the file is already open, i have tried the
following
_FileStream = new FileStream(@"C:\Data.txt",
FileMode.Open,...
|
by: UnixSlaxer |
last post by:
Hello,
Running a query for the first time on DB2 takes a fixed amount of time.
But when query is executed for the second time, the amount of time is
usually less since the query is (most...
|
by: Robert |
last post by:
I have a report that runs on a parameter query. I run the report, enter the
parameter, and it works as long as there is at least one record returned by
the query. But if it comes up empty, I get...
|
by: ronald balk |
last post by:
Dear people,
after many efforts I'm putting my hope on you.
Last week I changed a lot of forms and reports in an Access 2000 database.
It all worked fine and I closed the database without any...
| |
by: John Baker |
last post by:
Hi:
I have a pop up form based on a query.
I am openings the form, and wish to close it immediately if the query has result.
My method is to put a macro in the "on Open" event , which has...
|
by: MLH |
last post by:
If I give someone a runtime app,
they can open the database
window by pressing the F-11
key. How to prevent???
|
by: Max2006 |
last post by:
Hi,
I have a search result GridView bound to an ObjectDataSource. Once I open
the asp.net page, the ObjectDataSource automatically refreshes the GridView
which is the default behavior.
I am...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |