Object invalid or no longer set on CopyObject method

T

tig

I thought you could use the CopyObject method on anything but modules,
but I continue to get this error when looping though my dbs. I've
tried TransferDatabase and get the same result

Any ideas?

TIA

Here's my code:

Sub copy_forms()

Dim x
Dim dbnames(1 To 9) As String
dbnames(1) = "C:\db1.mdb"
dbnames(2) = "C:\db2.mdb"
dbnames(3) = "C:\db3.mdb"
dbnames(4) = "C:\db4.mdb"
dbnames(5) = "C:\db5.mdb"
dbnames(6) = "C:\db6.mdb"
dbnames(7) = "C:\db7.mdb"
dbnames(8) = "C:\db8.mdb"
dbnames(9) = "C:\db9.mdb"

For Each x In dbnames
DoCmd.CopyObject x, "Main", acForm, "Main"
'DoCmd.TransferDatabase acExport, "Microsoft Access", x, acForm,
"Main", "Main"
Next

Close
MsgBox "Form Copies Complete"

End Sub
 
S

Stefan Hoffmann

hi,
Sub copy_forms()

Dim x
Dim dbnames(1 To 9) As String

For Each x In dbnames
You can only enumerate Collections, not arrays.

Use

Dim Index As Long
For Index = LBound(dbnames()) To UBound(dbnames())
Next Index

or

Dim x As Variant
Dim dbnames As Collection
Set dbnames = New Collection
dbnames.Add "nam"
For Each x In dbnames

Despite that, i'm not sure whether x should be declared as Object instead.



mfG
--> stefan <--
 
T

tig

Stefan said:
hi,

You can only enumerate Collections, not arrays.

Use

Dim Index As Long
For Index = LBound(dbnames()) To UBound(dbnames())
Next Index

or

Dim x As Variant
Dim dbnames As Collection
Set dbnames = New Collection
dbnames.Add "nam"
For Each x In dbnames

Despite that, i'm not sure whether x should be declared as Object instead.



mfG
--> stefan <--

Stefan

Thanks for the suggestion. Unfortunately I got the same error using
either of your suggestions as well. I did test mine out with multiple
macros and that seems to work. Which leads me to believe it's related
to forms.

It doesn't make sense though that Microsoft would note that you use
this method with forms in their help file.

Any new ideas??

Thanks again.
 
S

Stefan Hoffmann

hi,
Thanks for the suggestion. Unfortunately I got the same error using
either of your suggestions as well.
What error message and number do you get?


mfG
--> stefan <--
 
T

tig

Stefan said:
hi,

What error message and number do you get?


mfG
--> stefan <--

Run-time error '3420' Object invalid or no longer set. Thanks again
for taking a look.
 
S

Stefan Hoffmann

hi,
Run-time error '3420' Object invalid or no longer set. Thanks again
for taking a look.
There is a single Close in your code. What should be closed?


mfG
--> stefan <--
 
T

tig

hi,


mfG
--> stefan <--

Hmmm...I think originally I was thinking I needed close each database
after I updated it. It's not in the loop anymore. I should probably
just get rid of that. Do you think that's what my problem is?
 
S

Stefan Hoffmann

hi,
Hmmm...I think originally I was thinking I needed close each database
after I updated it. It's not in the loop anymore. I should probably
just get rid of that. Do you think that's what my problem is?
Yup. Close is a method of many objects, but you have call it
Object.Close. In your code there is no Object. part.


mfG
--> stefan <--
 

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

Similar Threads


Top