AllExperts > Using MS Access 
Search      
Using MS Access
Volunteer
Answers to thousands of questions
 Home · More Using MS Access Questions · Answer Library  · Encyclopedia ·
More Using MS Access Answers
Question Library

Ask a question about Using MS Access
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Scottgem
Expertise
I can answer almost all types of questions relating to Microsoft Access usage and application design. My strengths are database and interface design.

Experience
I've been designing databases for over 15 years working with dBase, FoxPro, Approach and Access.

Organizations
Author of Microsoft Office Access 2007 VBA
Techncial Editor for Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports & Queries From Que Publishing

 
   

You are here:  Experts > Computing/Technology > Business Software > Using MS Access > Vba run a query syntax required

Using MS Access - Vba run a query syntax required


Expert: Scottgem - 6/26/2009

Question
I have written a query which when opened returns all the table names in an mdb.file. This works fine. I called it QYForTables. It has the path and file name to the mdb file hard coded.

SELECT MSysObjects.Name
FROM MSysObjects IN 'C:\documents and settings\alan\desktop\bcodesample.mdb'
WHERE (((Left([Name],4))<>"MSys") AND ((MSysObjects.Type)=1) AND ((Left([Name],1))<>"~"))
ORDER BY MSysObjects.Name;

Now I want to run it from an event. So I need to build a string to do a Docmd.RunQuery "SQLStr". However it seems that this command cant deal with a "Select" SQL. The primary reason for wanting to do this is to make the path and file name programmable (from a textbox entry) to get a list of tables in the target mdb file (Which is not the current db) into a combobox.

Help would be appreciated

I flatter myself that I am quite good at VBA but this defeats me.

Answer
First, when using embedded SQL you have to buid the SQL statement using concatenation. I'm not sure whether you have done that or not. So if you are entering the path to the MDB file you would do something like this:

strSQL = "SELECT MSysObjects.Name "
strSQL = strSQL & "FROM MSysObjects IN '" & Me.filepath & "' "

Second, If the end result is to populate a combobox on that form, after you have generated the correct SQL state, just use:

Me.comboname.RowSource = strSQL

Hope this helps,
Scott<>
Microsoft Access MVP 2007
Author: Microsoft Office Access 2007 VBA  

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.