Use MS Query in VBA to only get a SQL statement
If I understood, the question was about using MSQuery interactively within
code, to help a "non-coding" user create a query from within Excel at runtime.
I agree with Al that Access would actually be a better option, but
implemented in a diferent way. I don't know of any way to manipulate MSQuery
under VBA control, but you have full freedom to do so with Access, assuming
your users will have Access on their machines. You could create an Access
database that has a linked table to your Interbase table. You could then use
Automation to open and run the Access database under VBA control, where you
could bring up a new query design grid or even the query wizard for your
Excel user, then grab that SQL string and close (or at least hide) Access -
then you can use the SQL however you need to.
If you are not familiar with the concept of Automation you will need to
research the help files, this newsgroup, and/or the MSDN library. But I will
give a basic description. To run Access under VBA control, you add a
reference to MS Access to your Excel VBA project, then declare an object
variable to refer to an Access session. You can then use the Access object
model in your Excel VBA project to start up Access (by default will be
invisible to user), manipulate it, make it visible when desired, hide it,
close it, etc... e.g:
Dim AccessApp As Access.Application ' defines the variable to hold an Access
session
Set AccessApp = New Access.Application ' starts the session - hidden
AccessApp.OpenCurrentDatabase MyDBFilePath ' opens an Access file
AccessApp.Visible = True ' shows your Access session to the user
AccessApp.Visible = False ' hides it but keeps it running
You can use VBA code to work with Access just as you would if you were
running it from an Access project as long as you prefix all your properties
and methods with AccessApp. (e.g. AccessApp.DoCmd.OpenQuery "My Query").
When done, quit your Access session, and destroy the object variable:
AccessApp.Quit
Set AccessApp = Nothing
I like your idea of providing users with a built-in interface to design a
query at runtime; with Automation I think you could implement it without
needing too much other than some basic automation knowledge and a few
commands (Start the query design/Make Access visible and pass control to the
user/when user is done read and store the SQL text and close the query/ hide
Access).
"Al" wrote:
The optimal place to get your SQL statements is from MS Access as Access will
allow you to have a where condition on a field not included in the Select
statement. After creating your Query using the Access GUI, you can switch to
SQL view and simply copy and paste the query into your Excel VBA module.
"RB Smissaert" wrote:
I wonder if this were possible:
Use MS Query to construct a SQL statement and use the statement in VBA code
(ADO, ODBC connecting to Interbase), but don't let it run the statement.
So just use the MS Query as a SQL wizard, incorporated in VBA code.
Doing this would save a lot of coding to get a similar wizard, although it
shouldn't be too difficult either.
One drawback I can see is that MS Query won't let you do a WHERE condition
on a field that is not in the SELECT
clause. The good thing though is that it will automatically add any joins,
although I am not sure how it will handle
joins that are not inner joins.
Hope this explains it clear enough and thanks for any advice.
RBS
|