VBA SQL CREATE TABLE with String for table name

AccessNub

Registered User.
Local time
Today, 05:53
Joined
Jul 22, 2006
Messages
71
I am usually able to figure this stuff out on my own nowadays, but I am really stuck, so I defer to your knowledge.

I am trying to use .Execute to run SQL to create a table in the database, the problem is that the table name is a string value that was determined through code.

If I don't use a string for the table name, I don't get an error, but with a string I am getting a syntax error that I can't seem to fix. Here is an example:

Code:
StrTablename = "Autopay Accounts"
Currentdb.Execute "CREATE TABLE " & StrTablename & " (RecordStatus varchar(2))"

Anyone have an idea? I think it's a problem with quote's somewhere.
 
Try without the inadvisable space in the table name. ;)
 
Use:
Code:
StrTablename = "Autopay Accounts"
Currentdb.Execute "CREATE TABLE [" & StrTablename & "] (RecordStatus varchar(2))"
or as pbaldy says, remove the space from the name
 
Oh man, that is usually the first thing I do, I am pretty anti-spaces, but I was trying to follow the format that the other tables were named.

Thanks, no spaces works
 
No problem. The method with brackets would work, but in my mind option 1 is not using them to begin with.
 
Totally agree, no spaces is the best option.
The table I most had issues with in the past had an '&' in its name.
 

Users who are viewing this thread

Back
Top Bottom