G
Guest
it possible to work with two recordsets? I keep getting the error message
"Object variable or With block variable not set" for the variables strCtl2
and strCtl3 but not strCtl1..... they appear to me to be set. One of you
Access lords will probably spot the problem in the code below, which I've
been staring at far too long - thanks for your help!
Public Sub CreateFieldValues(strRecSource1, strRecSource2, strCtl1, strCtl2,
strCtl3 As String)
On Error Resume Next
Dim db As DAO.Database
Dim rst1, rst2 As DAO.Recordset
Dim strFieldValue As String
Set db = DBEngine(0)(0)
Set rst1 = db.OpenRecordset(strRecSource1)
If rst1.EOF And rst1.BOF = True Then
Exit Sub
End If
' loop trough the records and capture the data value, insert into new
record
Set rst2 = db.OpenRecordset(strRecSource2)
While Not rst1.EOF
strFieldValue = strCtl1
With rst2
!strCtl2 = strFieldID
!strCtl3 = strFieldValue
End With
rst1.MoveNext
rst2.MoveNext
Wend
Set rst1 = Nothing
Set rst2 = Nothing
Set db = Nothing
End Sub
this is then called when a button on a form is clicked:
Call CreateFieldValues("Current Agency Contact", "New Agency Contact",
"AC_Code", "CYCode", "AC_Code")
"Object variable or With block variable not set" for the variables strCtl2
and strCtl3 but not strCtl1..... they appear to me to be set. One of you
Access lords will probably spot the problem in the code below, which I've
been staring at far too long - thanks for your help!
Public Sub CreateFieldValues(strRecSource1, strRecSource2, strCtl1, strCtl2,
strCtl3 As String)
On Error Resume Next
Dim db As DAO.Database
Dim rst1, rst2 As DAO.Recordset
Dim strFieldValue As String
Set db = DBEngine(0)(0)
Set rst1 = db.OpenRecordset(strRecSource1)
If rst1.EOF And rst1.BOF = True Then
Exit Sub
End If
' loop trough the records and capture the data value, insert into new
record
Set rst2 = db.OpenRecordset(strRecSource2)
While Not rst1.EOF
strFieldValue = strCtl1
With rst2
!strCtl2 = strFieldID
!strCtl3 = strFieldValue
End With
rst1.MoveNext
rst2.MoveNext
Wend
Set rst1 = Nothing
Set rst2 = Nothing
Set db = Nothing
End Sub
this is then called when a button on a form is clicked:
Call CreateFieldValues("Current Agency Contact", "New Agency Contact",
"AC_Code", "CYCode", "AC_Code")