Hi,
I'm fairly new to programming in Access with VBA. I have a large bit of code I'm working with to create an overall document rating for several documents that are used to rate certain standards for Child Care facilities. Most of it seems to be working except where I'm attempting to count the number of records returned by a query and use that count to run through a For..Next loop. Here is the code:
Code:
strSQLSelCenter = "SELECT MaxRatingPerYear.state_name, (MaxRatingPerYear.center_rating) AS RetCenter_family, (MaxRatingPerYear.tracking_date)AS CenterOriginDate " & _
"FROM MaxRatingPerYear " & _
"WHERE (((MaxRatingPerYear.state_name)='" & thisState_Name & "') AND ((MaxRatingPerYear.comp_id)='" & thisComp_ID & "') AND ((MaxRatingPerYear.tracking_date)<=" & iYear & ") " & _
"AND ((MaxRatingPerYear.center_rating)<>0));"
Set rsstrSQLSelCenter = db.OpenRecordset(strSQLSelCenter)
iDocNum = rsstrSQLSelCenter.Fields.Count
If iDocNum > 0 Then
For CtrCount = 0 To iDocNum
Set CurrentUberRat = rsstrSQLSelCenter.Fields("RetCenter_family")
Select Case CurrentUberRat
Case 1
If thisRetCenter_family > 2 Then
thisRetCenter_family = thisRetCenter_family
Else
thisRetCenter_family = CurrentUberRat
End If
Case 2
If thisRetCenter_family > 2 Then
thisRetCenter_family = thisRetCenter_family
ElseIf thisRetCenter_family = 1 Then
thisRetCenter_family = thisRetCenter_family
Else
thisRetCenter_family = CurrentUberRat
End If
Case 3
thisRetCenter_family = CurrentUberRat
Case 4
thisRetCenter_family = CurrentUberRat
rsstrSQLSelCenter.MoveLast
Case 0
thisRetCenter_family = thisRetCenter_family
End Select
Next CtrCount
This code actually works well for about 295 record creations then it gets a "Run-time error '3021': No current record."
I have narrowed down the problem to being the rs.fields.count statement returning a larger count number than the query actually returns. For example, a query that returns 1 record may show a record count of 3. I have looked into using DCount instead but cannot see a way to make it work when the query uses variable values from this VBA module.
This is a project I've inherited and have reached the end of my knowledge to fix the problems that existed in the original code. If I can get this last piece figured out I think it will finally be functional. Anny help would be appreciated in getting this count issue resolved.
Thanks!