Using a variable in a "DoCmd" VBA argument

G

George Walsh

I have written an event procedure to run when a user clicks a command button
in a form. The button press is supposed to engage the action to make a copy
of a specified table.

The form exposes a list box showing all the table names in the current
database to the user, and it allows the user to specify both the present
name of the table to be copied and the new name of the copied table.

The event procedure in VBA declares two variant variables and performs the
DLookup function to obtain the specified names. This part is tested and
works.

I next want to evoke the DoCmd.CopyObject function which includes the names
of the "DestinationDatabase" and "NewName" as arguments; however, I don't
know how to make the call work unless I actually type the table names
enclosed by quotation marks.

For example: this works ...
DoCmd.CopyObject "ClientSourceData", "ClientSourceData_12-31-1999",
acTable, "ClientSourceData"

Is there any way to get the DoCmd to accept the table names by passing the
table names from the user-selected variables rather than by typing literal
names in quotes in the DoCmd call?

Or, is there another way to accomplish what I am trying to do? I can't find
a solution and would appreciate some suggestions. Thanks.
 
A

Allen Browne

If you have the names in controls on the form, you can refer directly to
those instead of the literals in quotes.

For example if you have text boxes named Text1, Text2, and Text3:
DoCmd.CopyObject Me.Text1, Me.Text2, acTable, Me.Text3
 
A

Allen Browne

If you have the names in controls on the form, you can refer directly to
those instead of the literals in quotes.

For example if you have text boxes named Text1, Text2, and Text3:
DoCmd.CopyObject Me.Text1, Me.Text2, acTable, Me.Text3
 
G

George Walsh

Thanks very much! That works.


Allen Browne said:
If you have the names in controls on the form, you can refer directly to
those instead of the literals in quotes.

For example if you have text boxes named Text1, Text2, and Text3:
DoCmd.CopyObject Me.Text1, Me.Text2, acTable, Me.Text3
 
G

George Walsh

Thanks very much! That works.


Allen Browne said:
If you have the names in controls on the form, you can refer directly to
those instead of the literals in quotes.

For example if you have text boxes named Text1, Text2, and Text3:
DoCmd.CopyObject Me.Text1, Me.Text2, acTable, Me.Text3
 

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

Top