[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 :)
Re: passing reference of textBox to function
TextLength is NOT valid property in VB6 - replace that with this:
Len(CurrentTextBox.Text)
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!)
Re: passing reference of textBox to function
Then you NOT using VB6 (aka VB 1998).
Re: passing reference of textBox to function
Help->About says VB6.5.1053
1 Attachment(s)
Re: passing reference of textBox to function
Is that a VBA code editor? How do you launch it? From one office apps?
Here is what real VB6 should look like.
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')
Re: passing reference of textBox to function
Quote:
Originally Posted by
Doogle
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.
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!!
Re: passing reference of textBox to function
Quote:
Originally Posted by
RhinoBull
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.
Re: passing reference of textBox to function
Quote:
Originally Posted by
Doogle
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...
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?
Re: [RESOLVED] passing reference of textBox to function
Moved To Office Development
Thanks for the report Rhino! :thumb:
Is this thread resolved?
Re: [RESOLVED] passing reference of textBox to function
Yes, Hack... thanks. I will "report" if I ever post to the wrong forum again!
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
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.
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!
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