VBA Query with Query Criteria

BigNate

Board Regular
Joined
Dec 17, 2014
Messages
242
Hello Everyone,

I'm trying to use VBA to run a query with the query criteria being pulled from the values contained in textboxes on the same form. I know how to run the query with no criteria:
VBA Code:
Private Sub ResultsBtn_Click()
DoCmd.OpenQuery ("Results")
End Sub

And I know the concept of a wildcard (*) in a query, but I'm not sure how to combine these concepts. Using the picture shown below for example, I am trying to click the button that says "Report" called "ResultsBtn" to query all rows in a table. Using the attached image as an example, I want to try to add to my code above so that I would run the "Results" query to show all items that...
  • have 10 in the partNumber somewhere in the input textbox to the right called "PartInput"
  • Has a date between the dates given in the two input textboxes called "FromDate" and "ToDate"
  • (I think I can figure out the other criteria shown in the screenshot if I get assistance on the above two points)
**If any of the boxes on the right are blank, I want to ignore them and only filter based on other criteria.

Thanks in advance to whomever takes the time to help me. I used to know how to do this, but haven't looked at Access/VBA for several years!
 

Attachments

  • AccessForm.PNG
    AccessForm.PNG
    9.1 KB · Views: 14

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
In the query, in the field PartInPut type:
SQL:
Like "*"& [forms]![ResultsBtn]![PartInput] & "* " or [forms]![ResultsBtn]![PartInput] is null

In the query, in the field Date type:
SQL:
>= [forms]![ResultsBtn]![FromData] or [forms]![ResultsBtn]![FromData] is null AND <= [forms]![ResultsBtn]![ToData] or [forms]![ResultsBtn]![ToData]  is null
 
Upvote 0
Thank you very much for your help! For some reason, the date range filter still isn't working. I tried to modify it a bit to troubleshoot myself, but can't see the issue. Here is the line with the issue (note I changed the name of "ResultsBtn" to "HTS"):
SQL:
>=[forms]![HTS]![FromData] Or [forms]![HTS]![FromData] Is Null And <=[forms]![HTS]![ToData] Or [forms]![HTS]![ToData] Is Null

Any ideas in what I'm not seeing?
 
Upvote 0
Grouping is often a good idea when stringing AND, OR, Is Null, etc. Consider
>= A Or A Is Null And B <= C Or C Is Null
Is it
(>= A Or A Is Null) And B <= C Or C Is Null
>= A Or (A Is Null And B <= C) Or C Is Null
(>= A Or A Is Null) And (B <= C) Or C Is Null
(>= A Or A Is Null) And (B <= C Or C Is Null)
or who knows what? There are rules about the order used if not specified and all I can remember about them is they're quite complicated.

Also, this has no qualifier
And <=[forms]![HTS]![ToData] <<- what is <= this?
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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