VBA SQL - Run-time error 3134 - Syntax Error in INSERT INTO statement
This is running MS Access 2003
I thought it was a fairly strait forward SQL statment, but apparently not?
Here is the code - a lot of different things going on here, but I'll highlight the portion with the error:
This is the actual SQL that is being produced which is giving the syntax error:
My suspicion is that I'm doing something wrong with the part where I am wanting to add text. Basically, what I am wanting to do is include the table name for the table being appended FROM in the last column.
I tried testing this on SQL Server, and got no errors in just checking it. Of course, it won't run, b/c it is based on Access local tables, not SQL Server tables.
Any thoughts/suggestions?
Oh, and the rgxValidate is using a Function from here:
--
"If to err is human, then I must be some kind of human!" -Me
This is running MS Access 2003
I thought it was a fairly strait forward SQL statment, but apparently not?
Here is the code - a lot of different things going on here, but I'll highlight the portion with the error:
Code:
Option Compare Database
Option Explicit
Private Sub CombineTdfs()
Dim db As DAO.Database
Dim strSqlDel As String
Dim strSqlApnd As String
Dim strSqlFieldsTo As String 'Fields to append to
Dim strSqlFieldsFrom As String 'Fields to append from
Set db = CurrentDb
Dim tdf As TableDef
Dim fld As Field
Dim bolDropCol As Boolean
Dim bolSSN As Boolean 'test to see if table contains ssn field "SS#"
bolDropCol = False
For Each tdf In db.TableDefs
bolSSN = False
strSqlFieldsTo = "Date, [Case Number], [Plaintiff Last Name]," & _
" MI, [Plaintiff First Name]," & _
" County, State, [Plaintiff Counsel #1]," & _
" [Plaintiff Counsel #2], Defendant," & _
" [House #], [Street Name], City, Zip"
strSqlFieldsFrom = "t.Date, t.[Case Number], t.[Plaintiff Last Name]," & _
"t.MI, t.[Plaintiff First Name], " & _
"t.County, t.State, t.[Plaintiff Counsel #1]," & _
"t.[Plaintiff Counsel #2], t.Defendant, " & _
"t.[House #], t.[Street Name], t.City, t.Zip"
strSqlDel = "ALTER TABLE [" & tdf.Name & "]"
strSqlApnd = "INSERT INTO [tblALL] "
For Each fld In tdf.Fields
If Len(fld.Name) = 3 Then
If rgxValidate(fld.Name, "\w\d\d") Then
bolDropCol = True
strSqlDel = strSqlDel & " DROP COLUMN [" & fld.Name & "]"
End If
If fld.Name = "SS#" Then
bolSSN = True
End If
End If
Next fld
If bolDropCol = True Then
db.Execute strSqlDel
bolDropCol = False
End If
If bolSSN = True Then
strSqlFieldsTo = strSqlFieldsTo & ", [SS#], [Court District]"
strSqlFieldsFrom = strSqlFieldsTo & ", t.[SS#], '" & tdf.Name & "'"
Else
strSqlFieldsTo = strSqlFieldsTo & ", [Court District]"
strSqlFieldsFrom = strSqlFieldsFrom & ", '" & tdf.Name & "'"
End If
strSqlApnd = strSqlApnd & "(" & strSqlFieldsTo & ") SELECT " & strSqlFieldsFrom & _
" FROM [" & tdf.Name & "] t"
[HIGHLIGHT]db.Execute strSqlApnd[/HIGHLIGHT]
Next tdf
End Sub
This is the actual SQL that is being produced which is giving the syntax error:
Code:
?strSqlApnd [GREEN]'Command in Immediate Window to show what the variable is currently storing[/GREEN]
INSERT INTO [tblALL] (Date, [Case Number], [Plaintiff Last Name], MI, [Plaintiff First Name], County, State, [Plaintiff Counsel #1], [Plaintiff Counsel #2], Defendant, [House #], [Street Name], City, Zip, [Court District]) SELECT t.Date, t.[Case Number], t.[Plaintiff Last Name], t.MI, t.[Plaintiff First Name], t.County, t.State, t.[Plaintiff Counsel #1], t.[Plaintiff Counsel #2], t.Defendant, t.[House #], t.[Street Name], t.City, t.Zip, [COLOR=WHITE][HIGHLIGHT BLUE]'Alabama Middle'[/HIGHLIGHT][/COLOR WHITE] FROM [Alabama Middle] t
My suspicion is that I'm doing something wrong with the part where I am wanting to add text. Basically, what I am wanting to do is include the table name for the table being appended FROM in the last column.
I tried testing this on SQL Server, and got no errors in just checking it. Of course, it won't run, b/c it is based on Access local tables, not SQL Server tables.
Any thoughts/suggestions?
Oh, and the rgxValidate is using a Function from here:
--
"If to err is human, then I must be some kind of human!" -Me