I am trying to write code that will loop through controls in a frame on a userform, find text boxes and if the text box is empty then use a function to add a specified value to an array. I then want to display a message box based on the array showing which text boxes are empty.
I have the follow Sub and Function.
Private Sub CommandButton1_Click()
Dim ctrl1 As Control
Dim nTB As Integer
Dim asTB() As String
Dim sTB As String
Dim i As Integer
nTB = 0
For Each ctrl1 In NewReferral.Frame10.Controls
If TypeName(ctrl1) = "TextBox" Then
If ctrl1.Value = "" Then
'sTB = WhichTB(ctrl1)
nTB = nTB + 1
ReDim Preserve asTB(1 To nTB)
asTB(nTB) = WhichTB(ctrl1)
Else
End If
End If
Next ctrl1
For i = LBound(asTB) To UBound(asTB)
msg = msg & asTB(i) & vbNewLine
Next i
MsgBox "the values of my dynamic array are: " & vbNewLine & msg
Unload Me
End Sub
Function WhichTB(TargetTB As Control)
If TargetTB = Me.TextBox1 Then
WhichTB = "Sample Text 1"
Else
End If
If TargetTB = Me.TextBox2 Then
WhichTB = "Sample Text 2"
Else
End If
If TargetTB = Me.TextBox3 Then
WhichTB = "Sample Text 3"
Else
End If
End Function
My problem is that the message box displays the same text for each empty text box. For example, say I leave all 3 text boxes empty. My message box will display:
"the values of my dynamic array are:
Sample Text 3
Sample Text 3
Sample Text 3"
when what I was really looking for is:
"the values of my dynamic array are:
Sample Text 1
Sample Text 2
Sample Text 3"
I am pretty sure that the problem is that the "If" statements in the function reset the value for "WhichTB" on each subsequent encounter of an empty text box. I don't know how to pass discrete values for "WhichTB" back to the Sub and then to the array.
I hope that makes sense.
I appreciate any help.
Bookmarks