[RESOLVED] How to verify if one of two fields is filled in
Hi forum
I have 2 text boxes on an Excel workbook and I want to use VBA to validate the field content.
The user has to fill in at least one of these text boxes with a numeric value greater than 0. If he fills in both then that is fine.
If neither is complete then I'd like to format the text box so that it is obvious something needs to be filled in for that particular field.
Any help / code sample is greatly appreciated.
Thanks
Re: How to verify if one of two fields is filled in
Some code to play with:
Code:
Private Sub cmdValidate_Click()
Dim tb1Val 'text box 1 value
Dim tb2Val 'text box 2 value
Dim tb1Good As Boolean
Dim tb2Good As Boolean
tb1Val = tb1.Value
tb2Val = tb2.Value
If IsNumeric(tb1Val) Then
If tb1Val <= 0 Then
'not okay
Else
tb1Good = True
Exit Sub
End If
End If
If IsNumeric(tb2Val) Then
If tb2Val <= 0 Then
'not okay
Else
tb2Good = True
Exit Sub
End If
End If
If tb1Good = False And tb2Good = False Then
tb1.BorderStyle = fmBorderStyleSingle
tb1.BorderColor = vbRed
End If
End Sub
TB1 is the name of the first text box, TB2 is the second...
Re: How to verify if one of two fields is filled in
Thanks a lot for this and for replying to all other post over the last couple of weeks.
Code sample looks good. Thanks. I will give it ago within my project and adjust as needed.