compile error: For control variable already in use

  • Thread starter gmazza via AccessMonster.com
  • Start date
G

gmazza via AccessMonster.com

Hey there,
I am trying to open a recordset twice for the same table and I am getting
this error.
I named the recordset's different names but still get this error.
This is my code, more or less, just the parts that matter:

Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset

intCount = 0
activityCount = 50

Set rs1 = CurrentDb.OpenRecordset("Criteria")
If rs1.RecordCount > 0 Then
rs1.MoveFirst
Do While Not rs1.EOF

For Each fld In rs1.Fields

If fld.Name = "ClinicalTrialId" Then
If fld.Value = 1 Then
Else
Exit For
End If
End If

Set rs = CurrentDb.OpenRecordset("Criteria")
If rs.RecordCount > 0 Then
rs.MoveFirst
Do While Not rs.EOF

For Each fld In rs.Fields

If fld.Name = "CriteriaValue" Then
If IsNull(fld.Value) Then
Exit For
Else
intCount = intCount + 1
With Me.Controls("Label" & intCount)
.Caption = fld.Value
.Visible = True
End With
End If
End If

If fld.Name = "CriteriaText" Then
If IsNull(fld.Value) Then
Exit For
Else
activityCount = activityCount + 1
With Me.Controls("Text" & activityCount)
.Visible = True
End With
End If
End If

Next fld
rs.MoveNext
Loop

End If

Next fld
rs1.MoveNext
Loop

End If

' Clean up objects.
Set fld = Nothing
rs.Close
Set rs = Nothing

Set fld = Nothing
rs1.Close
Set rs1 = Nothing


Any help is appreciated!
 
R

RonaldoOneNil

It is nothing to do with the recordset variables. The error tells you exactly
what the problem is. You have two For Each loops, one nested inside the other
but you are using the same variable (fld) for both.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top