Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2006

Tip: Looking for answers? Try searching our database.

Need Coding Advice

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anne - 24 Jan 2006 15:41 GMT
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")
Douglas J Steele - 24 Jan 2006 15:56 GMT
What are you trying to do? Since I have to guess, I'd say that you're hoping
that the following code

        While Not rst1.EOF
            strFieldValue = strCtl1
            With rst2
                !strCtl2 = strFieldID
                !strCtl3 = strFieldValue
            End With
            rst1.MoveNext
            rst2.MoveNext
        Wend

will use whatever values you're passing for strCtl2 and strCtl3 as field
names in the recordset.

First, to change values in a DAO recordset, you need to issue the Edit
method before, and the Update method after.

To use variables as field names, you need to use the Fields collection.

Try:

        While Not rst1.EOF
            strFieldValue = strCtl1
            With rst2
                .Edit
                .Fields(strCtl2) = strFieldID
                .Fields(strCtl3) = strFieldValue
                .Update
            End With
            rst1.MoveNext
            rst2.MoveNext
        Wend

BTW, your parameter declaration probably isn't doing what you think it is.

(strRecSource1, strRecSource2, strCtl1, strCtl2, strCtl3 As String)

declares 5 parameters: 4 as Variants, and 1 (strCtl3) as a string. You can't
"short circuit" declarations in VBA. You probably want:

(strRecSource1 As String, strRecSource2 As String, strCtl1 As String,
strCtl2 As String, strCtl3 As String)

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> 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
[quoted text clipped - 38 lines]
> Call CreateFieldValues("Current Agency Contact", "New Agency Contact",
> "AC_Code", "CYCode", "AC_Code")
Michel Walsh - 24 Jan 2006 16:14 GMT
Hi,

It is possible, sure.

Note that in VBA,

Dim rst1, rst2 As DAO.Recordset

declare rst1 as a Variant, not a DAO.Recordset. Preferable to do it in two
lines:

Dim rst1 AS DAO.Recordset
Dim rst2 As DAO.Recordset

Next, DAO requires that you use the Edit method of  the recordset before
starting writing in it, or AddNew, and it seems it is what you try to do,
if you want to append a new record to its recordset.

Finally, it may be preferable that you use an SQL statement to

INSERT INTO table2(listOfFIelds) SELECT listOfFields FROM table1 WHERE
someCriteria

which turns to be a single line of code,

   CurrentDb.Execute "INSER INTO ... " , dbFailOnERror

and probably perceptibly faster, than looping through recordset to make "n"
append operations.

Hoping it may help,
Vanderghast, Access MVP

> 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
[quoted text clipped - 40 lines]
> Call CreateFieldValues("Current Agency Contact", "New Agency Contact",
> "AC_Code", "CYCode", "AC_Code")
Michel Walsh - 24 Jan 2006 16:19 GMT
Hi,

And as Doug mentioned it, with recordset, indeed, even if I didn't mention
it, you need to call the  Update method when you have finished the Edit, or
Append, for each record you edit/append... if you ever desire to go the
"recordset" way rather than with the SQL way.

Vanderghast, Access MVP

> Hi,
>
[quoted text clipped - 75 lines]
>> Call CreateFieldValues("Current Agency Contact", "New Agency Contact",
>> "AC_Code", "CYCode", "AC_Code")
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.