Solved

Get 'query names' from Access via vbscript

Posted on 2009-04-09
9
535 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now