Storing SQL Queries to a table

RobWulf

Registered User.
Local time
Yesterday, 22:38
Joined
Apr 4, 2013
Messages
11
[Solved] Storing SQL Queries to a table

I can't seem to find it and this has to be the last bit of stuff I need to get my Database up and running for my boss using an EXE front end.

What I am trying to do is something similar to what I did for my reports

I have a table with the following fields

ID = Autonumber
Discrip = Text (Discription of the Report)

The combo box on the On Change event has this code

Private Sub ReportCombo_AfterUpdate()
DoCmd.OpenReport ReportCombo, acViewPreview
End Sub

Can I set something like this for SQL commands?
 
Last edited:
Code:
Private Sub QueryCombo_AfterUpdate()
    DoCmd.OpenQuery Me.QueryCombo
End Sub

However users should not directly view queries.

This alternative can be used for action queries to avoid warnings:
Code:
Private Sub QueryCombo_AfterUpdate()
    CurrentDb.Execute Me.QueryCombo, dbFailOnError
End Sub

(Me is a reference to the current object and should be included when referring to controls on the form.)
 
If you would prefer to store the actual SQL text in the table then put this field in a hidden column of the combo and refer to that column in the Execute command.
 
I just don't understand what I am doing wrong.

I have a test table set up with 3 columns An ID colum with Auto number, a column for the Description and one for the SQL


I set up a form with the field set to Text for the SQL command and put SELECT * FROM Customers in that field, but when I run the form with the drop down it gives me an error saying it can't find the table for the querry
 
Execute only works on action queries (eg INSERT, UPDATE, DELETE).

To open a SELECT from code you would need to write the SQL text to the SQL Property of a QueryDef and then OpenQuery on that.
 
or docmd.runsql myCombo.column(2) - based on your dropdown having three columns
 

Users who are viewing this thread

Back
Top Bottom