I want to use a VBA-variable as a criteria in a query.
Now I read a field from a form (form!form1.field1), and set the value of the field from VBA (me!field1=12 e.g.), but that’s slow, I guess, and a bit clumsy I think.
Can anyone help?
rettnuc.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » VBA variables in queries (2000)
I don’t think there is an easier way. Standard SQL doesn’t know about VBA, forms etc. Microsoft extended SQL in Access to recognize VBA functions and references to controls on forms or reports. Variables – not.
Maybe, if you explain why you want to refer to a variable (and not to a control or a function), somebody will come up with a bright idea.
Parameterquery is not an option because of the reason I want to use that variable in a query:
I use a combobox on a form to select a writer. On that form I have different buttons which open different forms which show information about that writer. Using subforms has come into my mind, but is not what I want.
Therefore I use different queries, having one item in common: writerID. Catching that ID from the main-form is slow, so I want to set a public variable and use that as a criterium in the different queries for the forms.
Q: IS there anyone having bright ideas? Otherwise I’ll stick with the GetVariable function HansV and me mentioned before.
Have you considered using the WhereCondition argument of the OpenForm method?
It would work more or less like this:
Base your information forms on queries without criteria. The queries should contain WriterID, however.
The OnClick handler of the command buttons on your main form could look like this:
Private Sub cmdMyButton_Click()
DoCmd.OpenForm FormName:=”frmDetailInfo”, WhereCondition:=”WriterID=” & [cboWriter]
End Sub
where cboWriter is the name of the combo box used to select the writer. I assumed that WriterID is numeric; if it’s text, replace the last part of the instruction by
WhereCondition:="WriterID='" & [cboWriter] & "'"
You could also open the forms using the OpenArgs argument to pass the writerID. There is NO way to directly reference variables in a query. You can’t even use built-in variables in a query, only their numeric equivalents. So either use Hans’s functional approach or make it easy on yourself and use either the WhereCondition or OpenArgs to open the forms in the first place.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications