Question : Can I use DoCmd.CopyObject to copy a form with VBA code from one database to another?

I'm using Access 2007, creating Access 2002-2003 databases (.mdb)

I am trying to create a small database to be used as sort of a "Service Pack" for an existing database.  It has some forms that have been modified since the existing database was deployed, and it's only job is to export the forms from this database to the existing database, overwriting the existing forms.

I'm using the DoCmd.CopyObject method.  The attached code is an excerpt, so that's why things aren't Dim'ed and whatnot.  But my problem's definitely not in syntax.  Everything works find for forms that have no attached VBA code.  But when I try to use DoCmd.CopyObject for forms with associated VBA code, I get an error.

If the form previously had VBA code attached to it, I get:
"The Microsoft Access database engine could not find the object 'Attributes'.  Make sure the object exists and that you spell its name and the path name correctly."

If the form did not have VBA code before, but does now, I get:
" cannot complete this operation.  You must stop the code and try again."

When I open the database these were supposed to be copied to, I find that forms that previously had VBA code have been deleted, but the old VBA code still exists.  If there was no VBA code associated previously, the form is overwritten with the new form, but the VBA code does not transfer over.

So it seems like the CopyObject command is getting half-way through by deleting the previous form, but then getting caught up on deleting the previous VBA code, or it over-writes the form and sees there was no previous VBA, but gets caught up trying to attach VBA code to the new form?

Does anyone know a way around this besides setting up the database right the first time so that I could just plop a new program end on top instead of going through all this trouble?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Set rs = CurrentDb.OpenRecordset(sqlIndexTable)
    With rs
        'CHECK: recordset is empty
        If .RecordCount = 0 Then
            Call MsgBox( _
                "The index table, " & sqlIndexTable & " is empty." & vbNewLine & _
                "Plesae define instructions for the update and try again." _
              , vbOKOnly _
              , "Index Table Empty" _
              )
            GoTo Exit_Handler
        End If
        
        .MoveFirst
        Do While Not .EOF
            Select Case LCase(!Action)
                Case "copyobject"
                    Call DoCmd.CopyObject(sDestDatabase, !ObjectName, !ObjectType, !ObjectName)
                    Call WriteToLog( _
                        "Copied Object '" & !ObjectName & "' (Type " & !ObjectType & ") to " & sDestDatabase _
                      )
            End Select
            .MoveNext
        Loop
    End With

Answer : Can I use DoCmd.CopyObject to copy a form with VBA code from one database to another?

Use the docmd.TransferDatabase method instead.
Random Solutions  
 
programming4us programming4us