Change Data in a Query Criteria Field using a Macro

CJBills

New Member
Joined
Apr 27, 2015
Messages
13
In Access 2007, I would like to write a macro to change data in one Criteria field of a Query.

In my example, the Query is called "Query", the Field is called "ID", and the Criteria is numbered "100".
(In SQL "([TABLE NAME].[ID])=100").

I'd like to change the Criteria using a macro to "50".

Thanks, in advance.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
In Access 2007, I would like to write a macro to change data in one Criteria field of a Query.

In my example, the Query is called "Query", the Field is called "ID", and the Criteria is numbered "100".
(In SQL "([TABLE NAME].[ID])=100").

I'd like to change the Criteria using a macro to "50".

Thanks, in advance.

When you say Macro, I'm assuming you mean VBA because I don't think you can do so via Macro. Theoretically you could just prompt the user to input the number for the criteria in the query like this.

Code:
 SELECT * FROM TABLENAME WHERE ID =[ENTER ID]

But in any case, the way to do it with VBA is to change the query def

So say you have your query called "Query" and the SQL is literally this
Code:
 SELECT * FROM TABLENAME WHERE ID = 100

You could put this in a module and then run it

Code:
Sub ChangeQueryDef()'Change the definition of a Query
Dim QD As QueryDef


Set QD = CurrentDb.QueryDefs("Query")
QD.SQL = "SELECT * FROM TABLENAME WHERE ID = 50"


End Sub

This would change the WHERE ID = 100 to 50, but you could've just changed it in the query itself or put the parameter like the original example I posted to make your life easier if it were changing constantly.
 
Upvote 0
Indeed, VBA is what I meant. Thank you very much for your reply and direction. This comment is long, but additional advice and guidance would be gratefully appreciated; this issue has been plaguing me.

Following the directions, my results were unsuccessful.

1) I would need to retain End User error, and would not be able to do the first suggestion:

Code:
 SELECT * FROM TABLENAME WHERE ID =[ENTER ID]

2) The second suggestion, scripting VBA to change the query def, either ran without changing the intended criteria (no criteria changed), or resulted in an error.

I inputted the following in a module, then ran it:

Code:
'Change the definition of a Query
Sub ChangeQueryDef()
Dim QD As QueryDef

Set QD = CurrentDb.QueryDefs("Query")
QD.SQL = "SELECT * FROM TABLENAME WHERE ID = 50"

End Sub

ERROR ONE / ATTEMPTED SOLUTION ONE:
Using “*” did not work (Run-time error ‘ 3265’: Item not found in this collection), so I omitted it; I have not been able to input it effectively, if you have suggestions or explanations, please let me know.

ERROR TWO:
The content intended to change is at the bottom of the query’s SQL script, which includes 50+ query fields. The result of the module did not change the intended field criteria; nothing changed, in fact.

ERROR THREE:
The module cleared all the query’s content, then replaced it with “SELECT FROM TABLENAME WHERE ID = 50", where TABLENAME and ID are unique to my example.

ERROR FOUR / ATTEMPTED SOLUTION TWO:
To remedy the issue of the VBA script replacing everything or changing nothing, I inputted the entire SQL script. Each time I inputted the entire Query SQL Script the VBE lines ran out of room, causing red-text because of line length limitations. To fix this I used quote-comma-underscore combinations (ie “CONTENT", _) to extend the line, as can be done in Excel; this did not work.

ATTEMPTED SOLUTION THREE:
I thought that maybe adding another variable would work, but it has not:

Code:
Dim ChangeUserID As String
ChangeUserID = Replace("[TABLENAME].[USER ID]=100", "100", "50")
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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