Error message displayed on form depending on calculated value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djgeverson
    New Member
    • Feb 2014
    • 10

    Error message displayed on form depending on calculated value

    On my form I have a calculated value text box which is the sum of 5 text boxes. I have three error messages which I want to appear "high risk" "low risk" and "medium risk" depending on the value in the calculated text box. I have these error messages as labels on the form and when the form loads they are made not visible.

    I want the messages to appear as the "calculate" button I have is clicked. Currently I only the last error message in the code appears when the button is clicked and the value is within the range specified. I feel like its something simple that I am missing but cant figure it out.
    Code:
    Private Sub Command43_Click()
    
        txtcalculate.Value = CDbl(Nz([txt1], 0)) + CDbl(Nz([txt2], 0)) + CDbl(Nz([txt3], 0)) + CDbl(Nz([txt4], 0)) + CDbl(Nz([txt5], 0))
    
    
        If txtscalculate.Value <= 16 Then
                Me.lbllow.Visible = True
         End If
         
         
        If txtcalculate.Value >= 34 Then
            Me.lblhigh.Visible = True
        End If
    
    
        If txtcalculate.Value = 17 - 33 Then
            Me.lblmedium.Visible = True
        End If
    
                       
    End Sub
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1291

    #2
    djgeverson,
    You should begin with:
    Code:
    Me.lblmedium.Visible = false
    Me.lblhigh.Visible=false
    Me.lbllow.Visible=false
    otherwise each subsequent button click could result in more than one error message being displayed.

    This line
    Code:
    If txtcalculate.Value = 17 - 33 Then
    is an obvious error. Probably you meant
    Code:
    If txtcalculate.Value >= 17 and  txtcalculate.Value <=33 Then
    Are the 3 messages physically positioned separately? If they are on top of each other you might not see if more than one was lit up.

    Jim

    Comment

    • djgeverson
      New Member
      • Feb 2014
      • 10

      #3
      Hi Jim!

      Thanks so much for that! I am new to VB so didnt think the last piece of code was right but thanks for showing me how to do it! It now works like a treat!

      Comment

      Working...