Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining a labels name when dblclicked 6

Status
Not open for further replies.

jpl458

Technical User
Sep 30, 2009
337
US
Have a form that has 40 entries of a label and a text box with each label. When a label is double clicked in need to get the name of the lable in vba. I have tried this:

Private Sub Bay01_DblClick(Cancel As Integer)
EventSwitch = True
Dim fromCtl As String
fromCtl = Me.ActiveControl.Name

DoCmd.OpenForm "BaysFilled", , , , , , OpenArgs = fromCtl 'Pass name to dialogue box

EventSwitch = False
End Sub


This gives me the name of the textbox, not the name of the label, which is what I need to write the code in the dialogue box..
The name is text and is passed as text via OpenArgs.
EventSwich is a Boolian variable in a standard module.

Thanks in advance



jpl

 
Whit MS Access questions, your opportunity to get better responses will be in...
Forum702
Forum705

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Is the label named Bay01 ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Assuming the lables are linked to the textbox. Then the textbox has a controls collection. Control(0) is the label

Public Function getLableName()
Dim lblName As String
lblName = Me.ActiveControl.Controls(0).Name
DoCmd.OpenForm "BaysFilled", , , , , , lblName
EventSwitch = False
End Function

Now you can write a single function and not 40 event procedures. Hilite all 40 textboxes, and in the double click event property type
=getLableName()

This only works for a function and not a procedure. No idea why.
 
No idea why
Because a sub don't return any value ...
 
Because a sub don't return any value
Yes, but can you explain why that would be important? You cannot reference the returned value as far as I know, and I could not see much utility even if you could. Far more common is the desire to have a common event procedure, as I have demonstrated. Since access does not have a control array. So you can write a single event and fire it with as many controls as you would want.

So I have 40 controls that when dblclicked open a form, based on a single function and not 40 event procedures. Simply by putting "=openForm()" in the property of the double click event.

Public Function OpenForm()
Dim lblName As String
lblName = Me.ActiveControl.Controls(0).Name
DoCmd.OpenForm "BaysFilled", , , , , , lblName
EventSwitch = False
End Function

so it simply returns a default null value into the unknown. But this would not work simply by making it a sub.

Public sub OpenForm()
Dim lblName As String
lblName = Me.ActiveControl.Controls(0).Name
DoCmd.OpenForm "BaysFilled", , , , , , lblName
EventSwitch = False
End Function

So why require a function if you cannot even use the returned value?
 
why that would be important
I guess due the [!]=[/!]
 
MajP,
Functions can return values if you code them to.

Code:
Public Function Add2(byval num1 as double, byval num2 as double) as double
    Add2 = num1 + num2
End Function

Notice the function name is assigned a value. This is the value the function will return.

The function can then return its value to a variable like this:
Code:
myVar = Add2(2.71, 3.14)

In your example functions above, no return value was assigned to the function.
 
PHV The labels are all named BayNN or Bay01, Bay02........Bay40. I sort of thought that if you clicked the label it would become the ActiveControl, but I guess not. The Labels are not linked to the text boxes.


jpl

 
Then simply make the lables textboxes.
Control Source: ="Your Caption"
disable or lock them.
 
Labels in a Access cannot be the Active control because you cannot set focus to them.
 
So, replace this:
fromCtl = Me.ActiveControl.Name
with this:
fromCtl = "Bay01"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Or do what I suggest and write a single function instead of 40 event procedures. A lot easier to write and to error check.
 
Thanks to all. I have to relink the labels to the text boxes using cut and paste. I prefer the function approach. But thanks to all for the conversation. I learned a lot, and always do in here.

Thanks again

jpl
 
To All . . .
MajP said:
[blue]Labels in a Access cannot be the Active control because [purple]you cannot set focus to them[/purple].[/blue]
The above is correct and because of this [purple]a label can never be the active control[/purple]. My point being that [blue]Me.ActiveControl.Name[/blue] is simply returning the current control with the focus (which is not the label). Also note that attached labels have no events.

Just thought I'd point this out . . .

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Aceman, I have attached all of the labels to their corresponding text box in one version of the ap. In design mode they have 5 events e.g. Click, Dblclick, Mouseup etc. I have coded this version with MajP's suggestion which is

Assuming the lables are linked to the textbox. Then the textbox has a controls collection. Control(0) is the label

Public Function getLableName()
Dim lblName As String
lblName = Me.ActiveControl.Controls(0).Name
DoCmd.OpenForm "BaysFilled", , , , , , lblName
EventSwitch = False
End Function

Now you can write a single function and not 40 event procedures. Hilite all 40 textboxes, and in the double click event property type
=getLableName()

I also coded another version using PHV's method. (It was good practice), but can't run either until Monday because of network issues. I am certain PHV's way will work, but MajP's version is, well, a bit more elegant; and if it works then I have learned something new.

Thanks

jpl
 
Learning something new, is nothing new @Tek-Tips!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
What I found so far. When I got on the network this am I played with both versions. What I found is that Aceman1 is correct. In the version that has the labels attached to the text boxes there are no events, zero, zip etc. Thus I could not dblclick the label and get any response from the code, and there were no events presented in design mode for the labels (I must have been mistaken last night and looking at the wrong version). PHV's version all accept the double click because the labels are not attached to the text boxes.

Now, on to the next iteratioon.

But theanks to all, really learned a lot, and I'm sure I will be back.

jpl
 
I know that you know what you want to do and what needs to happen.

My question is: how intuitive is it for the User to know to (double) click on a label for something to happen? (Unless you have a little message somewhere directing them to do so…)

Just an observation.


Have fun.

---- Andy
 
Clicking on an label associated to a textbox actually triggers the on click event of the associated textbox. The associated label does not expose any events. The active control would then be the associated text box.

Clicking on a non-associated label triggers the label's on click event. The label does expose events, but like all labels will not take focus, and like all labels cannot be the active control. However, you can always hard wire the event procedure. In this case the active control will be whatever the active control was prior to clicking the label.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top