Variable control names in sub to show space left in text box

G

Guest

I have 5 text boxes on my form, each with the max length set to 255
characters. On the AfterUpdate and KeyUp events for each text box I will call
the one procedure to show the max number of characters left to be entered.
Instead of having 5 different procedures I have tried to make one which takes
in the textbox name and label name, in this case txtNotes and lblNotes.
However, i'm having loads of problems with this code and it doesn't even come
close to working. Can any one see what's wrong with it?

Private Sub txtNotes_AfterUpdate()
TextBoxLengths "txtNotes", "lblNotes"
End Sub
Private Sub txtNotes_KeyUp(KeyCode As Integer, Shift As Integer)
TextBoxLengths "txtNotes", "lblNotes"
End Sub

Public Sub TextBoxLengths(cntTextBoxName, cntLabelName As Controls)
If Me.ActiveControl.Name = cntTextBoxName Then
Me.Controls(cntLabelName.Caption) = "(" & 255 -
Len(Me.Controls(cntTextBoxName.Text)) & ")"
If Len(Me.Controls(cntTextBoxName.Text)) < 1 Then
Me.Controls(cntLabelName.Visible) = False
Else
Me.Controls(cntLabelName.Visible) = True
End If
Else
If Len(Me.Controls(cntTextBoxName)) > 0 Then
Me.Controls(cntLabelName.Visible) = True
Me.Controls(cntLabelName.Caption) = "(" & 255 -
Len(Me.Controls(cntTextBoxName)) & ")"
Else
Me.Controls(cntLabelName.Visible) = False
End If
End If
End Sub


Thanks!
 
W

Wolfgang Kais

Hello Adam.

Adam said:
I have 5 text boxes on my form, each with the max length set to 255
characters. On the AfterUpdate and KeyUp events for each text box I
will call the one procedure to show the max number of characters left
to be entered. Instead of having 5 different procedures I have tried
to make one which takes in the textbox name and label name, in this
case txtNotes and lblNotes. However, i'm having loads of problems
with this code and it doesn't even come close to working.
Can any one see what's wrong with it?

Private Sub txtNotes_AfterUpdate()
TextBoxLengths "txtNotes", "lblNotes"
End Sub
Private Sub txtNotes_KeyUp(KeyCode As Integer, Shift As Integer)
TextBoxLengths "txtNotes", "lblNotes"
End Sub

Public Sub TextBoxLengths(cntTextBoxName, cntLabelName As Controls)

The first argument has type "variant", the second "controls collection".
Both arguments should be declared "as String".
If Me.ActiveControl.Name = cntTextBoxName Then
Me.Controls(cntLabelName.Caption) = "(" & 255 -
Len(Me.Controls(cntTextBoxName.Text)) & ")"
If Len(Me.Controls(cntTextBoxName.Text)) < 1 Then
Me.Controls(cntLabelName.Visible) = False
Else
Me.Controls(cntLabelName.Visible) = True
End If
Else
If Len(Me.Controls(cntTextBoxName)) > 0 Then
Me.Controls(cntLabelName.Visible) = True
Me.Controls(cntLabelName.Caption) = "(" & 255 -
Len(Me.Controls(cntTextBoxName)) & ")"
Else
Me.Controls(cntLabelName.Visible) = False
End If
End If
End Sub

"Caption", "Text" and "Visble" are properties of "Controls(...)",
so their dot has to appear outside the brackets.

But why not use the Change event (OnChange)? If doing so, you
only need one event procedure per textbox and you also can be sure
that the active control is the one you are editing.
(Why do you want the label to disappear when the textbox is blank?)

Private Sub txtNotes_Change()
UpdateLabel "lblNotes"
End Sub

Private Sub UpdateLabel(strLabelName As String)
Dim lngLength as Long
lngLength = Len(Screen.ActiveControl.Text)
With Me.Controls(strLabelName)
.Caption = "(" & 255 - lngLength & ")"
.Visible = (lngLength > 0)
End With
End Sub
 

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