[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.
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: [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
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