View Single Post
 
Old 03-19-2019, 09:31 PM
ScottyBee ScottyBee is offline Windows 10 Office 2016
Novice
 
Join Date: Mar 2019
Location: Portland, Oregon
Posts: 12
ScottyBee is on a distinguished road
Default Loop Through Boolean Values

Hello Guess,

Yes, my latest code was to augment the code you posted earlier shown here:

Private Sub btnOK_Click()
Dim i As Integer, aCtl As Control
For i = 1 To 8
With Me.Controls("TextBox" & i)
If .Value = "" Then
MsgBox "Nothing in this one"
.SetFocus
Exit Sub
Else
MsgBox .Name & vbCr & .Value
End If
End With
Next i
End Sub

I adapted it a little but it worked great in identifying null values and returning focus to the textbox control that needed to be entered.

My most recent post was trying to take the ideas you gave me to solve another issue. That issue involves checking that all textboxes have been changed from their default values. A snapshot of my user form is shown here:

User Form.JPG

I added your code to the "Check for Nulls" command button and it work like a charm. As you can see from the user form there are default values presented to the user. They are required to enter all fields. The code you provided catches when the user presses delete then tabs to the next field leaving the previous one blank.

Taking your idea, I tried to code the "Verify all Fields Updated" button to check for changes. In a module, I have declared public boolean variables to be used in the Change Event for each textbox control on the form. When the textbox is changed, then the code changes the boolean value to True.

I was trying to think of a way to loop all of the boolean variables like you did with the text objects but I guess that cannot be done. It looks like I will have to text them individually.

I did have a question about your code here:

Sub UserForm_Initialize()
Me.TextBox1 = "Initial value"
Me.PreTextBox1 = "Initial value" 'each visible TextBox has a hidden partner prefilled with same initial content
End Sub
Sub UserForm_Validate()
If Me.TextBox1 <> Me.PreTextBox1 then
MsgBox "A change was detected"
end if
End Sub

Questions:

1. Since I have 8 textboxes, I assume I will duplicate your code for each control and have the focus returned to the control that did not change. I also have 3 combo boxes but will code them later.

2. Looks like I have two approaches to this issue. Use your logic using hidden textboxes or use a change event for each control with code to test for any change? Is any better than the other?

3. A final validation for my form will involve checking the textbox values for a fixed number of numerical characters. Are you aware of any resources online going over VBA validation techniques for form texboxes or other controls?

This will be a project a week or two down the road.

Thanks much!
Reply With Quote