Ich füge SQL INSERT INTO-Anweisungen in einem VBA-Skript mithilfe von ODBC ein, um Daten aus einem Word-Dokument zu ziehen und es automatisch in eine Zugriffsdatenbank einzufügen.SQL-Fehler, möglicher Newbie-Fehler
Ich habe verifiziert, dass die Daten keine Diskrepanz sind, und ich habe es mit den anderen INSERT INTO-Anweisungen verglichen, die ohne Problem laufen. Diese Anweisungen sind mit Zeichenfolgenvariablen verknüpft, die dann im Befehl cnn.execute verwendet werden.
Der Code zu speichern und meine Variablen auszuführen ist wie folgt:
Sub TransferInstructorInfo()
'Transfer new Instructor Information record to DB
Dim cnn As ADODB.Connection
Dim strConnection As String
Dim InstTable As String
Dim EduTable As String
Dim EmpTable As String
Dim RefTable As String
Dim strPath As String
Dim doc As Word.Document
' Define Doc Variables
Dim Campus As String
Dim DOE As String
Dim Fname As String
Etc...
'Link defined variables to forms in the word doc
Campus = Chr(39) & doc.FormFields("Campus").Result & Chr(39)
DOE = Chr(39) & doc.FormFields("HireDate").Result & Chr(39)
Fname = Chr(39) & doc.FormFields("FirstName").Result & Chr(39)
Mname = Chr(39) & doc.FormFields("MiddleName").Result & Chr(39)
Nname = Chr(39) & doc.FormFields("NickName").Result & Chr(39)
etc...
InstTable = "INSERT INTO Instructors (InstructorID,[First Name],[Middle Initial],[Last Name],Nickname,Campus,[Job Title],Address,City,State,Zip,Phone,DOB,SSN,DOE)" _
& "SELECT " & InstructorID & "," & Fname & "," & Mname & "," & Lname & "," & Nname & "," & Campus & "," & Job & "," & Address & "," & City & "," & State & "," & Zip & "," & Phone & "," & DOB & "," & SSN & "," & DOE & ";"
EmpTable = "INSERT INTO [Employment History] (InstructorID, Company1, CompAdd1, Comp1Supervisor, CompPhone1, Comp1From, Comp1To, Comp1Title, Comp1Description, Company2, CompAdd2, Comp2Supervisor, CompPhone2, Comp2From, Comp2To, Comp2Title, Comp2Description, Company3, CompAdd3, Comp3Supervisor, CompPhone3, Comp3From, Comp3To, Comp3Title, Comp3Description, Company4, CompAdd4, Comp4Supervisor, CompPhone4, Comp4From, Comp4To, Comp4Title, Comp4Description, Company5, CompAdd5, Comp5Supervisor, CompPhone5, Comp5From, Comp5To, Comp5Title, Comp5Description, Company6, CompAdd6, Comp6Supervisor, CompPhone6, Comp6From, Comp6To, Comp6Title, Comp6Description, Company7, CompAdd7, Comp7Supervisor, CompPhone7, Comp7From, Comp7To, Comp7Title, Comp7Description)" _
& "SELECT " & InstructorID & "," & Comp1 & "," & Comp1Add & "," & Comp1sup & "," & Comp1Phone & "," & comp1from & "," & comp1To & "," & Comp1Title & "," & Comp1Desc & "," & Comp2 & "," & Comp2Add & "," & Comp2sup & "," & Comp2Phone & "," & comp2from & "," & comp2To & "," & Comp2Title & "," & Comp2Desc & "," & Comp3 & "," & Comp3Add & "," & Comp3sup & "," & Comp3Phone & "," & comp3from & "," & comp3To & "," & Comp3Title & "," & Comp3Desc & "," & Comp4 & "," & Comp4Add & "," & Comp4sup & "," & Comp4Phone & "," & comp4from & "," & comp4To & "," & Comp4Title & "," & Comp4Desc & "," & Comp5 & "," & Comp5Add & "," & Comp5sup & "," & Comp5Phone & "," & comp5from & "," & comp5To & "," & Comp5Title & "," & Comp5Desc & "," & Comp6 & "," & Comp6Add & "," & Comp6sup & "," & Comp6Phone & "," & comp6from & "," & comp6To & "," & Comp6Title & "," & Comp6Desc & "," & Comp7 & "," & Comp7Add & "," & Comp7sup & "," & Comp7Phone & "," & comp7from & "," & comp7To & "," & Comp7Title & "," & Comp7Desc & ";"
RefTable = "INSERT INTO References (InstructorID, CharRef1, CRAddress1, CRPhone1, CharRef2, CRAddress2, CRPhone2, CharRef3, CRAddress3, CRPhone3)" & "SELECT " & InstructorID & "," & charref1 & "," & CRAddress1 & "," & CRPhone1 & "," & charref2 & "," & CRAddress2 & "," & CRPhone2 & "," & charref3 & "," & CRAddress3 & "," & CRPhone3 & ";"
EduTable = "INSERT INTO [Education History] (InstructorID,Sch1,Sch1Address,Sch1From,Sch1To,Sch1GradDate,Sch1DegreeType,Sch2,Sch2Address,Sch2Program,Sch2From,Sch2To,Sch2GradDate,Sch2DegreeType,TradeSchool,TSAddress,TCourse,TSFrom,TSTo,TSGradDate,TSDegreeType,[Highest Degree Earned],[Highest Awarding Institute],[ASE Certified])" _
& "SELECT " & InstructorID & "," & Sch1 & "," & Sch1Add & "," & Sch1from & "," & Sch1to & "," & Sch1Graddate & "," & Sch1Degree & "," & Sch2 & "," & Sch2Address & "," & sch2program & "," & sch2from & "," & sch2to & "," & Sch2Graddate & "," & sch2degree & "," & TSName & "," & TSAdd & "," & TSprogram & "," & TSfrom & "," & TSto & "," & TSgrad & "," & TSDegree & "," & Highestedu & "," & Highestdegree & "," & ASE & ";"
Debug.Print EduTable
Debug.Print EmpTable
Set cnn = New ADODB.Connection
'DSN connection Set DSN locally if it fails, ODBC panel USER DSN, use access driver and point it to the database
cnn.Open "Instructor"
cnn.Execute InstTable
cnn.Execute RefTable
cnn.Execute EmpTable
cnn.Execute EduTable <----Here is where the debug breaks.
Die ersten drei Insert-Anweisungen Die edu Tabelle nicht funktioniert.
Alle drei funktionieren ordnungsgemäß durch Ziehen der Daten, hier ist die Abfrage, die fehlschlägt und ich kann nicht herausfinden, warum.
"INSERT INTO [Education History] (InstructorID,Sch1,Sch1Address,Sch1From,Sch1To,Sch1GradDate,Sch1DegreeType,Sch2,Sch2Address,Sch2Program,Sch2From,Sch2To,Sch2GradDate,Sch2DegreeType,TradeSchool,TSAddress,TCourse,TSFrom,TSTo,TSGradDate,TSDegreeType,[Highest Degree Earned],[Highest Awarding Institute],[ASE Certified])" _
& "SELECT " & InstructorID & "," & Sch1 & "," & Sch1Add & "," & Sch1from & "," & Sch1to & "," & Sch1Graddate & "," & Sch1Degree & "," & Sch2 & "," & Sch2Address & "," & sch2program & "," & sch2from & "," & sch2to & "," & Sch2Graddate & "," & sch2degree & "," & TSName & "," & TSAdd & "," & TSprogram & "," & TSfrom & "," & TSto & "," & TSgrad & "," & TSDegree & "," & Highestedu & "," & Highestdegree & "," & ASE & ";"
Der VBA-Debugger sagt mir, es ist eine ungültige Insert-Anweisung, und der Online-Kontrolleure mir sagen, es nicht um [Education History], was soll ich hier fehle?
Ich weiß, ich kann Transaktionen verwenden und dasselbe erreichen, aber ich glaube, ich würde immer noch mit meinen INSERT INTO-Anweisungen einen Fehler auftreten.
Jede Hilfe wird sehr geschätzt, wurde auf diesem für eine Weile festgefahren.
Text und Datum Qualifier hinzufügen und sehen, ob es immer noch nicht. Besser noch, parametriere die Abfrage. – Comintern
Ich habe keine Werte in den anderen.Ich verwende Select anstelle von Werten für diese Abfragen. Es markiert die nicht arbeitende um den Anfang der Abfrage. – Lostidentity
'Debug.Print' das generierte SQL und versuchen Sie es in Ihrem Abfrageeditor auszuführen. –