Command Button - using Queries

L

learning_codes

Hi,

I have about 40 Queries by Year. I don't want to write it down one
by one to make a command button.

DoCmd.OpenQuery "2007", acViewNormal, acEdit
DoCmd.OpenQuery "20071", acViewNormal, acEdit
DoCmd.OpenQuery "20072", acViewNormal, acEdit

I'm tired if I make the change then I have to go back the command
button to adjust the name of the queries.

Is there a way for create a code for "Like 2007*" or Like 2006* or
Like 2005* or Like 2004*, etc. in one command button.

DoCmd.OpenQuery "2007", acViewNormal, acEdit
DoCmd.OpenQuery "2006", acViewNormal, acEdit
DoCmd.OpenQuery "2005", acViewNormal, acEdit
DoCmd.OpenQuery "2004", acViewNormal, acEdit

Your help would be much appreciated.

Thanks
 
D

Douglas J. Steele

No, there's no way to have the OpenQuery command do that, but you could easy
run a loop in VBA to go through all the queries and only run those that
match a certain criteria:

Dim dbCurr As DAO.Database
Dim qdfCurr As DAO.QueryDef

Set dbCurr = CurrentDb()
For Each qdfCurr In dbCurr.QueryDefs
If Left$(qdfCurr.Name, 4) = "2007" Then
DoCmd.OpenQuery qdfCurr.Name, acViewNormal, acEdit
End If
Next qdfCurr

For the life of me, though, I don't understand why you'd want to do that:
queries aren't intended to be interacted with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top