Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display a temporary query

Status
Not open for further replies.

PrincipalFuller

Programmer
Mar 21, 2012
4
US
Is it possible to display a query without saving the query?

I would like to have a table with three fields: Query Name, Category, SQL

And then in VB I want to grab the SQL from the table, run the query, and display the results... without needing to save each and every query that will be available.

Is this possible?

Alternatively, if I use code such as this:
strSQL = "SELECT Gender, PPAID FROM dbo_Ppl_Student;"
Set qdf = CurrentDb.CreateQueryDef("NewQuery", strSQL)
DoCmd.OpenQuery qdf.Name

Is there a way to check to see if the query already exists before it creates the "NewQuery" ?

(Use Case: I'm making an Access DB which will be stored locally on each users hard drive, connected to a cloud based SQL server, and if I add a new query, I don't want to re-update each users local database file).

Thanks!

 
I did a little work on this idea in the past. Here are some notes.

Code:
    Set db = CurrentDb

    strSQL = Me.txtSQLText & " " & Me.txtSQLWhere & " " & Me.txtSQLOrder
    If Trim(strSQL) = vbNullString Then strSQL = "SELECT * FROM TableName"
 
''I know I do not have any objects other than queries 
''with this name in this DB, if you are likely to have a
''situation where a table could have the same name, you will
''need to check the type. 
    If DCount("*", "MSysObjects", "Name='tmpViewQuery'") = 1 Then
        db.QueryDefs("tmpViewQuery").SQL = strSQL
    Else
        CurrentDb.CreateQueryDef "tmpViewQuery", strSQL
    End If
    
    DoCmd.OpenQuery "tmpViewQuery", acViewDesign
    DoCmd.RunCommand acCmdSQLView


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top