Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

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

Discussion in 'Information Technology' started by Adam Thwaites, Jul 28, 2006.

  1. Adam Thwaites

    Adam Thwaites
    Expand Collapse
    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!
    --
    Adam Thwaites
    Access Database Designer
    adam.thwaites.remove@thisbit.mercedes.co.uk
    Manchester, UK
     
  2. Loading...

    Similar Threads Forum Date
    How To Control The 5 Thieves? Questions and Answers Apr 7, 2016
    General Controlling Kaam (Lust) Hard Talk Apr 8, 2014
    USA Sikh Americans join Barack Obama in urging lawmakers on Gun Control Breaking News Mar 29, 2013
    Controversial SGPC Takes Control of Live Telecast at Bangla Sahib Hard Talk Feb 7, 2013
    USA Sikhs Join Gun Control Debate. Meet with Vice President Biden Breaking News Jan 16, 2013

  3. Wolfgang Kais

    Wolfgang Kais
    Expand Collapse
    Guest

    Hello Adam.

    Adam Thwaites wrote:
    > 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

    --
    Regards,
    Wolfgang
     

Share This Page