Return the name of open query

ilcaa

Well-known Member
Joined
May 25, 2005
Messages
751
Office Version
  1. 365
Platform
  1. Windows
i am working in Excel VBA i need to return the name of the current open query... what is the proper syntax?
using this example, how would i return "This_Query"

Code:
Dim CN as New ADODB.Connection
CM.Open ConnectionString

This_Query = "select....."

Dim rs As New ADODB.RecordSet
rs.Open This_Query, CN

-- process record set --
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You didnt open a query, this is an SQL statement.
If you want an actual query name in the access db, then it is in the list of db.querydefs.

If you just want to return "This_Query", you can change the routine to a function and return the name.
or just msgbox
"This_Query"

 
Upvote 0
the above is an excel Function (i left alot of the code out but kept the connection part). i want to shorten the code to process the records so id like to call 2 queries within same function but have a statement that processes the information depending on query name... If Query.Name =" queryname" then process records like this...

I assume rs.Open <query name> is the name of the query, and i was hoping to reference this name within my vba code.. or can you even reference a query name
 
Upvote 0
What I mean is, if the query actually exists in the database as a querydef, then you must ask for it by name, "qsMyQuery".
but since you already know the name, where do you want the name to go?
 
Upvote 0

Forum statistics

Threads
1,221,860
Messages
6,162,479
Members
451,769
Latest member
adyapratama

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