Solved

Get 'query names' from Access via vbscript

Posted on 2009-04-09
9
571 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 - Microsoft MVP, Access and Data Platform) 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
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…

713 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