简体   繁体   English

MS Access VBA中的SQL语句中的Syantax错误

[英]Syantax error in SQL statement in VBA of MS Access

I wrote code: 我写了代码:

str = "INSERT INTO prod_LJ_Completion VALUES (" _
    & 0 & ", " & PartID & ", " & cmbLJ_TypeID.value & ", " & isChk(chkLJ_Transformator.value) & ", " _
    & isChk(chkLJ_LAD.value) & ", " & txtLJ_KevlarCable.value & ", " & txtLJ_GrayCable.value & ", " _
    & txtLJ_WhiteCable.value & ", " & cmbLJ_CylinderTypeID.value & ", " & isChk(chkLJ_JackPack.value) & ", " _
    & txtJackPackID.value & ", " & isChk(chkLJ_Completed) & ", " & txtLJ_CompletionDate.value & ", " _
    & 0 & ", " & "dbo" & ", " & txtLegLength.value & ", " & txtInsertDiameter.value & ", " & txtPlateKits.value & ", " _
    & isChk(chkPipe_Gal.value) & ", " & isChk(chkDemoModel.value) & ", " & 0 & ", " & 0 & ", " & txtComments.value & ", " & txtAA.value & ")"
CurrentProject.Connection.Execute str

When this code is executed I get error: "Syntax error in INSERT INTO statement". 执行此代码后,出现错误:“ INSERT INTO语句中的语法错误”。

What is wrong? 怎么了?

It looks like you are not properly quoting string values. 看来您没有正确引用字符串值。 Eg, 例如,

& "dbo" &

should be 应该

& "'dbo'" &

The same needs to be done for any other string data types. 对于任何其他字符串数据类型,都需要执行相同的操作。

I think you are finding your code difficult to debug because of that monster line that builds SQL text on the fly. 我认为您发现您的代码难以调试,因为那条怪异的行动态生成了SQL文本。

Rather than dynamically building SQL at run time, consider instead creating a procedure with strongly typed parameters in the database at design time then invoking the procedure with parameter values at run time. 与其在运行时动态构建SQL,不如考虑在设计时在数据库中创建一个具有强类型参数的过程,然后在运行时使用参数值来调用该过程。 Adding a column list to your INSERT INTO statement may help too. 将列列表添加到INSERT INTO语句也可能会有所帮助。

For example, as a one off exercise create you procedure, something like this (guessing column names and types): 例如,作为一项一次性练习,您将创建一个过程,如下所示(猜测列的名称和类型):

CREATE PROCEDURE Add_prod_LJ_Completion
(
 :PartID INTEGER, 
 :LJ_TypeID INTEGER, 
 :LJ_Transformator YESNO, 
 :LJ_LAD YESNO, 
 :LJ_KevlarCable VARCHAR(10), 
 :LJ_GrayCable VARCHAR(20), 
 :LJ_WhiteCable VARCHAR(30), 
 :LJ_CylinderTypeID INTEGER, 
 :LJ_JackPack YESNO,  
 :JackPackID INTEGER, 
 :LJ_Completed YESNO,  
 :LJ_CompletionDate DATETIME, 
 :LegLength DECIMAL(11, 3), 
 :InsertDiameter DECIMAL(9, 5), 
 :PlateKits CHAR(5), 
 :Pipe_Gal YESNO,  
 :DemoModel YESNO,  
 :Comments MEMO, 
 :AA CHAR(10)
)
AS
INSERT INTO prod_LJ_Completion 
(
 const_1, PartID, LJ_TypeID, LJ_Transformator, 
 LJ_LAD, LJ_KevlarCable, LJ_GrayCable, 
 LJ_WhiteCable, LJ_CylinderTypeID, 
 LJ_JackPack, JackPackID, LJ_Completed, LJ_CompletionDate, 
 const_2, const_3, LegLength, InsertDiameter, PlateKits, 
 Pipe_Gal, DemoModel, const_4, const_5, Comments, AA

)
VALUES 
(
 0, :PartID, :LJ_TypeID, :LJ_Transformator, 
 :LJ_LAD, :LJ_KevlarCable, :LJ_GrayCable, 
 :LJ_WhiteCable, :LJ_CylinderTypeID, 
 :LJ_JackPack, :JackPackID, :LJ_Completed, :LJ_CompletionDate, 
 0, 'dbo', :LegLength, :InsertDiameter, :PlateKits, 
 :Pipe_Gal, :DemoModel, 0, 0, :Comments, :AA
);

At run time you can use a ADODB Command object to execute the procedure with parameters that reference your front end controls, something like this: 在运行时,您可以使用ADODB Command对象来执行带有引用您的前端控件的参数的过程,如下所示:

Sub runitwithparams()

  Dim cmd As ADODB.Command
  Set cmd = New ADODB.Command
  With cmd
    Set .ActiveConnection = CurrentProject.Connection
    .NamedParameters = True
    .CommandType = adCmdStoredProc
    .CommandText = "Add_prod_LJ_Completion"

    .Parameters.Append = _
        .CreateParameter(":PartID", adInteger, adParamInput, , _
            PartID)
    .Parameters.Append = _
        .CreateParameter(":LJ_TypeID", adInteger, adParamInput, , _
            cmbLJ_TypeID.Value)
    .Parameters.Append = _
        .CreateParameter(":LJ_Transformator", adInteger, adParamInput, , _
            isChk(chkLJ_Transformator.Value))
   '// ...
   '// etc etc
   '// ...
    .Parameters.Append = _
        .CreateParameter(":AA", adChar, adParamInput, 10, _
            txtAA.Value)

    Dim rowsAffectedTally As Long
    .Execute rowsAffectedTally

  End With

End Sub

Also, your table seems quite 'wide'. 另外,您的表似乎很“宽”。 Consider the design principle that a table EITHER models an entity OR a relationship between entities but not both. 考虑一个设计原则,即表EITHER对实体或实体之间的关系进行建模,但不能对两者进行建模。 Your table has column names postfixed with ID, which suggest multiple relationships, and attribute names (eg LegLength) that suggests entities. 您的表具有以ID后缀(表示多个关系)的列名以及表示实体的属性名(例如LegLength)。 Consider modelling one entity type per table and relationships each in a separate table. 考虑为每个表建模一种实体类型,并在单独的表中建模每种实体的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM