View Full Version : query using variables


Robert_Halliwell
03-06-2008, 09:29 AM
I am currently making a project as part of my Computing course. The project is a running diary, where users can log in and record their running times(Amongst other things). As part of the system i am going to include a calorie calculator, to do so i have to work out the BMI (Body Mass Index). To do this you have to divide the weight by the square of the height. The weight and height are stored in the tables.
I am planning on creating a form for this conversion which will refer to a query. When the users log-in their ID is stored in a Public Variable called "UserID".
What i would like to do is query the database for their height and weight using this variable, but i don't know how to go about doing this.
Does anyone have any suggestions?
Many Thanks

WayneRyan
03-06-2008, 10:41 AM
Robert,

Access queries can't reference Public Variables directly.

Clunky as it sounds, you're gonna have to use a Public Function
to communicate the value to the query:

In your query for the Criteria put --> =GetUsesrID


Public Function GetUserID () As Long <-- Return the datatype of your Public Variable
GetUserID = UserID
End Function


Wayne

Robert_Halliwell
03-06-2008, 11:46 AM
Thanks Wayne,
I am getting a "Data Type Mismatch in Criteria Expression" message when i run the query. I think it is because RunnerID (The ID i am querying for) is an AutoNumber. Have you got any suggestions on this?

P.S. I forgot to mention i am using Access 2003, if that makes any difference.

Thanks.

WayneRyan
03-06-2008, 04:13 PM
Robert,

Public Function GetUserID () As Long

Long is the datatype for Autonumber.

Wayne

Robert_Halliwell
03-07-2008, 01:27 AM
Wayne,
Attached is a picture with a series of screenshots of the system.
I checked that GetUserID is set to "Long" and it still doesnt work.
Could you check the picture to see if there is anything I have missed

Many Thanks
Robert

Mile-O
03-07-2008, 01:29 AM
Not "GetUserID" - that's a string literal. You need to call the function: GetUserID().

Robert_Halliwell
03-07-2008, 02:57 AM
ah ha! It works thanks a lot to both of you!