Solved Select Into Query with varible issue (1 Viewer)

silentwolf

Active member
Local time
Today, 10:37
Joined
Jun 12, 2009
Messages
575
Hi guys,

unfortunatelly I can not figure out what I am missing or doing wrong with following SQL String and it feels like I tried all but am not able to get it run without errors.

Code:
Public Sub CreateFromExistingTable(selectTable As String, intoTable As String)
    Dim strSQL As String
          
    strSQL = "SELECT  " & selectTable & ".* INTO " & intoTable & "FROM " & selectTable & """"
    
    CurrentDb.Execute strSQL, dbFailOnError
                
End Sub

I like to Create a Table from an Existing Table ..

Can someone please check my SQL String?

Many thanks

Albert
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:37
Joined
Sep 21, 2011
Messages
14,326
Have you thought to Debug.Print the sql string?

A make table query comes out as

Code:
SELECT * INTO Test
FROM Table1;
 

silentwolf

Active member
Local time
Today, 10:37
Joined
Jun 12, 2009
Messages
575
HI Gasman,

yes I did debug.print
and if I run SELECT * Into Test
FROM Table1;

it works..
but I would like to have Test and Table1 as varibles so I can use the same query for different tables .(
 

ebs17

Well-known member
Local time
Today, 19:37
Joined
Feb 7, 2020
Messages
1,949
Code:
strSQL = "SELECT S.* INTO " & intoTable & " FROM " & selectTable & " AS S"
Using a table alias makes things clearer because the name of the table can only be used in one place.
You were missing a space before FROM.
With so many DoubleQuotes at the end I'm undecided; I prefer simple spellings.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:37
Joined
Sep 21, 2011
Messages
14,326
You can still do that using the syntax as above.
 

silentwolf

Active member
Local time
Today, 10:37
Joined
Jun 12, 2009
Messages
575
Hi Gasman,

hmm not sure if I am honest.. I tried with debuging but was not able to spot it.. :( Sorry
 

ebs17

Well-known member
Local time
Today, 19:37
Joined
Feb 7, 2020
Messages
1,949
Do you even need the alias?
Of course not with one table in the source query. However, the table alias is more efficient than using the table name multiple times. If more than one table is used, the question no longer arises.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:37
Joined
Sep 21, 2011
Messages
14,326
Hi Gasman,

hmm not sure if I am honest.. I tried with debuging but was not able to spot it.. :( Sorry
Well the tablename would have been concatenated with the From?
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:37
Joined
Sep 21, 2011
Messages
14,326
Hi Gasman,
sorry for late responds.. was not on

No there was no concatination...
Really?

Code:
strSQL = "SELECT  " & selectTable & ".* INTO " & intoTable & "FROM " & selectTable & """"
would show as below with tablenames called as their literal variable names, if you debug.printed strSQL

Code:
SELECT  selectTable.* INTO intoTableFROM selectTable

and that should show that From error, regardless of whether the actual syntax is correct or not.
 

ebs17

Well-known member
Local time
Today, 19:37
Joined
Feb 7, 2020
Messages
1,949
As an addition: To be on the safe side, you could mask the table names with brackets if there could be spaces or special characters.
 

silentwolf

Active member
Local time
Today, 10:37
Joined
Jun 12, 2009
Messages
575
In my database are no spaces in table names but I am aware of that thanks for pointing it out Eberhard.

@Gasman..
it was all performed via vba and no concatinations in a form..

And yes there was also missing a space lol but I tried so many different things got stuck ..

Anyway it all works now thanks to all of you guys helping out!

Cheers
 

silentwolf

Active member
Local time
Today, 10:37
Joined
Jun 12, 2009
Messages
575
Btw is there now a button somehwere to close the treat or does it stay open?
 

Users who are viewing this thread

Top Bottom