Reference Label from TextBox

S

Stephen Newman

I am using a class I found here that works very well for my needs. Now
I would like to reference and change the caption of a label of the
same name (number) as the text box.

The Class:

Public WithEvents txtBox As MSForms.TextBox, CtlNum As String
Public laBl As String

Private Sub Class_Terminate()
Set txtBox = Nothing
End Sub

Private Sub txtBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 46, 48 To 57
If Right(txtBox, 2) = ".5" Then KeyAscii = 0
If KeyAscii = 46 Then SendKeys ("5")
''' These are all number. No Problem
Case Else
''' Some other kind of character. Beep and cancel it.
Beep
KeyAscii = 0
End Select
SetLabel
End Sub

Private Sub txtBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
If KeyCode = 8 Then txtBox.Value = ""
End Sub

Private Sub txtBox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If Len(txtBox.Text) = 3 And Right(txtBox.Text, 2) <> ".5" Then txtBox
= Null
If Len(txtBox.Text) > 4 Then txtBox = Null
End Sub

'Here's where it fails.

Sub SetLabel()
'This returns the TextBox number
CtlNum = Mid(txtBox.Name, 8, Len(txtBox.Name))
laBl = "Label" & 3
MsgBox UserForm.Name.laBl.Caption
End Sub

All help will be appreciated.

Thank you.
 
P

Peter T

Your routine "SetLabel" doesn't make any sense, what do you want it to do.

Regards,
Peter T
 
R

Rumplestiltskin

With a little more Google searching I've found how to access the
label. The routine I'm using creates a UserForm dynamically, and
places controls based on worksheet cell information. The UserForm name
changes if it detects a similarly named UserForm in the Forms folder.
I've got all that working well. Now, all I need is the method to
access the userform from the class.
Your routine "SetLabel" doesn't make any sense, what do you want it to do.

Regards,
Peter T
Sub SetLabel()
'This returns the TextBox number
CtlNum = Mid(txtBox.Name, 8, Len(txtBox.Name))
'Currently, Me is invalid. I need the name of the active UserForm
MsgBox Me.Controls("Label" & CtlNum).Object.Caption
End SubI can write what I need into the routine later. Right now all I need
to be able to do is access the control from the class.
 
P

Peter T

I have even less idea of what you are doing now than I had before. Anyway
just this bit
Now, all I need is the method to
access the userform from the class.

Assuming you mean you want to reference the userform that contains your the
textbox which is as referenced by Public WithEvents txtBox, try

txtBox.Parent

Regards,
Peter T
 
R

Rumplestiltskin

I have even less idea of what you are doing now than I had before. Anyway
just this bit

Assuming you mean you want to reference the userform that contains your the
textbox which is as referenced by Public WithEvents txtBox, try

txtBox.Parent

Regards,
Peter T
Thank for the input, but that didn't work. What I require is a method
to reference the currently active UserForm.
 
P

Peter T

Rumplestiltskin said:
Thank for the input, but that didn't work. What I require is a method
to reference the currently active UserForm.

Your routine SetLabel is called by the keypress event of a textbox on a
form. Surely the form containing the textbox is the "currently active
UserForm". That being the case txtBox.Parent will reference the form.

If that's not the case you really need to explain what you are doing
otherwise to assist is just a guessing game.

Regards,
Peter T
 
P

Peter T

Peter T said:
Your routine SetLabel is called by the keypress event of a textbox on a
form. Surely the form containing the textbox is the "currently active
UserForm". That being the case txtBox.Parent will reference the form.

.....unless the textbox is "in" a frame or multipage in which case you'd use

txtBox.Parent.Parent

or if on a Multipage then you'd use

txtBox.Parent.Parent.Parent

Regards,
Peter T
 
S

Stephen Newman

Yes, it works now. I'd left out the textbox number when I tried.

Thanks very much for your help.
 

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

Top