VB to Update Query(s) without opening up each query

santa12345

Board Regular
Joined
Dec 2, 2020
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hello.
I am trying to automate the following.
I need to update Query 1, 2, 3, and 4 with a dealership ID #.
So instead of opening up each query and entering the dealership ID #, I was wondering if there is an easier way of automating this.
I used to use [Forms]![Form1]![dealerno] but this is not working anymore within (querying a Azure DB). Forms would call another table where I had that dealer # entered.
Any suggestions would be much appreciated. Thank you.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
There are a few ways you can go about this.

1. Create a "one-record" table where you store the dealership ID that you want, and include this table in your 4 queries (link to this table). That will automatically filter your 4 queries for that specific dealership ID. So any time you wanted to change it, you just change that one record in your table.

2. Use VBA to automatically rewrite the SQL code of your queries to add the filter your queries by that ID. Sometimes, if the queries are a bit long or complex, I will create a little "cheat". If will create a query that returns all my desired fields and does all my calculations, but does not have any dealership ID criteria. Then save it with a name like "subQuery1". Then create another query named "Query1". It doesn't really matter what you put in this query.

Then in my VBA code, I update the SQL code of Query1 to something to the effect of:
VBA Code:
SELECT subQuery1.*
FROM subQuery1
WHERE DealershipID = '...'

So there are a few ways to accomplish what you need.
 
Upvote 0
Hi Joe.
Thank you.
#1 doesn't work. I've tried that, but the query runs forever, never finishes. Not sure if its something to do with linking to an Azure DB?? :(
I can try #2 this morning.
 
Upvote 0
I suggest you post the query sql to have your forms reference checked. If it works when you open it from the nav pane (when the form is open of course), it should work if you run it from code.
When you tried the first suggestion, did you remember to link the id fields from the 2 tables?
 
Upvote 0
When you tried the first suggestion, did you remember to link the id fields from the 2 tables?
Good point.

If the data is remotely stored, I wonder if a Pass-Through query may help with performance, as opposed to bringing everything back, and then querying on the Access side.
(I have never worked directly with Azure DB's, but I am assuming that you can do a Pass-Through Query from Access to Azure).
 
Upvote 0
I would have just referred to a TempVar for the criteria and set that from the form if not an Azure DB.
 
Upvote 0

Forum statistics

Threads
1,224,097
Messages
6,176,331
Members
452,721
Latest member
Du Toit

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