Hi guys,
I'm new on this forum and with Access VBA, and I've been struggling with a VBA function for almost 2 days now. What I have is a query based on an union query, and I would like to categorize the records in the union query according to 24 criteria. What I have come up with is a VBA function that checks the records and assigns certain category codes that I have defined. The purpose is to then have the category codes check against time targets for each one of them.
The trouble is that once the code runs, it is stuck in an endless loop, and I cannot seem to make it work.
Here's the code. At first there was a Select Case statement, but I've changed it to multiple If statements. I have no problem reverting to the Select Case statement once the loop issue is solved:
Any ideas would be much appreciated, as I am due with this on Monday (so no pressure
).
Also, is there a way to do this using SQL rather than VBA, given the multiple conditions (24)?
Thank you all for your input,
karatekid
I'm new on this forum and with Access VBA, and I've been struggling with a VBA function for almost 2 days now. What I have is a query based on an union query, and I would like to categorize the records in the union query according to 24 criteria. What I have come up with is a VBA function that checks the records and assigns certain category codes that I have defined. The purpose is to then have the category codes check against time targets for each one of them.
The trouble is that once the code runs, it is stuck in an endless loop, and I cannot seem to make it work.
Here's the code. At first there was a Select Case statement, but I've changed it to multiple If statements. I have no problem reverting to the Select Case statement once the loop issue is solved:
Code:
Function CatCode(vTargetGroup, vGroupByType, vActionAnsType, vAdminAns, vComplType)
Dim db As DAO.Database
Dim tdf As DAO.Recordset
Set db = CurrentDb
Set tdf = db.OpenRecordset("Union_CC_Written")
With tdf
vTargetGroup = tdf("TargetGroup").Value
vGroupByType = tdf("GroupByType").Value
vActionAnsType = tdf("Action").Value
vAdminAns = tdf("AdminAns").Value
vComplType = tdf("Complaint Type EN").Value
End With
On Error GoTo 0
If tdf.RecordCount = 0 Then Exit Function
tdf.MoveFirst
Do Until tdf.EOF = True
If tdf("LogonName").Value = "null" Then
CatCode = "Pending"
End If
If vTargetGroup = "voucher" Then
CatCode = "IllegVoucher"
End If
If vGroupByType = "Residential" And vTargetGroup = _
"Consumer - Internal use only" And vActionAnsType = "STATISTICA" And _
vAdminAns = "Nefondata" And vComplType = "Consumer - Internal use only" Then
CatCode = "ConsInternal_N(STAT)"
End If
If vGroupByType = "Residential" And vTargetGroup = _
"Consumer - Internal use only" And vAdminAns = "Fondata" And vComplType = _
"Consumer - Internal use only" Then
CatCode = "ConsInternal_F"
End If
If vGroupByType = "Residential" And vTargetGroup = "taxare" And _
vActionAnsType = "WRITTEN" And vAdminAns = "Fondata" Then
CatCode = "ConsCharging_W_F"
End If
If vGroupByType = "Residential" And vTargetGroup = "taxare" And _
vActionAnsType = "WRITTEN" And vAdminAns = "Nefondata" Then
CatCode = "ConsCharging_W_N"
End If
If vGroupByType = "Residential" And vTargetGroup = "taxare" And vAdminAns = _
"Fondata" Then
CatCode = "ConsCharging_CC_F"
End If
If vGroupByType = "Residential" And vTargetGroup = "taxare" And vAdminAns = _
"Nefondata" Then
CatCode = "ConsCharging_CC_N"
End If
If vGroupByType = "Residential" And vTargetGroup = "tehnice" And _
vActionAnsType = "WRITTEN" Then
CatCode = "ConsTech_W"
End If
If vGroupByType = "Residential" And vTargetGroup = "diverse" And _
vActionAnsType = "WRITTEN" Then
CatCode = "ConsOther_W"
End If
If vGroupByType = "Residential" And vTargetGroup = "diverse" Then
CatCode = "ConsOther_CC"
End If
If vGroupByType = "Residential" And vTargetGroup = "tehnice" Then
CatCode = "ConsTech_CC"
End If
If vGroupByType = "Residential" And vActionAnsType = "OTHER" Then
CatCode = "ConsToS_OTHER"
End If
If vGroupByType = "Residential" And vActionAnsType = "STATISTICA" Then
CatCode = "ConsToS_STAT"
End If
If vGroupByType = "Corporate" And vTargetGroup = _
"Corporate - Internal use only" And vComplType = _
"Corporate - Internal use only" Then
CatCode = "Corp_internal"
End If
If vGroupByType = "Corporate" And vTargetGroup = "diverse" And _
vActionAnsType = "WRITTEN" Then
CatCode = "CorpOther_W"
End If
If vGroupByType = "Corporate" And vTargetGroup = "taxare" And _
vActionAnsType = "WRITTEN" Then
CatCode = "CorpCharging_W"
End If
If vGroupByType = "Corporate" And vTargetGroup = "tehnice" And _
vActionAnsType = "WRITTEN" Then
CatCode = "CorpTech_W"
End If
If vGroupByType = "Corporate" And vTargetGroup = "diverse" Then
CatCode = "CorpOther_CC"
End If
If vGroupByType = "Corporate" And vTargetGroup = "taxare" Then
CatCode = "CorpCharging_CC"
End If
If vGroupByType = "Corporate" And vTargetGroup = "tehnice" Then
CatCode = "CorpTech_CC"
End If
If vGroupByType = "Corporate" And vActionAnsType = "OTHER" Then
CatCode = "CorpToS_OTHER"
End If
If vGroupByType = "Corporate" And vActionAnsType = "STATISTICA" Then
CatCode = "CorpToS_STAT"
End If
If vTargetGroup = "Resending a written answer" And vActionAnsType = _
"RETRANSMIS" And vComplType = "Resending a written answer" Then
CatCode = "RetryAns_R"
End If
If vTargetGroup = "Resending a written answer" And vActionAnsType = _
"WRITTEN" And vComplType = "Resending a written answer" Then
CatCode = "RetryAns_R"
End If
tdf.MoveNext
Loop
MsgBox "Query executed", vbInformation
tdf.Close
Set db = Nothing
Set tdf = Nothing
End Function

Also, is there a way to do this using SQL rather than VBA, given the multiple conditions (24)?
Thank you all for your input,
karatekid
Last edited: