By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,506 Members | 2,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,506 IT Pros & Developers. It's quick & easy.

Passing variable value from code to query criteria?

P: n/a
How can I pass a variable (Dim Property As String) to the query
criteria? I figure I could do it the long way and send it to a form and
have the query access it from there but I figure there has gotta be an
easier way?

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You can't do it directly, but you can specify a function as a query
criterion.
SELECT...
FROM ...
WHERE Table1.Field1=MyFunction(arg1,arg2...)

Nov 13 '05 #2

P: n/a
Sounds good but unfortunatly I haven't used them before. Could you just
expand a little on how to use it. I think I can get it to call the
function from the query but not sure how to set it up to pick up the
owner code from my code. Currently have a public variable
"PropertyCode", which I then use in my function....

Function GetPropertyCode() As String

GetPropertyCode = PropertyCode

End Function

Public PropertyCode As String

"PropertyCode" is set in my sub routine before I call the query... what
else do I need to change...

Thanxs....

Nov 13 '05 #3

P: n/a
Okay, so you add GetPropertyCode to your query as a criterion or as an
expression...

SELECT ... GetPropertyCode() AS Whatever
FROM SomeTable
WHERE...

Nov 13 '05 #4

P: n/a
Don't quite understand that last bit.... how can I select it from a
table when it's from code?? The current code I have is:

SELECT Herd_Animals.Owner, Herd_Animals.Animal_Tag, Herd_Animals.Grade,
Herd_Animals.Sex_Code, Herd_Animals.Date_Of_Birth,
Herd_Animals.Group2_Code INTO QTable1
FROM Herd_Animals
WHERE (((Herd_Animals.Owner)=GetPropertyCode()) AND
((Herd_Animals.Date_Of_Birth)>=[Forms]![Form1]![TxtStartDate] And
(Herd_Animals.Date_Of_Birth)<=[Forms]![Form1]![TxtEndDate]));

Again, I haven't played around with SQL much before. I realise this is
fairly basic...

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.