Solved

Get 'query names' from Access via vbscript

Posted on 2009-04-09
9
557 Views
Last Modified: 2012-05-06
I have some access queries that return supplier records with missing data etc.  

Via vbscript I'd like to get access to the names of all available queries (to put in an array), or do some kind of 'loop through all queries' process (referring to the queries by reference number or something), so I can call each query in turn.  

How can I call queries by number or get a list of their names?

Thanks!
0
Comment
Question by:Nathan08
9 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 300 total points
ID: 24104961
This will give you a list of queries:

SELECT MSysObjects.Name
FROM MSysObjects
WHERE (((MSysObjects.Type)=5))
ORDER BY MSysObjects.Name;

mx
0
 
LVL 13

Expert Comment

by:ioane
ID: 24104970
Hi Nathan08,

You can access the collection of querries in a databse by using:

   Application.CurrentDb.QueryDefs(n)

Can also use:

   Application.CurrentDb.QueryDefs.Count

to get the number of querries in the database etc.
0
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 200 total points
ID: 24105006
To get a list of all queries in database.mdb you can use ...

SELECT [Name]
FROM MSysObjects in "C:\folder\database.mdb"
WHERE [Name] Not Like "~*" AND Type=5
ORDER BY [Name];
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 65

Expert Comment

by:rockiroads
ID: 24105057
Can u confirm, do you want it in vba? or actual proper vbscripting? the latter would require opening a database, then going through the queries using querydefs
0
 

Author Comment

by:Nathan08
ID: 24105425
Hi guys, thanks for the comments.  It's in vbscript- I'm using a tool called 'automate' which supports vbscript as well as executing sql queries like the ones mentioned above.  If something like those queries above do return the list of names it'll be fantastic, I'll be testing those the day after easter.

Thanks for your efforts so far guys!
0
 

Author Comment

by:Nathan08
ID: 24105431
Tramtrak- that looks like VBA- if you were doing it from VBScript would you need to call the application etc first?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24105820
from what I said before you have to open the database. But my mistake was to use tabledefs, not quite, as that is dao and we are creating access objects. Need to use allqueries which is obtained from the object

this example here iterates thru and displays each query name

dim acc
dim i

set acc = createobject("access.application")
acc.opencurrentdatabase "C:\mydb.mdb"

for i = 0 to acc.currentdata.allqueries.count-1
    wscript.echo acc.currentdata.allqueries(i).name
next

acc.closecurrentdatabase


0
 

Author Comment

by:Nathan08
ID: 24112981
Thanks everyone.  I was getting method or property not found for the vbscript but the query based approaches did work like a charm.  Note to those looking this up later: you'll get an error of 'no read permission on msysobjects'.  Fix this by going to options->view in access and showing system objects, then to tools->security->user and group permissions and give read access to admin for the msysobjects table.

Great work guys, not sure what the best method of dividing points is but since I could only get the queries to work and both queries had their own advantages I'll divide them.

thanks!
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24113598
pity that vbscript didnt work for you as it worked fine for me. I tested it before I posted it. Oh well, maybe useful to somebody else then
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question