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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Command for showing the result of a query

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
GB
I can use VBA to UPDATE and INSERT entries into table, but what about regular queries? For example, I want the VBA equivilent of creating a new query from the Queries control panel, and running it (so the results appear on the screen).

Here is some simple VBA code that performs a query, but there is no command to show it to the user. What would that command be? I can't find anything useful in the DoCmd object, as OutputTo sends a saved query to a file.


Dim DatabaseConnection As ADODB.Connection
' Set the connection to the DB
Set DatabaseConnection = Application.CurrentProject.Connection
' Get the name of the person to add

' Text between speech marks is copied in as-is, while that outside of it (i.e. & Name & and & Number &)
' has the value extracted from the variables.
' Be sure that single quotes, that must go around strings, are included as-is !
SQL = "SELECT * From MyTable"
DatabaseConnection.Execute SQL
 
If you just want to display the Query Resultset to the Screen, you can use the DoCmd.OpenQuery, but as far as I know this only works with Save Query Objects
 
That's right, only saved queries. So in my on-click event code I'd need to save the query then run it... it seems a bit poor. I think I'll try it though. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top