Results 1 to 3 of 3

Thread: [RESOLVED] How to verify if one of two fields is filled in

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    90

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    90

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width