VBA: List field names where criteria not met?

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
Related to, but sufficiently different from:
http://www.mrexcel.com/forum/micros...veral-fields-visual-basic-applications-2.html

Classifying business customers: we have programs where each business aims for a Type (Good, Better, Best), and we measure what type they achieve based on a number of factors.
So if a business wants to be a type Better, all 4 of these categories (fields) must be met:
TopStock = BEST
Cat2=yes
Cat3=yes
Cat5=yes

In the cases where a business does NOT meet the criteria for their desired Type, I'd like to list why. So for example, if this business who wants to be Better lists the following:
TopStock = GOOD
Cat2=yes
Cat3=no
Cat5=yes
I'd like a variable to list the field names of the factors they failed on:
Variable = "TopStock,Cat3"

I've included my code (so far) below - skip down to the Hash Tags (####) for the part about this question:
Code:
Option Compare Database

Public Sub TypeAchieved()
Dim rs As DAO.Recordset
Dim x As Long
Dim TypeAchieved As String
Dim Met As String
Dim Miss As String

'*!*!*!Field "TYPEAIMED" is the goal. TypeAchieved a.k.a. Earned is where they're at currently*!*!*
'%&%&% skip down to ######s for question (this area for bkground) %&%&%&%&%&

    '//Table2 is now a copy of signups, plus "earned" and "missing" as extra fields
    Set rs = CurrentDb.OpenRecordset("Table2", dbOpenTable)
        
    Do While Not rs.EOF
        With rs
            TypeAchieved = ""

    '//Type BEST
            If TypeAchieved = "" Then
                x = 0
                x = x + IIf(rs!TopStock = "BEST", 1, 0)
                x = x + IIf(rs!Cat2 = "50plus", 1, 0)
                x = x + IIf(rs!Cat3= "yes", 1, 0)
                x = x + IIf(rs!Cat4= "yes", 1, 0)
                x = x + IIf(rs!Cat5= "yes", 1, 0)
                x = x + IIf(rs!Disp1 = "yes" Or rs!Disp2 = "yes" Or rs!Disp3 = "yes", 1, 0)
                If x = 6 Then '////this should be the number of factors above
                    TypeAchieved = "BEST1" 'corr. to BEST
                End If
            End If
    
    '//Type BETTER
            If TypeAchieved = "" Then
                x = 0
                x = x + IIf(rs!TopStock = "BETTER" Or rs!TopStock = "ALMOST BEST" Or rs!TopStock = "BEST", 1, 0)
                x = x + IIf(rs!Cat2= "50plus", 1, 0)
                x = x + IIf(rs!Cat4 = "yes", 1, 0)
                x = x + IIf(rs!cAT5 = "yes", 1, 0)
                If x = 4 Then
                    TypeAchieved = "BETTER1" 'corr. to Better
                End If
            End If
    
    '//Type GOOD
            If TypeAchieved = "" Then
                x = 0
                x = x + IIf(rs!Cat2 = "25-49" Or rs!Cat2 = "50plus", 1, 0)
                x = x + IIf(rs!Cat4 = "yes", 1, 0)
                x = x + IIf(rs!Cat5 = "yes", 1, 0)
                If x = 3 Then
                    TypeAchieved = "GOOD1" 'corr. to Good
                    Else: TypeAchieved = "N/A"
                End If
            End If
                            
 '######## how achievement compares to aim ##############
            If rs!TYPEAIMED = "BEST" Then
                If TypeAchieved = "BEST1" Then  'If TypeAchieved is any lower than BEST1, it doesn't count
                    Met = TypeAchieved
                    Else: Met = "notbest"
                End If
            End If
            
            If rs!TYPEAIMED = "Better" Then
                If TypeAchieved = "BETTER1" Then  'If TypeAchieved is any lower than BETTER1, it doesn't count
                    Met = TypeAchieved
                    Else: Met = "notbetter"
                End If
            End If
            If rs!TYPEAIMED = "Good" Then
                If TypeAchieved = "GOOD1" Then  'If TypeAchieved is any lower than GOOD1, it doesn't count
                    Met = TypeAchieved
                    Else: Met = "notgood"
                End If
            End If
 
'######    'If Not Met = TypeAchieved Then
'######         'Miss = list fieldnames that were value 0
                    
            .Edit
            !Earned = IIf(TypeAchieved = "", Null, TypeAchieved)
'#######  ' !Missing = IIf(Miss = "", Null, Miss)
            
            .Update
            .MoveNext
        End With
    Loop
    
    rs.Close
    Set rs = Nothing

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top