Results 1 to 6 of 6
  1. #1
    jbeets is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2020
    Posts
    94

    Add criteria to existing vba calculation that returns "N/A" if all responses in the category are N/A

    Hello,



    The below calculation is used on a form that creates a score based on the responses in the combo boxes. In some cases all of the responses in a category may be "N/A" which then causes an error based on the way the current code is written. I need to modify the code to say that if all of the responses are "N/A" then return N/A in the CoverageScore box otherwise complete the calculation.

    This is what I am trying to accomplish but I am getting an error saying "Else without If". I'm sure I'm missing something silly but I just dont know.

    Code:
    If nYes = 0 Then CoverageScore = "N/A"Else
    CoverageScore = Format((nYes) / (nYes + nNo + nNA), "Percent")
    End If
    The full code is below:

    Code:
    Private Sub btnCoverageScore_Click()Dim c As Control, nYes As Long, nNo As Long, nNA As Long
    nYes = 0
    nNo = 0
    nNA = 0
    
    
    
    
    For Each c In Me.Controls
        If c.Tag = "Coverage" Then
            If c.Value = "Yes" Then nYes = nYes + 1
            If c.Value = "No" Then nNo = nNo + 1
            If c.Value = "NA" Then nNA = nNA + 1
        End If
    Next c
    
    
    CoverageScore = Format((nYes) / (nYes + nPartial + nNo + nNA), "Percent")
    
    
    For Each c In Me.Controls
        If c.Tag = "Coverage" Then
            If IsNull(c.Value) Then
             MsgBox ("ComboBox selection left blank. Please ensure all drop downs are selected before continuing.")
        Else
            txtCoverageStatus = "Complete"
        End If
    End If
    Next c
    
    
    For Each c In Me.Controls
        If c.Tag = "Status" Then
            If c.Value = "Complete" Then
            txtCoverageStatus.BackColor = vbGreen
        End If
    End If
    Next c
    End Sub
    Thank you for any help you can provide!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,538
    Try deleting the End If
    Code:
    For Each c In Me.Controls    If c.Tag = "Coverage" Then
            If IsNull(c.Value) Then
             MsgBox ("ComboBox selection left blank. Please ensure all drop downs are selected before continuing.")
        Else
            txtCoverageStatus = "Complete"
        End If
    End If 
    Next c
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    jbeets is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2020
    Posts
    94
    I deleted the End If as suggested but I get the same error "Else without If" and it is highlighting this else:

    Code:
    If nYes = 0 Then CoverageScore = "N/A"Else
    CoverageScore = Format((nYes) / (nYes + nNo + nNA), "Percent")
    End If

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Try it this way:

    Code:
    If nYes = 0 Then 
        CoverageScore = "N/A"
    Else
        CoverageScore = Format((nYes) / (nYes + nNo + nNA), "Percent")
    End If
    Last edited by davegri; 01-11-2021 at 09:39 AM. Reason: clarif

  5. #5
    jbeets is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2020
    Posts
    94
    Thank you Davegri... That fixed my issue.

    Thank you both for your help today!

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Glad to help and thanks for the star!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 05-04-2017, 01:10 AM
  2. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  3. Replies: 4
    Last Post: 06-18-2015, 09:37 PM
  4. Multiple "category fields" in a form
    By QuakinTom in forum Access
    Replies: 2
    Last Post: 03-25-2014, 03:49 AM
  5. Replies: 2
    Last Post: 01-03-2014, 09:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums