INSERT INTO Access vba的语法错误

时间:2016-01-28 13:05:10

标签: vba ms-access access-vba

当我运行以下代码时,我收到INSERT INTO语句的语法错误。 在执行DoCMD步骤之前,locals窗口将valuestring的值显示为值为" 1/4/2016"

我认为错误与输入日期字段的字符串有关,但不确定如何修复它。

Formdate被格式化为日期,并且未声明data1,尽管它在本地窗口中正确显示为日期,#1/4/2016#

    Public Sub Import2(FileName As Variant)
    Dim wb As Object, ws As Object
    Dim xl As Object
    Set xl = CreateObject("excel.Application")
    Dim qs As String
    Dim ValueString As String


    'opens workbook, populates data1, etc.
    Set wb = xl.Workbooks.Open(FileName)
            Set ws = wb.worksheets("For Export")
            data1 = ws.cells(2, 1)
            Data2 = ws.cells(2, 2)
            Data3 = ws.cells(2, 3)
      ValueString = "(" & data1 & ")"
    qs = "INSERT INTO MAF (FormDate) VALUES & valuestring"
    DoCmd.RunSQL qs
    'CurrentDb.Execute qs

1 个答案:

答案 0 :(得分:0)

像这样修改:

Data1 = ws.cells(2, 1)
ValueString = Format(Data1, "yyyy\/mm\/dd")
qs = "INSERT INTO MAF (FormDate) VALUES (#" & valuestring & "#)"