Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

view vba select query

I'm using Access and am learning VBA.  I want do a select query in vba preferably using DoCmd.RunSQL.  But I also want to view the results somehow. Preferably as a basic query table in vba.  I don't mean create a table from a query but just show the query in tabuilar form.
0
vrosas_03
Asked:
vrosas_03
  • 4
  • 3
  • 2
3 Solutions
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:


DoCmd.OpenQuery "YourSelectQueryName"

RunSQL is only for Action queries (Delete, Append, Update, MakeTable)

mx
0
 
vrosas_03Author Commented:
This opens a query created with query designer and doesn't let you alter it.
Private Sub run_query_Click()
DoCmd.OpenQuery "Query1"
End Sub

I want to be able to alter the SQL from VBA
like this:

Private Sub run_query_Click()
DoCmd.OpenQuery "Select * from Table1"
End Sub
0
 
GrahamMandenoCommented:
Create a query and save it.  Then alter its SQL before you open it:
Private Sub run_query_Click()
CurrentDb.QueryDefs("MyDynamicQuery").SQL = "Select * from Table1"
DoCmd.OpenQuery "MyDynamicQuery"
End Sub

Open in new window


Best wishes,
Graham
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Don't you mean:


  CurrentDb.CreateQueryDef "MyDynamicQuery", "Select * from Table1"
  DoCmd.OpenQuery "MyDynamicQuery"

mx
0
 
GrahamMandenoCommented:
No, I meant create and save the QueryDef, then use it repeatedly, changing the SQL as required.

G.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Well, I guess if the OP want so create it in VBA .. and save it ... and reuse it ... there is now a way :-)

mx
0
 
vrosas_03Author Commented:
Thanks a lot.  those solutions will be useful to me in the project I'm working on.     In one you create the table and in the others the table must already exist.  I'm including my own experiments in case anyone might find them useful

'this creates an access query:

Private Sub run_query_Click()
  CurrentDb.CreateQueryDef "MyDynamicQuery", "SELECT * FROM Table1;"
  DoCmd.OpenQuery "MyDynamicQuery"
End Sub


'Alters a query already existing:

Private Sub run_query2_Click()
DoCmd.Close acQuery, "MyDynamicQuery3", acSaveYes
CurrentDb.QueryDefs("MyDynamicQuery3").SQL = "SELECT * FROM Table1;"
DoCmd.OpenQuery "MyDynamicQuery3"
End Sub



'Creates and opens the query this is what I created before I realized you had said to create the table:

Private Sub run_query4_Click()
Dim MyDynamicQuery3 As QueryDef
CurrentDb.CreateQueryDef ("MyDynamicQuery4")
DoCmd.Close acQuery, "MyDynamicQuery4", acSaveYes
CurrentDb.QueryDefs("MyDynamicQuery4").SQL = "SELECT * FROM Table1;"
DoCmd.OpenQuery "MyDynamicQuery4"
End Sub

Open in new window

0
 
vrosas_03Author Commented:
its asking me to explain why i accepted my own solution but I don't recall doing that.
0
 
vrosas_03Author Commented:
can I delete my comments?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now