Running query in VBA with where condition (1 Viewer)

path479

Registered User.
Local time
Today, 23:02
Joined
Jan 4, 2016
Messages
22
I have a query set up in Access without any criteria or parameters set.

In VBA, I can use DoCmd.OpenQuery to run it. However it doesn't have the parameter to run it with where condition. Whereas DoCmd.OpenReport you can

Is there any way to run the query in VBA with where condition? Or is there anyway to get around it without changing the original query?
 

Ranman256

Well-known member
Local time
Today, 09:02
Joined
Apr 9, 2015
Messages
4,337
docmd.openquery "myQuery",,,"where condition"
 

Minty

AWF VIP
Local time
Today, 14:02
Joined
Jul 26, 2013
Messages
10,371
docmd.openquery "myQuery",,,"where condition"

I don't think this is correct. There is no Where clause when opening a query.

You can amend or create a query definition in VBA and then open it or use it as a recordset / recordsource.
 

Ranman256

Well-known member
Local time
Today, 09:02
Joined
Apr 9, 2015
Messages
4,337
oops , sorry, that was openForm
for form filters you have a query showing all recs on the form
usually the form has controls to filter what you want
then apply the filter using me.filter
me.filter = "[country]='" & cboCountry & "'"
me.filterOn = true
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:02
Joined
May 7, 2009
Messages
19,242
docmd.openquery "myQuery"
screen.activedatasheet.filter = "id = 3" 'put your filter here
screen.activedatasheet.filteron = True 'activate the filter
 

path479

Registered User.
Local time
Today, 23:02
Joined
Jan 4, 2016
Messages
22
docmd.openquery "myQuery"
screen.activedatasheet.filter = "id = 3" 'put your filter here
screen.activedatasheet.filteron = True 'activate the filter

Hi arnelgp

Thank you so much. This does exactly filter what I need. However it is my fault I didn't explain very well what I am trying to do.

We want to have a button on a form which when click would:
- trigger the VBA code to run the query, (with the where condition or filter the records)
- export the filtered records into an excel file.

We prefer not to have these filtered records shown on screen, and certainly not in edit mode.

Would you have any thoughts on how we could achieve this?

Thank you!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:02
Joined
May 7, 2009
Messages
19,242
it would be better to create a query with a form parameter that will filter your records.
therefore when exporting it to excel will not be difficult.

to export to excel:

docmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel12Xml,"query1","z:\zxy.xlsx",true
 

curtyrut

Registered User.
Local time
Today, 09:02
Joined
Dec 23, 2015
Messages
23
Hello, can you help me resolve my problem? I created a form with a command button that will open a query. However, my objective is to open the query then filter based on the end user group. For example, I have the follow code:

DoCmd.OpenQuery "HrOwner", acViewNormal, acReadOnly
DoCmd.SetFilter wherecondition:=[Hiring Owner] = HrGroup

HrGoup is a variable that changes based on the end user setup.

I continue to receive a run-time error 438, which states "Object doesn't support this property or method.

I tried many of different ways to apply filter and to no avail was not successful. Your help is greatly appreciated.
 

sxschech

Registered User.
Local time
Today, 06:02
Joined
Mar 2, 2010
Messages
793
They may have been talking about putting the parameter in the query and then getting the value from the form rather than with a filter.

In your query in the criteria box you would put something like:

Code:
Field: [Hiring Owner]
Table:
Sort:
Show:
Criteria:[COLOR=Blue][Forms]![YourFormName].[YourParameterTextBoxComboBoxEtc][/COLOR]
Then when the query is run, it will get the criteria from the form.
 

curtyrut

Registered User.
Local time
Today, 09:02
Joined
Dec 23, 2015
Messages
23
Hello sxschech, thank you for the speedy reply and your remedy work! Once again thank you for helping me resolve my problem.
 

Users who are viewing this thread

Top Bottom