Results 1 to 18 of 18

Thread: [RESOLVED] passing reference of textBox to function

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Resolved [RESOLVED] passing reference of textBox to function

    On my UserForm I have tons of textBox's that I require validating the input on. Instead of repeating this code in all of the _Change() functions I thought it would be best to write a public one and call it the 100 times. Unfortunately I can't figure out how to pass the TextBox reference correctly. Is this possible or not?

    On my form I have a txtBox called "txtc2srequest"
    The Change function is:
    Private Sub txtc2srequest_Change()
    Call ConfirmInput(txtc2srequest, 15, 1)
    End Sub

    Public Sub ConfirmInput(ByRef CurrentTextBox As TextBox, length As Integer, which As Integer)
    If (CurrentTextBox.TextLength < length) Then
    CurrentTextBox.BackColor = vbRed
    GoTo DONE
    ElseIf (CurrentTextBox.TextLength > length) Then
    CurrentTextBox.BackColor = vbRed
    GoTo DONE
    ElseIf (CurrentTextBox.TextLength = length) Then
    CurrentTextBox.BackColor = vbWhite
    If which Then
    bigStringToFields
    Else
    indivToBigString
    End If
    DONE:
    End Sub

    I get a "Type Mismatch Error 13" and the line is the Call ConfirmInput line. When I mouse-over txt2c2srequest it gives me the value of txtc2srequest.Value and doesn't act like a "TextBox" or larger data type.

    Help!
    Thanks

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: passing reference of textBox to function

    TextLength comes up in the auto-complete. But still that's not the problem, but I'll keep that in mind if I get past this first issue! (Thanks!)

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: passing reference of textBox to function

    Help->About says VB6.5.1053

  6. #6

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: passing reference of textBox to function

    This looks like VBA,

    You should fully qualify the textbox in the ConfirmInput subroutine
    Code:
    Public Sub ConfirmInput(CurrentTextBox As msforms.TextBox, length As Integer, which As Integer)
    (Actually, using 'As Object' or 'As Control' also work but you wont get 'auto-complete')
    Last edited by Doogle; May 2nd, 2011 at 04:18 AM.

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: passing reference of textBox to function

    Quote Originally Posted by Doogle View Post
    You should fully qualify the textbox in the ConfirmInput subroutine...
    That isn't necessary at all - you do that when passing control to subroutine. This way you can pass control from any form.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: passing reference of textBox to function

    I'm sorry... desperate for help I (again) posted in the wrong location. Some boards make it less obvious but it would help if I knew the acronyms a little better. This ~is~ VBA (which I now know what that means!). I've mostly used VB6 to make standalone programs and was taking a short-cut doing this in Excel.

    "As Object" was the key! Everything worked as expected then!
    Thank you both for your time!!

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: passing reference of textBox to function

    Quote Originally Posted by RhinoBull View Post
    That isn't necessary at all
    I beg to differ.......

    Using VBA in Excel (for example)

    Given you have a UserForm and a TextBox (TextBox1) and CommandButton (CommandButton1)

    Code:
    Private Sub MYSub(txt As TextBox)
    MsgBox txt.Name
    End Sub
    
    Private Sub CommandButton1_Click()
    Call MYSub(TextBox1)
    End Sub
    Results in "Type Mismatch", on the call, whereas
    Code:
    Private Sub MYSub(txt As msforms.TextBox)
    MsgBox txt.Name
    End Sub
    
    Private Sub CommandButton1_Click()
    Call MYSub(TextBox1)
    End Sub
    results in the MsgBox being displayed.

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: passing reference of textBox to function

    Quote Originally Posted by Doogle View Post
    I beg to differ.......

    Using VBA in Excel (for example)...
    And I was referring to VB6 which is where we are - VBA related questions need to be posted/answered in VBA forum.
    This thread should've been moved...

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: [RESOLVED] passing reference of textBox to function

    If it wasn't clear from my number of posts, I'm new.
    Mea Culpa

    I looked how to move it when you first pointed out my mistake, Rhino and couldn't find it. I JUST now found how to mark the question as resolved. Is it possible to move it?

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] passing reference of textBox to function

    Moved To Office Development

    Thanks for the report Rhino!

    Is this thread resolved?

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: [RESOLVED] passing reference of textBox to function

    Yes, Hack... thanks. I will "report" if I ever post to the wrong forum again!

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: [RESOLVED] passing reference of textBox to function

    Solution
    Code:
    Public Sub ConfirmInput(ByRef CurrentTextBox As Object, length As Integer)
        If (Len(CurrentTextBox.Text) < length) Then
            CurrentTextBox.BackColor = vbRed
            GoTo DONE
        ElseIf (Len(CurrentTextBox.Text) > length) Then
            CurrentTextBox.BackColor = vbRed
            GoTo DONE
        ElseIf (Len(CurrentTextBox.Text) = length) Then
            CurrentTextBox.BackColor = vbWhite
        End If
    DONE:
    End Sub

  16. #16
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] passing reference of textBox to function

    Get rid of the GoTo and the "DONE:" altogether as it's absolutely unnecessary.
    If some of your logic requires exiting procedure rather than continue you can always use Exit Sub/Function instead.

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: [RESOLVED] passing reference of textBox to function

    In my effort to clean up portions of the code that don't matter to the post I forgot to remove that part.
    "Exit" is better though (of course). I'll change it in your honor!

  18. #18
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] passing reference of textBox to function

    In fact you can shorten the code to
    Code:
    Public Sub ConfirmInput(ByRef CurrentTextBox As Object, length As Integer)
    If (Len(CurrentTextBox.Text) = length) Then
        CurrentTextBox.BackColor = vbWhite
    Else
        CurrentTextBox.BackColor = vbRed
    End If
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width