Set your Query Criteria using VBA

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
ACCESS QUESTION

Can you set your queries criteria using VBA (Initiated by a Button Click event)?

I am trying to figure a way to query by a date range the user selects or show all records if the user does not select dates.

(I don't want to default the dates)
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Use a form.
the form has 2 boxes, txtStart and txtEnd.
2 queries; 1 for all, 1 for the range.

clicking the Find button opens one...:
Code:
sub btnFind_click()
If IsNull(txtStart) then
    Docmd.openquery "qsAllDates
else
    Docmd.openquery "qsDateRng
end if
end sub

the query for qsDateRng looks at the form to get the range....
select * from table where [dateFld] between forms!myForm!txtStart and forms!myForm!txtEnd
 
Last edited:
Upvote 0
(I don't want to default the dates)

If you show all records you have the same result as including all records from the earliest date to the latest date (plus all records with null dates). So you probably could achieve this with default dates after all.
 
Upvote 0

Forum statistics

Threads
1,221,687
Messages
6,161,287
Members
451,695
Latest member
Doug Mize 1024

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