How do I run a query from a VBA editor window in Access?

I would like to do something like this:

DoCmd.OpenQuery "select * from some_table;"

      

How can I do it?

Please keep in mind, I do not want to create a new query, save it and run it. I just want to inject a select statement into VBA code and run it.

+2


a source to share


2 answers


If you just want to get the maximum value, DMax-Function should do the trick:

myVariable = DMax("fee", "courses", "region = 'UK'")

      



(matches SELECT MAX(fee) FROM courses WHERE region = 'UK'

).

+4


a source


You can run a query to retrieve a single value with the following:

Set rst = CurrentDB.OpenRecordset("Select Max(myCol) FROM myTable")
myValue = rst(0).Value

      



Which you can shorten to one line like this:

lngValue = CurrentDB.OpenRecordset("Select Max(myCol) FROM myTable")(0)

      

+1


a source







All Articles