Results 1 to 9 of 9

Thread: [RESOLVED] Problem validate Textboxes in order to set spinbutton and scollbar with values

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    12

    Resolved [RESOLVED] Problem validate Textboxes in order to set spinbutton and scollbar with values

    Hi all,
    I have made tree textboxes (TextBox1,TextBox2,TextBox3).
    Also, I have a spinbutton (spinbutton1) and two scroollbars(scrollbar1,scrollbar2).

    I want user to fill textbox1 with values from 0-100
    textbox2 from 0-360 and textbox3 0-360.

    User have the option to change values in textboxes using spinbutton and scrollbars.

    On the bottom of my form I use two buttons, a compute and a reset button.
    I want compute button to become enable when user has type only number in a spesific range.

    But, it seems verifyrange() does not work

    Could tou help me please correct my code ?


    Code:
    Const light_blue_color As Long = &HFFDBB7
    Const white_color As Long = &HFFFFFF
    Const red_color As Long = &H1A10D1
    Const black_color As Long = &H0
    
    Const SpinMin As Integer = 0
    Const SpinMax As Integer = 100
    Const SpinChallenge As Integer = 1
    
    Const ScrollMin As Integer = 0
    Const ScrollMax As Integer = 360
    Const ScrollChallenge As Integer = 1
    
    
    
    Private Sub Frame1_Click()
    
    End Sub
    
    Private Sub Label1_Click()
    
    End Sub
    
    Private Sub UserForm_Initialize()
    
        'disable all buttons
        Call disenable(False, False)
        ErrorImage.Visible = False
        
         '================================================
        'TextBoxes(color)
        '================================================
        With TextBox1
            .Text = ""
            .BackColor = light_blue_color
        End With
        With TextBox2
            .Text = ""
            .BackColor = light_blue_color
        End With
        With TextBox3
            .Text = ""
            .BackColor = light_blue_color
        End With
        
        '================================================
        'spinbutton and scrollbars (min,max,SmallChange)
        '================================================
        With SpinButton1
            .Min = SpinMin
            .Max = SpinMax
            .SmallChange = SpinChallenge
        End With
        With ScrollBar1
            .Min = ScrollMin
            .Max = ScrollMax
            .SmallChange = ScrollChallenge
        End With
        With ScrollBar2
            .Min = ScrollMin
            .Max = ScrollMax
            .SmallChange = ScrollChallenge
        End With
          
    End Sub
    
    Could I make verifyrange and 
    
    Private Sub reset_Click()
        Call UserForm_Initialize
    End Sub
    
    Private Sub SpinButton1_Change()
        TextBox1.Value = SpinButton1.Value
    End Sub
    
    Private Sub ScrollBar1_Change()
       TextBox2.Value = ScrollBar1.Value
    End Sub
    
    Private Sub ScrollBar2_Change()
       TextBox3.Value = ScrollBar2.Value
    End Sub
    
    Private Sub TextBox1_Change()
            Call checkNumericValue(TextBox1)
            Call TextboxesifFilled(TextBox1, TextBox2, TextBox3)
    End Sub
    
    
    Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        TextBox1.ControlTipText = "Please Type distanse ..."
    End Sub
    
    Private Sub TextBox2_Change()
            Call checkNumericValue(TextBox2)
            Call TextboxesifFilled(TextBox1, TextBox2, TextBox3)
    End Sub
    
    Private Sub TextBox2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        TextBox2.ControlTipText = "Please Type angle1 ..."
    End Sub
    
    Private Sub TextBox3_Change()
            Call checkNumericValue(TextBox3)
            Call TextboxesifFilled(TextBox1, TextBox2, TextBox3)
    End Sub
    
    Private Sub TextBox3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        TextBox3.ControlTipText = "Please Type angle2 ..."
    End Sub
    
    
    
    
    'blue when empty - white when is filled - red when is wrong input
    Private Sub checkNumericValue(ByRef tb As MSForms.textbox)
        If tb.Text = "" Then
            tb.BackColor = light_blue_color
            ErrorImage.Visible = False
            Label1.Caption = ""
        ElseIf tb.Text <> "" And verifyNumeric(tb.Value) = False Then
            tb.BackColor = red_color
            tb.ForeColor = white_color
        Else
            tb.BackColor = white_color
            tb.ForeColor = black_color
        End If
    End Sub
    
    
    
    
    'Enable/Disable buttons compute and reset
    Private Sub disenable(ByVal computeBool As Boolean, ByVal resetBool As Boolean)
            compute.Enabled = computeBool
            reset.Enabled = resetBool
    End Sub
    
    
    Private Sub TextboxesifFilled(ByRef tb1 As MSForms.textbox, ByRef tb2 As MSForms.textbox, ByRef tb3 As MSForms.textbox)
        If tb1.Value <> "" And tb2.Value <> "" And tb3.Value <> "" Then
            If verifyRange(TextBox1.Text, SpinMin, SpinMax) And verifyRange(TextBox2.Text, ScrollMin, ScrollMax) And verifyRange(TextBox3.Text, ScrollMin, ScrollMax) Then
                Call disenable("True", "True")
            End If
        ElseIf tb1.Value = "" And tb2.Value = "" And tb3.Value = "" Then
            Call disenable("False", "False")
        Else
             Call disenable("False", "True")
        End If
    End Sub
    
    
    
    '======================================================================
    '= ================================================================== =
    '=                      Validate Inputs on TextBoxes                  =
    '= ================================================================== =
    '======================================================================
    Public Function verifyNumeric(vInput As Variant) As Boolean
        Dim strMsg As String
        If Not myIsNumeric(vInput) And vInput <> "" Then
            ErrorImage.Visible = True
            strMsg1 = "Error : '" & vInput & "' is not numeric."
            Label1.ForeColor = red_color
            Label1.Caption = strMsg1
            verifyNumeric = False
        Else
            verifyNumeric = True
        End If
    End Function
    
    Public Function myIsNumeric(vInput As Variant) As Boolean
        Dim I As Integer
        Dim strChr As String
        
        If Not Trim(vInput & " ") = "" Then
            myIsNumeric = True
            vInput = CStr(vInput)
        
            For I = 1 To Len(vInput)
                strChr = Mid(vInput, I, 1)
                If Not IsNumeric(strChr) Then myIsNumeric = False
            Next I
        End If
    End Function
    
    Public Function verifyRange(vInput As Variant, minValue As Variant, maxValue As Variant) As Boolean
        Dim strMsg As String
        If Val(vInput) < minValue Or Val(vInput) > maxValue Then
            ErrorImage.Visible = True
            strMsg2 = "Error : '" & vInput & "' is not in range [" & minValue & "," & maxValue & "]."
            Label1.ForeColor = red_color
            Label1.Caption = strMsg1
            verifyRange = False
        Else
            verifyRange = True
        End If
    End Function

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    Hi Prokopis and welcome to VBForums! Also thank you so very much for using code tags, not to many people know how to use them.

    Could you do me a favor and verify which version of Visual Basic it is that you're using?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    12

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    dday9
    Could you do me a favor and verify which version of Visual Basic it is that you're using?
    I am using the visual basic of excel 2010

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    Ah, ok. This question is for Visual Basic for Applications or VBA. I'll move the thread to office development where it belongs.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    What's not working? I can get it do display an error message, and make an error image visible when I put a number larger than 100 in textbox1.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    12

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    You are right. Something I made wrong.

    Testing this application I typed a number [0,100] on textbox1 , on textbox2 a number [0,360] and textbox3 a number different [0,360] for instance 361.
    If I delete one digit in the end the error image and message are still displayed.
    Also, Scrollbar values are set when
    Any ideas ?

    Also I made the following changes :
    Code:
    Private Sub TextboxesifFilled(ByRef tb1 As MSForms.textbox, ByRef tb2 As MSForms.textbox, ByRef tb3 As MSForms.textbox)
        If tb1.Value <> "" And tb2.Value <> "" And tb3.Value <> "" Then
            If verifyRange(TextBox1.Text, SpinMin, SpinMax) And verifyRange(TextBox2.Text, ScrollMin, ScrollMax) And verifyRange(TextBox3.Text, ScrollMin, ScrollMax) Then
                Call disenable("True", "True")
                ScrollBar1.Value = tb2.Value
                ScrollBar2.Value = tb3.Value
            End If
        ElseIf tb1.Value = "" And tb2.Value = "" And tb3.Value = "" Then
            Call disenable("False", "False")
        Else
             Call disenable("False", "True")
        End If
    End Sub
    Code:
    Public Function verifyRange(vInput As Variant, minValue As Variant, maxValue As Variant) As Boolean
        Dim strMsg As String
        If Val(vInput) < minValue Or Val(vInput) > maxValue Then
            ErrorImage.Visible = True
            strMsg2 = "Error : '" & vInput & "' is not in range [" & minValue & "," & maxValue & "]."
            Label1.ForeColor = red_color
            Label1.Caption = strMsg2
            verifyRange = False
        Else
            verifyRange = True
        End If
    End Function

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

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    There are only two places where you make ErrorImage not visible. One is when the form is initialized, the other is in the checkNumericValue sub when a textbox is blank.

  8. #8
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    do you have any reason to use
    textboxes and spinbuttons/scrollbars
    instead of
    labels and scrollbars
    (other then making it your users difficult) ?

    For heaven's sake, make it easy for your users (and yourself)

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    12

    Re: Problem validate Textboxes in order to set spinbutton and scollbar with values

    Quote Originally Posted by IkkeEnGij View Post
    do you have any reason to use
    textboxes and spinbuttons/scrollbars
    instead of
    labels and scrollbars
    (other then making it your users difficult) ?

    For heaven's sake, make it easy for your users (and yourself)
    You have right ! I correct this as you said.
    Thanks a lot

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