使用具有计算字段的VBA将记录插入Access 2016表中

时间:2018-10-30 18:47:02

标签: sql vba ms-access

我有一个旧的(旧的)Access数据库,该数据库跟踪进行中的工作信息。当前,最终用户在Excel中创建报价。如果无法完成报价,则最终用户必须将电子表格中的信息复制/粘贴/手动输入到此Access数据库中。

在Access中有一个“开始日期”,“停止日期”和“周转时间”计算字段。轮转时间是停止日期减去开始日期。 我正在尝试使该过程自动化,因此,如果无法完成电子表格,他们可以单击按钮以使用输入的信息自动更新Access数据库,以保存手动工作。

当我尝试运行代码时,我得到了通用代码:

  

“ INSERT INTO语句中的语法错误”。

代码如下:

    gblDatabaseLocation = "c:\UnderRevision\"
gblDatabaseName = "DLA WIP.accdb"

Dim CN As ADODB.Connection
Set CN = New ADODB.Connection
Dim sql As String

CN.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & gblDatabaseLocation & gblDatabaseName

Set cm = New ADODB.Command
cm.ActiveConnection = CN


' load variables... from spreadsheet...
wID = "" ' will be autopopulated...
wReady = False ' default is N
wStatus = "OPEN" ' they want to select it manually
wInDate = Worksheets("SOLICITATION SUPPORT").Range("D6").value
wOutDate = "12/31/2019" ' blank for now...
wProgram = Worksheets("SOLICITATION SUPPORT").Range("D10").value
wBidPoint = Worksheets("SOLICITATION SUPPORT").Range("D18").value
wOwner = Worksheets("SOLICITATION SUPPORT").Range("D4").value
wPartNum = Worksheets("SOLICITATION SUPPORT").Range("D14").value
wQty = Worksheets("SOLICITATION SUPPORT").Range("D15").value
wSolicitation = Worksheets("SOLICITATION SUPPORT").Range("D11").value
wPRNUM = Worksheets("SOLICITATION SUPPORT").Range("D12").value
wNSN = Worksheets("SOLICITATION SUPPORT").Range("D13").value
wPending = "" ' want to choose it
wMultiPend = True ' want to choose it
wEstDollars = Worksheets("SOLICITATION SUPPORT").Range("AA6").value
wSupplier = "<SUPPLIER>" ' should read this in...
wSoleSource = "<SOLESOURCE>" ' varies based on solicitation
wFOB = Worksheets("SOLICITATION SUPPORT").Range("W10").value
wInspection = Worksheets("SOLICITATION SUPPORT").Range("W11").value
wMilStd130 = Worksheets("SOLICITATION SUPPORT").Range("N10").value
wShelfLife = "30" ' select later
wTime = "10" ' input later - shelf life called out in SOL
wTraceability = "Y" ' if approved = Y, pending = X
wSource = "<SOURCE>" ' from SOL
wSupplierPartNum = "<SUPPLIERPARTNUM>" ' from SOL
wIAW = "<IAW>" ' select later
w50US = Worksheets("SOLICITATION SUPPORT").Range("N17").value
wISO = Worksheets("SOLICITATION SUPPORT").Range("N24").value
wJCP = Worksheets("SOLICITATION SUPPORT").Range("N25").value
wInspAtMFG = Worksheets("SOLICITATION SUPPORT").Range("N14").value
wQPL_QML = Worksheets("SOLICITATION SUPPORT").Range("N15").value
wFat = Worksheets("SOLICITATION SUPPORT").Range("N12").value
wMfgSymbol = Worksheets("SOLICITATION SUPPORT").Range("N11").value
wSBSA = Worksheets("SOLICITATION SUPPORT").Range("N22").value
wCOQC = Worksheets("SOLICITATION SUPPORT").Range("N16").value
wNotes = Worksheets("SOLICITATION SUPPORT").Range("A31").value
wPKey = "" ' pull in later
wQuoted = True
wTat = DateDiff("D", wInDate, wOutDate)

wNotNotified = "<NOT NOTIFIED>"

