473,503 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prevent empty query from opening

48 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. If IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
  3. MsgBox "No Search Criteria Selected", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
  4. Else
  5. If Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
  6. MsgBox "You must select a date range or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
  7. Else:
  8. If IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
  9. MsgBox "You must select a VIP or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
  10. Else
  11. If Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
  12. DoCmd.OpenQuery "qryWalkthrough", acViewNormal, acViewNormal
  13. Else
  14. If Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And Not IsNull([cboIssue]) Then
  15. DoCmd.OpenQuery "qryWalkthrough", acViewNormal
  16. Else
  17. If IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
  18. DoCmd.OpenQuery "qryWalkthrough", acViewNormal
  19. Else
  20. If Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
  21. MsgBox "Please remove one search criteria", vbDefaultButton1 = vbOKOnly, "To many Critera fields chosen"
  22. End If
  23. End If
  24. End If
  25. End If
  26. End If
  27. End If
  28. End If
  29.  
  30. End Sub
  31.  
Jan 16 '13 #1
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.
Jan 16 '13 #2
SeadooRider
48 New Member
Thanks, I'm all for easier/better ways of doing things but guess I went with what I know.
Jan 16 '13 #3
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. On Error GoTo Err_cmdSearch_Click
  3. Dim strCriteria As String
  4.  
  5. If IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
  6.   MsgBox "No Search Criteria Selected", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
  7.     Exit Sub
  8. ElseIf Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And IsNull([cboIssue]) Then
  9.   MsgBox "You must select a date range or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
  10.     Exit Sub
  11. ElseIf IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
  12.   MsgBox "You must select a VIP or issue type to continue", vbDefaultButton1 = vbOKOnly, "Insufficient Search Critera"
  13.     Exit Sub
  14. ElseIf Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue]) Then
  15.   strCriteria = "Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And IsNull([cboIssue])"
  16.     If DCount("*", "<Your Table>", strCriteria) <> 0 Then
  17.       DoCmd.OpenQuery "qryWalkthrough", acViewNormal, acViewNormal
  18.     End If
  19. ElseIf Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And Not IsNull([cboIssue]) Then
  20.   strCriteria = "Not IsNull([cboCustomer]) And IsNull([SDate]) And IsNull([EDate]) And Not IsNull([cboIssue])"
  21.     If DCount("*", "<Your Table>", strCriteria) <> 0 Then
  22.       DoCmd.OpenQuery "qryWalkthrough", acViewNormal
  23.     End If
  24. ElseIf IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
  25.   strCriteria = "IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue])"
  26.     If DCount("*", "<Your Table>", strCriteria) <> 0 Then
  27.       DoCmd.OpenQuery "qryWalkthrough", acViewNormal
  28.     End If
  29. ElseIf Not IsNull([cboCustomer]) And Not IsNull([SDate]) And Not IsNull([EDate]) And Not IsNull([cboIssue]) Then
  30.   MsgBox "Please remove one search criteria", vbDefaultButton1 = vbOKOnly, "To many Critera fields chosen"
  31.     Exit Sub
  32. End If
  33. End Sub
  34.  
P.S. - The Exit Subs are for illustration purposes only.
Jan 16 '13 #4
SeadooRider
48 New Member
ADezii, It needed the query name instead of the table name, but other than that it works perfect.

Thanks again
Jan 16 '13 #5
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).
Jan 16 '13 #6
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.
Jan 16 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
6921
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...
5
714
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,...
6
9912
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...
2
2057
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...
4
1787
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...
7
19482
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...
16
6229
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???
3
2809
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...
0
7202
marktang
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,...
0
7084
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...
1
6991
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...
0
7458
tracyyun
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...
0
4672
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.