If Statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dawn123
    New Member
    • Nov 2009
    • 15

    If Statements

    I have a program that allows the user to enter data I am using if statements that only will allow the user to enter integers and numbers if its greater than other and that works fine if the number or value is incorrect then a msg box appears. I have it so that if the number is wrong it takes the invalid number out of the test box.. I need to have it so that the user can enter the values over and over again. so the text boxes need to be cleared after the enter button is clicked. I have it now so that every time enter is clicked it clears the txt boxes but i only want them emptied once all the values are correct. I dont know how to do this with a if statement. I dont know how to tell it that the values are correct.. this is the code i am using
    Code:
    Private Sub cmdAdd_Click()
    
        strRepTree(j, 1) = cboSpecies.Text
    If Val(txtAge) <> Int(Val(txtAge)) Or Not IsNumeric(txtAge) Then
        MsgBox " Invalid Age"
        txtAge = ""
    Else
        strRepTree(j, 2) = txtAge
    End If
    
    If Not IsNumeric(txtHeight) Or Val(txtHeight) < Val(txtBLC) Then
        MsgBox "BLC value can not be greater than Height value "
        txtHeight = ""
    Else
        strRepTree(j, 3) = txtHeight
    End If
    
    If Not IsNumeric(txtBLC) Then
        MsgBox "Enter correct BLC value"
        txtBLC = ""
    Else
        strRepTree(j, 4) = txtBLC
    End If
    
    If Not IsNumeric(txtDia) Then
        MsgBox "Enter Correct Diameter"
        txtDia = ""
    Else
        strRepTree(j, 5) = txtDia
    End If
    
        frmStandVolume.lstRepTreeSum.AddItem strRepTree(j, 1) & "," & strRepTree(j, 2) & "," & strRepTree(j, 3) & "," & strRepTree(j, 4) & "," & strRepTree(j, 5)
       j = j + 1
     
    '****************** This is where I believe my if statements needs to go to see if all the numbers are correct************* 
        intRepTreeRecs = j - 1
        optHwd.Value = False
        optSwd.Value = False
        cboSpecies.Clear
        txtAge = ""
        txtHeight = ""
        txtBLC = ""
        txtDia = ""
    Last edited by Frinavale; Dec 23 '09, 02:57 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • dawn123
    New Member
    • Nov 2009
    • 15

    #2
    Nevermind. I got it working..

    Comment

    • Guido Geurs
      Recognized Expert Contributor
      • Oct 2009
      • 767

      #3
      dear,

      The first IF's are checking for a valid value.
      if the value is wrong, just quit the SUB in each If like this:

      Code:
      Private Sub cmdAdd_Click()
        
          strRepTree(j, 1) = cboSpecies.Text
          If Val(txtAge) <> Int(Val(txtAge)) Or Not IsNumeric(txtAge) Then
              MsgBox " Invalid Age"
              txtAge = ""
              Exit Sub
          Else
              strRepTree(j, 2) = txtAge
          End If
        
          If Not IsNumeric(txtheight) Or Val(txtheight) < Val(txtblc) Then
              MsgBox "BLC value can not be greater than Height value "
              txtheight = ""
              Exit Sub
          Else
              strRepTree(j, 3) = txtheight
          End If
        
          If Not IsNumeric(txtblc) Then
              MsgBox "Enter correct BLC value"
              txtblc = ""
              Exit Sub
          Else
              strRepTree(j, 4) = txtblc
          End If
        
          If Not IsNumeric(txtdia) Then
              MsgBox "Enter Correct Diameter"
              txtdia = ""
              Exit Sub
          Else
              strRepTree(j, 5) = txtdia
          End If
        
          frmStandVolume.lstRepTreeSum.AddItem strRepTree(j, 1) & "," & _
                                              strRepTree(j, 2) & "," & _
                                              strRepTree(j, 3) & "," & _
                                              strRepTree(j, 4) & "," & _
                                              strRepTree(j, 5)
         j = j + 1
        
      '****************** This is where I believe my if statements needs
      'to go to see if all the numbers are correct*************
      
              intRepTreeRecs = j - 1
              optHwd.Value = False
              optSwd.Value = False
              cboSpecies.Clear
              txtAge = ""
              txtheight = ""
              txtblc = ""
              txtdia = ""
      End Sub
      Or you can check if all the textboxes have a value (the are correct because they are checked by the previous IF's.
      So you can enter in your exist code:

      Code:
          If cboSpecies <> "" And txtAge <> "" And txtheight <> "" And _
              txtblc <> "" And txtdia <> "" Then
              frmStandVolume.lstRepTreeSum.AddItem strRepTree(j, 1) & "," & _
                                              strRepTree(j, 2) & "," & _
                                              strRepTree(j, 3) & "," & _
                                              strRepTree(j, 4) & "," & _
                                              strRepTree(j, 5)
         j = j + 1
        
      '****************** This is where I believe my if statements needs
      'to go to see if all the numbers are correct*************
      
              intRepTreeRecs = j - 1
              optHwd.Value = False
              optSwd.Value = False
              cboSpecies.Clear
              txtAge = ""
              txtheight = ""
              txtblc = ""
              txtdia = ""
          End If
      br,

      Comment

      Working...