' create sql string...
sql = "INSERT INTO WIP ( Ready, Status, [In Date], [Out Date], Program, [Bid Point], Owner, [Part #], Qty, Solicitation, [PR #], " _
    & "NSN, Pending, [Multi-Pend], [Est $], Supplier, [Sole Source], FOB, Inspection, MilStd130, [Shelf Life], Time, Traceability, Source, " _
    & "[Supplier Part #], IAW, [50% US], ISO, JCP, [Insp@Mfg], [QPL-QML], Fat, [Mfg Symbol], SBSA, COQC, Notes, PKEY, Quoted, tat, " _
    & "[Not Notified By PCF]) VALUES " _
    & "('" & wReady & "','" & wStatus & "',#" & wInDate & "#,#" & wOutDate & "#,'" & wProgram & "','" & wBidPoint & "','" _
    & wOwner & "','" & wPartNum & "','" & wQty & "','" & wSOlication & "','" & wPRNUM & "','" & wNSN & "','" & wPending & "','" & wMultiPend & "'," _
    & wEstDollars & ",'" & wSupplier & "','" & wSoleSource & "','" & wFOB & "','" & wInspection & "','" & wMilStd130 & "','" & wShelfLife & "','" _
    & wTime & "','" & wTraceability & "','" & wSource & "','" & wSupplierPartNum & "','" & wIAW & "','" & w50US & "','" & wISO & "','" _
    & wJCP & "','" & wInspAtMFG & "','" & wQPL_QML & "','" & wFat & "','" & wMfgSymbol & "','" & wSBSA & "','" & wCOQC & "','" & wNotes & "','" _
    & wPKey & "','" & wQuoted & "'," & wTat & ",'" & wNotNotified & "')"

cm.CommandText = sql
Set rs1 = New ADODB.Recordset
Set rs1 = CN.Execute(sql, varparams, adCmdText)

这是我的代码创建的SQL语句:

INSERT INTO WIP ( Ready, Status, [In Date], [Out Date], Program, [Bid Point], Owner, [Part #], Qty, Solicitation, [PR #], NSN, Pending, [Multi-Pend], [Est $], Supplier, [Sole Source], FOB, Inspection, MilStd130, [Shelf Life], Time, Traceability, Source, [Supplier Part #], IAW, [50% US], ISO, JCP, [Insp@Mfg], [QPL-QML], Fat, [Mfg Symbol], SBSA, COQC, Notes, PKEY, Quoted, tat, [Not Notified By PCF]) VALUES ('False','OPEN',#10/30/2018#,#12/31/2019#,'OSHKOSH','OSHKOSH','ALI ZERBE','1212FX','1','','','','','True',4,'<SUPPLIER>','<SOLESOURCE>','','','','30','10','Y','<SOURCE>','<SUPPLIERPARTNUM>','<IAW>','','','','','','','','','','','','True',427,'<NOT NOTIFIED>')

我已经仔细检查了语法,很确定我在所有字符串周围都有单引号,在日期周围有#。 我知道对于自动编号ID字段,我从字段列表和值列表中忽略了这一点。

但是SQL中如何引用计算字段?我是否也忽略它,让数据库进行计算?我在互联网上找不到对此的任何引用...

平淡的错误消息没有指向我任何特定的方向。

请帮助?

1 个答案:

答案 0 :(得分:0)

感谢您的回复。 我最终发现了多个问题,并设法解决了这些问题: (1)复制查询文本并粘贴到SSMS中并尝试执行它。它标记的第一件事是“源”字段。显然,这是SQL中的保留字。将其更改为Srce。用撇号替换日期周围的“#”。将True更改为1,将False更改为0。能够在SSMS中执行它。 (2)但是,此语法与链接表方法不兼容,因此我找到了正确的ODBC连接字符串,并绕过了链接表以更直接地进入SQL Server。这使我可以使用修改后的查询语法。 (3)将表而非访问表迁移到SQL Server。完全消除了计算所得的字段...重命名了所有字段,以消除空格,破折号,“&”号,美元符号以及任何可能引起任何问题的东西...

所以我现在可以正常运行了! 再次感谢。 史蒂夫