【问题标题】:SQL Error, possible newbie errorSQL 错误,可能是新手错误
【发布时间】:2016-08-26 20:41:39
【问题描述】:

我正在使用 ODBC 在 VBA 脚本中插入 SQL INSERT INTO 语句,以从 word doc 中提取数据并自动将其放入 access 数据库中。

我已验证数据不匹配,并将其与其他运行没有问题的 INSERT INTO 语句进行了比较。这些语句链接到字符串变量,然后在 cnn.execute 命令中使用。

存储和执行我的变量的代码如下:

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.

前三个插入语句有效 edu 表无效。

所有这三个都通过提取数据正常工作,这是失败的查询,我不知道为什么。

"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 & ";"

VBA 调试器告诉我它是无效的插入语句,在线检查器告诉我它在 [Education History] ​​附近失败,我在这里缺少什么?

我确实意识到我可以使用事务来完成同样的事情,但我相信我的 INSERT INTO 语句仍然会出错。

非常感谢任何帮助,在这个问题上停留了一段时间。

【问题讨论】:

  • 添加文本和日期限定符,看看它是否仍然失败。更好的是,参数化查询。
  • 我在其他任何一个中都没有值。我正在使用 select 代替这些查询的值。它在查询开始时标记不工作的那个。
  • Debug.Print 生成的 SQL 并尝试在查询编辑器中运行它。
  • 也许你应该在参数字符串之间添加'
  • 不使用值应该没问题。 @Lostidentity,我建议您将整个字符串(连接)存储到一个字符串变量中,并从该变量执行。这将使您的调试更容易弄清楚。正如 Tim Williams 所提到的,使用 Debug.Print 可以查看字符串中的内容。

标签: sql vba ms-access odbc


【解决方案1】:

想通了,我看的不够仔细

我在 Edu 查询中输入错误的两个变量。

我要感谢大家的帮助,特别是 @Tim Williams 的调试打印。当我遵循他的建议并将输出放入查询时,我注意到一些变量输出为 , 而不是它们的实际输入。

【讨论】:

    【解决方案2】:

    此外,由于您自称是新手,因此我们不建议您或未来的读者对您的 SQL 查询进行参数化(行业最佳实践),从而避免任何引用附件,这将是一种疏忽,运行效率更高,并迫使您作为开发人员明确定义每个值的数据类型(这有助于避免遗漏一些)。

    以下是 ADO 和 DAO 方法以及 one 查询的示例。虽然它可能看起来令人生畏并且新的代码行太多,但仍然考虑它,因为它可以安全地将用户表单中的值与 Word 文档绑定,以保护数据库免受 sql 注入的影响。您的一些聪明用户可能会尝试破坏您的工作!注意正在定义的 id、日期和字符串类型。

    ADO 方法(您当前使用的)

    Dim cmd As ADODB.Command
    ...
    Campus = doc.FormFields("Campus").Result
    DOE = doc.FormFields("HireDate").Result
    Fname = doc.FormFields("FirstName").Result
    Mname = doc.FormFields("MiddleName").Result
    Nname = doc.FormFields("NickName").Result
    ...
    
    InstTable = "INSERT INTO Instructors (InstructorID, [First Name], [Middle Initial]," _
                 & " [Last Name], Nickname, Campus, [Job Title]," _
                 & " Address, City, State, Zip, Phone, DOB, SSN, DOE) " _
                 & " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" 
    
    Set cmd = New ADODB.Command          ' RESET CMD WITH EACH SQL QUERY 
    With cmd
        .ActiveConnection = conn
        .CommandText = InstTable
        .CommandType = adCmdText
        .CommandTimeout = 45
    End With
    
    cmd.Parameters.Append cmd.CreateParameter("InstructorIDParam", adInteger, adParamInput, InstructorID)
    cmd.Parameters.Append cmd.CreateParameter("FNameParam", adVarChar, adParamInput, FName)
    cmd.Parameters.Append cmd.CreateParameter("MNameParam", adVarChar, adParamInput, MName)
    cmd.Parameters.Append cmd.CreateParameter("LNameParam", adVarChar, adParamInput, LName)
    cmd.Parameters.Append cmd.CreateParameter("NnameParam", adVarChar, adParamInput, Nname)
    cmd.Parameters.Append cmd.CreateParameter("CampusParam", adVarChar, adParamInput, Campus)
    cmd.Parameters.Append cmd.CreateParameter("JobParam", adVarChar, adParamInput, Job)
    cmd.Parameters.Append cmd.CreateParameter("AddressParam", adVarChar, adParamInput, Address)
    cmd.Parameters.Append cmd.CreateParameter("CityParam", adVarChar, adParamInput, City)
    cmd.Parameters.Append cmd.CreateParameter("StateParam", adVarChar, adParamInput, State)
    cmd.Parameters.Append cmd.CreateParameter("ZipParam", adVarChar, adParamInput, Zip)
    cmd.Parameters.Append cmd.CreateParameter("PhoneParam", adVarChar, adParamInput, Phone)
    cmd.Parameters.Append cmd.CreateParameter("DOEParam", adDate, adParamInput, DOE)
    cmd.Parameters.Append cmd.CreateParameter("SSNParam", adVarChar, adParamInput, SSN)
    
    cmd.Execute
    Set cmd = Nothing
    

    DAO方法

    ' SET VBA reference: Microsoft Office ##.0 Access database engine object library 
    
    Dim db As DAO.Database, qd As DAO.QueryDef
    
    Set db = OpenDatabase("C:\Path\To\Database.accdb", True)
    
    InstTable = "PARAMETERS InstructorIDParam Long, FnameParam Text(255), MnameParam Text(255), LnameParam Text(255),"_ 
                 & "         NnameParam Text(255), CampusParam Text(255), JobParam Text(255), AddressParam Text(255)," _ 
                 & "         CityParam Text(255), StateParam Text(255), ZipParam Text(255), PhoneParam Text(255)," _
                 & "         DOBParam Text(255), SSNParam Text(255), DOEParam DateTime;" _
                 & " INSERT INTO Instructors (InstructorID, [First Name], [Middle Initial], [Last Name], Nickname," _
                 & "             Campus, [Job Title], Address, City, State, Zip, Phone, DOB, SSN, DOE) " _
                 & " VALUES (InstructorIDParam, FnameParam, MnameParam, LnameParam, NnameParam, CampusParam, JobParam," _ 
                 & "         AddressParam, CityParam, StateParam, ZipParam, PhoneParam, DOBParam, SSNParam, DOEParam);" _
    
    Set qd = db.CreateQueryDef("", InstTable)
    
    qd!InstructorIDParam = InstructorID
    qd!FNameParam = FName
    qd!MNameParam = MName
    qd!LNameParam = LName
    qd!NnameParam = Nname
    qd!CampusParam = Campus
    qd!JobParam = Job
    qd!AddressParam = Address
    qd!CityParam = City
    qd!StateParam = State
    qd!ZipParam = Zip
    qd!PhoneParam = Phone
    qd!DOEParam = DOE
    qd!SSNParam = SSN
    
    qd.Execute
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-25
      • 2017-12-25
      • 2011-05-14
      • 1970-01-01
      • 2022-10-14
      • 2013-06-19
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多