Passing Variable from Form to Query using Click on Label Caption (1 Viewer)

Local time
Today, 13:53
Joined
Sep 2, 2022
Messages
5
Hello,

I have a Form (Page_Scan_Index) with many Labels (ex. Label1, Label2, etc.).

I have a Query (Stamps_on_Page) with 2 fields (Front_Page_Scan_Name and Scott_Number)

I want to CLICK on a Label in the Form and Pass the Label Caption to the Query as a variable.

I created this code:

Private Sub Label1_Click()
Dim pagename As String
pagename = [Forms]![Page_Scan_Index]![Label19].[Caption] + ".jpg"
DoCmd.OpenQuery ("Stamps_on_Page")
End Sub

So, the variable I’m trying to pass to the Query is “pagename”.

pagename is populating perfectly but the query never gets the string.

Within the Query, I am setting the Variable name as the criteria for Front_Page_Scan_Name like this:

[pagename]

I’m assuming I don’t know the correct syntax to pass a variable to the query?

Can someone please help?

Thanks,
Scott
 
Solution
SELECT Stamp_Collection.Scott_Number, Stamp_Collection.Front_Page_Scan_Name
FROM Stamp_Collection
WHERE (((Stamp_Collection.Front_Page_Scan_Name)=[pagename]))
ORDER BY Stamp_Collection.Sequence_Number;
Okay, that is a select query. Which brings up my other question, why open it and not just use a form to view it? Just curious...

theDBguy

I’m here to help
Staff member
Local time
Today, 10:53
Joined
Oct 29, 2018
Messages
21,474
Hi. Welcome to AWF!

Just wondering why you're trying to open the query? Is it an action query? If it's a select query, why not use a form to view the result?
 
Local time
Today, 13:53
Joined
Sep 2, 2022
Messages
5
Hello theDBguy,

Nice to be here...Thanks.
I'm a "Hobby" coder and use pretty basic layouts.
I am wide open to any suggestions you have.
The key here is that there are actually about 100 Labels...
Clicking on each one will result in a different query result.
This way seemed like it would be easy if I could just pass the variable "pagename" to the query.
I'm so close...Query just won't accept the variable.

Thanks,
Scott
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:53
Joined
Oct 29, 2018
Messages
21,474
Hello theDBguy,

Nice to be here...Thanks.
I'm a "Hobby" coder and use pretty basic layouts.
I am wide open to any suggestions you have.
The key here is that there are actually about 100 Labels...
Clicking on each one will result in a different query result.
This way seemed like it would be easy if I could just pass the variable "pagename" to the query.
I'm so close...Query just won't accept the variable.

Thanks,
Scott
Just for clarification, can you please post the SQL statement of the query in question? Thanks.
 
Local time
Today, 13:53
Joined
Sep 2, 2022
Messages
5
SELECT Stamp_Collection.Scott_Number, Stamp_Collection.Front_Page_Scan_Name
FROM Stamp_Collection
WHERE (((Stamp_Collection.Front_Page_Scan_Name)=[pagename]))
ORDER BY Stamp_Collection.Sequence_Number;
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:53
Joined
Feb 19, 2013
Messages
16,614
use a tempvar - something like

tempvars!pagename=[Forms]![Page_Scan_Index]![Label19].[Caption] + ".jpg"

and in your query

WHERE (((Stamp_Collection.Front_Page_Scan_Name)=tempvars!pagename))
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:53
Joined
Oct 29, 2018
Messages
21,474
SELECT Stamp_Collection.Scott_Number, Stamp_Collection.Front_Page_Scan_Name
FROM Stamp_Collection
WHERE (((Stamp_Collection.Front_Page_Scan_Name)=[pagename]))
ORDER BY Stamp_Collection.Sequence_Number;
Okay, that is a select query. Which brings up my other question, why open it and not just use a form to view it? Just curious...
 
Solution
Local time
Today, 13:53
Joined
Sep 2, 2022
Messages
5
First off...It worked!
You are magnificent!
Thank You very much.
I never knew there were tempvars...Looks like a new tool in my holster.

Second, To answer your question...I am probably going to do that next.
My simple mind needs to do things in baby steps...It helps me in debugging when things aren't too complicated.

Anyway, if I'm ever in your area, I'll wash your car for you!

Again, thank you very much...I would have never found the tempvar solution.

Scott
 

MsAccessNL

Member
Local time
Today, 19:53
Joined
Aug 27, 2022
Messages
184
if you are going to use the pagename for your tempvars, you probaly can use:
Tempvars!pagename = Label19.Caption & ".jpg"
or just
..pagename= "xxxx.jpg"
or put the name in the tag property of the label:
...pagename = Label19.Tag
or replace the labels with buttons now you can use:
...pagename = screen.activecontrol.caption &. "jpg"
copy past it to all button events or make it a function
or you can keep the Label and put a transparent Button with transparent outline over the Label, now you also can set a mouse hover hand.....
Just a few tips...
 

Users who are viewing this thread

Top Bottom