Reference to the sub name

  • Thread starter Thread starter Lars Brownie
  • Start date Start date
L

Lars Brownie

I have the following code:

Private Sub txt6_Click()
sChoose ("6")
End Sub

Is it possible to make it more generic so that the sub sChoose can
automatically pick the "6" from txt6_Click ?

Thanks, Lars
 
Thanks, but it's not exactly what I meant. In the click_event of txt6 I want
to grab the last character of txt6: the 6, so I can use the code generic in
the other textboxes.

Lars
 
It's not really clear what it is you're looking for. To get the last
character of whatever's in txt6, you'd use

Private Sub txt6_Click()
sChoose Right(Me.txt6, 1)
End Sub

To get the last character of whatever text box is active, you'd use

If TypeOf Screen.ActiveControl Is Textbox Then
sChoose Right(Screen.ActiveControl & vbNullString, 1)
End If

If you've clicked on a command button and want the last character of
whatever text box was active before you clicked on the command button, you'd
use

If TypeOf Screen.PreviousControl Is Textbox Then
sChoose Right(Previous.ActiveControl & vbNullString, 1)
End If

The reason for appending vbNullString to the control is to ensure that Null
isn't returned (in case sChoose is only expecting a string)
 
Txt6 is a label. I have a little form with 8 of these labels. User can click
one of the labels, after which the chosen number is set in another form's
field and the little form closes. Each of the 8 label's click events has the
same code:

sChoose(<<the number of the respective label>>)

The sub sChoose puts the respective number in the other form and closes it.
I made this sub to reduce the amount of code lines.

In stead of having to set the respective number for each event I was
wondering if the name of the sub can be extracted somehow so that the code
for every label is identical.

So in short the question is: Is it possible, in a sub, to grab the name of
that same sub?

Lars
 
Sounds as though you need to use Screen.ActiveControl, as I suggested.

Change sChoose into a function if it's currently a sub (even if it doesn't
return any value). Don't pass anything to it. You can then use:

Function sChoose()
Dim strValue As String

strValue = Right(Screen.ActiveControl.Name)

' Continue with the rest of your code, using strValue
' wherever you need the number of the label

End Function

Now, set the OnClick event for each of the controls to =sChoose() instead of
[Event Procedure] (you must include the equal sign and parentheses).

Note that you can change the value to =sChoose() for all eight labels at
once. Select each of the eight labels, then look at the Properties window.
Whatever you put for values will apply to all selected controls.
 
Thanks. I tried but unfortunately but it seems that a label can never get
the 'active control' status. I will try to make it work with textboxes or
else buttons.

Lars

Douglas J. Steele said:
Sounds as though you need to use Screen.ActiveControl, as I suggested.

Change sChoose into a function if it's currently a sub (even if it doesn't
return any value). Don't pass anything to it. You can then use:

Function sChoose()
Dim strValue As String

strValue = Right(Screen.ActiveControl.Name)

' Continue with the rest of your code, using strValue
' wherever you need the number of the label

End Function

Now, set the OnClick event for each of the controls to =sChoose() instead
of [Event Procedure] (you must include the equal sign and parentheses).

Note that you can change the value to =sChoose() for all eight labels at
once. Select each of the eight labels, then look at the Properties window.
Whatever you put for values will apply to all selected controls.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Lars Brownie said:
Txt6 is a label. I have a little form with 8 of these labels. User can
click one of the labels, after which the chosen number is set in another
form's field and the little form closes. Each of the 8 label's click
events has the same code:

sChoose(<<the number of the respective label>>)

The sub sChoose puts the respective number in the other form and closes
it. I made this sub to reduce the amount of code lines.

In stead of having to set the respective number for each event I was
wondering if the name of the sub can be extracted somehow so that the
code for every label is identical.

So in short the question is: Is it possible, in a sub, to grab the name
of that same sub?

Lars
 
Yeah, sorry I wasn't thinking. Of course labels can never be the active
control, since they cannot have focus.

Use text boxes. You can make them look the same as labels.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Lars Brownie said:
Thanks. I tried but unfortunately but it seems that a label can never get
the 'active control' status. I will try to make it work with textboxes or
else buttons.

Lars

Douglas J. Steele said:
Sounds as though you need to use Screen.ActiveControl, as I suggested.

Change sChoose into a function if it's currently a sub (even if it
doesn't return any value). Don't pass anything to it. You can then use:

Function sChoose()
Dim strValue As String

strValue = Right(Screen.ActiveControl.Name)

' Continue with the rest of your code, using strValue
' wherever you need the number of the label

End Function

Now, set the OnClick event for each of the controls to =sChoose() instead
of [Event Procedure] (you must include the equal sign and parentheses).

Note that you can change the value to =sChoose() for all eight labels at
once. Select each of the eight labels, then look at the Properties
window. Whatever you put for values will apply to all selected controls.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Lars Brownie said:
Txt6 is a label. I have a little form with 8 of these labels. User can
click one of the labels, after which the chosen number is set in another
form's field and the little form closes. Each of the 8 label's click
events has the same code:

sChoose(<<the number of the respective label>>)

The sub sChoose puts the respective number in the other form and closes
it. I made this sub to reduce the amount of code lines.

In stead of having to set the respective number for each event I was
wondering if the name of the sub can be extracted somehow so that the
code for every label is identical.

So in short the question is: Is it possible, in a sub, to grab the name
of that same sub?

Lars


"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> schreef in
bericht It's not really clear what it is you're looking for. To get the last
character of whatever's in txt6, you'd use

Private Sub txt6_Click()
sChoose Right(Me.txt6, 1)
End Sub

To get the last character of whatever text box is active, you'd use

If TypeOf Screen.ActiveControl Is Textbox Then
sChoose Right(Screen.ActiveControl & vbNullString, 1)
End If

If you've clicked on a command button and want the last character of
whatever text box was active before you clicked on the command button,
you'd use

If TypeOf Screen.PreviousControl Is Textbox Then
sChoose Right(Previous.ActiveControl & vbNullString, 1)
End If

The reason for appending vbNullString to the control is to ensure that
Null isn't returned (in case sChoose is only expecting a string)


--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Thanks, but it's not exactly what I meant. In the click_event of txt6
I want to grab the last character of txt6: the 6, so I can use the
code generic in the other textboxes.

Lars


"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> schreef in
bericht Private Sub txt6_Click()
sChoose (Me.txt6)
End Sub

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


I have the following code:

Private Sub txt6_Click()
sChoose ("6")
End Sub

Is it possible to make it more generic so that the sub sChoose can
automatically pick the "6" from txt6_Click ?

Thanks, Lars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top