1

我正在尝试组装一个带有多个换行符的插入语句。插入包括数字和文本数据。我不断收到错误消息,我找不到语法错误的地方。谁能看到我的错误?

CurrentDb.Execute "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _
        "GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _
        "LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _
        "ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _
        "QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _
        "VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", "" & _
        " & Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", "" & _
        " & Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", "" & _
        " & Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", "" & _
        " & Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", "" & _
        " & Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '"" & _
        " & Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', "" & _
        " & Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", "" & _
        " & Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", "" & _
        " & Me.AAStandard & ", " & Me.ASTM_B209_10 & ")"
4

1 回答 1

1

我不确定额外的引号和 & 符号是做什么用的,但它们导致了问题。它们出现在每条价值线的开始和结束处。最好把你的字符串放在一个单独的变量中,然后执行,这样更容易发现问题。您可能还想考虑参数,在这种情况下它们会让您的生活更轻松。

ssql = "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _
        "GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _
        "LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _
        "ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _
        "QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _
        "VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", " & _
        Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", " & _
        Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", " & _
        Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", " & _
        Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", " & _
        Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '" & _
        Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', " & _
        Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", " & _
        Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", " & _
        Me.AAStandard & ", " & Me.ASTM_B209_10 & ")"


Dim db As database
Set db = CurrentDB
db.Execute ssql dbFailOnError

最后插入的记录 ID

Set rs = db.OpenRecordset("select @@identity as id")
LastID = rs("id")

参数示例:

ssql = "INSERT INTO Table1 (Atext,Anumber) Values (@AText,@Anumber)"
Dim qdf As QueryDef
Set qdf = CurrentDb.CreateQueryDef("", ssql)
qdf.Parameters("@atext") = "abc"
qdf.Parameters("@Anumber") = 1
qdf.Execute dbFailOnError

最后插入的记录 ID

qdf.SQL = "select @@identity as id"
Set rs = qdf.OpenRecordset
LastID = rs("id")
于 2013-02-18T15:36:27.083 回答