0

I have been running into trouble executing SQL code in VBA Access when I refer to certain Table names.

For example,

INSERT INTO TempTable (ClientName) SELECT DISTINCT 1_1_xlsx.ClientName FROM 1_1_xlsx'<--does not work

The code works fine when I changed the Table name from 1_1_xlsx to Stuff.

INSERT INTO TempTable (ClientName) SELECT DISTINCT Stuff.ClientName FROM Stuff '<--works

I have no idea why the first query results in a syntax error and the second code is runs fine even when they refer to the same thing. I suspect it should be the naming conventions but I could not find any concrete answers.

Also, are there any ways that I could use 1_1_xlsx as my table name? Or am I just writing my query wrong?

2
  • 2
    If you have trouble with names, always start by bracketing them ([1_1_xlsx]) Commented May 31, 2018 at 7:31
  • Could be that xslx confuses the SQL parser to believe that this about some linked Excel file. Commented May 31, 2018 at 7:32

3 Answers 3

1

try this:

INSERT INTO TempTable (ClientName) SELECT DISTINCT [1_1_xlsx].ClientName FROM [1_1_xlsx]
Sign up to request clarification or add additional context in comments.

Comments

0

In many SQL based databases you can't have a table name or field name that starts with a number.

I suspect this is the underlying reason for your problem. Although Access will allow it, I have seen it cause problems in the past.

Comments

0

The problem is the number at the beginning of the table name. That is bad -- because it confuses the parser.

This is a bad table name, but SQL allows you to define table aliases. And, in this case, you don't even need to repeat the table name. So, here are two simple solutions:

INSERT INTO TempTable (ClientName)
    SELECT DISTINCT ClientName
    FROM 1_1_xlsx;

Or:

INSERT INTO TempTable (ClientName)
    SELECT DISTINCT t.ClientName
    FROM 1_1_xlsx as t

There is no reason to use the complete table name as an alias. That just makes the query harder to write and to read.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.