Copying Tables To A Newly Created Database (1 Viewer)

plengeb

New member
Local time
Today, 05:53
Joined
Sep 28, 2011
Messages
9
I am running a program in Access 2016. It creates a new database and copies objects into the new database.

When it runs the below command:
DoCmd.CopyObject sampledbfile, , acTable, "tblk_AuditTypes"

Access displays Run-time error 3049. "Cannot open database 'tblk_AuditTypes'. It may not be a database that your application recognizes, or the file may be corrupt."

It is a small table that I can open manually. It has only three entries. The new database gets corrupt, which I think causes the error.

I have not had the issue in previous versions of Access. Does anyone know why this is happening?

Thanks for any help you can provide.

Bill
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:53
Joined
Feb 19, 2002
Messages
43,223
This is the definition from the help entry:

expression. CopyObject( ** DestinationDatabase, ** NewName, ** SourceObjectType, ** SourceObjectName )

Perhaps you need parentheses.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:53
Joined
Feb 28, 2001
Messages
27,140
Pat, the MSDN example doesn't use parentheses even though the syntax template does.

My question would be this: What is in the variable 'sampledbfile' at the time you execute the command? Because that will be the destination DB name. But it should be the fully qualified name including device, path, name, and file type, or the UNC path, name, and type.

If it happens that the destination database variable is blank, then this command essentially would try to copy table tblk_AuditTypes onto itself. This command CAN be used to make a copy of a table in the same database to a differently named table, but I'm not sure this will work for same database/same object name.

The problem is that the error message you copied says the database name is tblk_AuditTypes, which is actually your SOURCE table name if your description is accurate. So is there a chance that the sampledbfile variable isn't what you think it is?

You have not posted here very much so I don't know your level of expertise. If I am shooting a bit low, forgive me. In order to determine the value of your variable:

Put a breakpoint in the code by opening a code window and clicking in the left margin of the VBA line that contains the DoCmd.CopyObject. You should see a dark red dot appear. That is your breakpoint.

Now let that code run. When it takes the breakpoint, the code window will pop up and that line will be highlighted. Hover your mouse's cursor over the variables on that line and you will see their values. Or you could open the Locals window and see the value of all declared variables. Or you could open the Immediate window and use Debug.Print to see the contents of a given variable.

When done, you can click on that dot to cancel the breakpoint, then use items from the Debug menu bar dropdown to resume your code execution.
 

JHB

Have been here a while
Local time
Today, 11:53
Joined
Jun 17, 2012
Messages
7,732
Just tried your code in MS-Access 2010 without problem, (I don't have 2016 version).
It creates a new database and copies objects into the new database.
Is it created by code?
Try a "Compact & Repair" at the newly created database and the only run the below code.
Code:
DoCmd.CopyObject sampledbfile, , acTable, "tblk_AuditTypes"
.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:53
Joined
Feb 28, 2001
Messages
27,140
JHB's suggestion is valid because a Compact & Repair will improve things for you if the problem is simple corruption. In fact, a C&R will copy everything from the target database to a new one, after which a series of judicious renames and deletions gets rid of the original database. If it works, it is great. (But if it doesn't work, it is not so good. - which is why we always recommend making a copy of your DB BEFORE you do a C&R.) However, even if it works, C&R doesn't fix everything.

My question remains: What is the value of variable 'sampledbfile' when you execute the given command?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:53
Joined
Feb 19, 2002
Messages
43,223
Pat, the MSDN example doesn't use parentheses even though the syntax template does.
The example was also confusing since it looked like there was no space following the CopyObject and the argument list started with a comma implying that the first argument was omitted.
 

Users who are viewing this thread

Top Bottom