字符串查询无法执行

时间:2015-01-20 06:13:43

标签: vba excel-vba ms-access excel

惠,

我正在编写代码,我可以从Excel传输数据并将其记录在Access数据库中。我目前的代码,我从一个网站复制它并更改了我的工作表的值和一些声明。但问题是,我无法运行我的代码,但是我从其他网站获得的文件能够成功运行。你能解释我的代码中的问题吗?这是代码:

Sub Customer Log()
Dim cn as Object
Dim strQuery as string
Dim custName As String
Dim ordID As String
Dim myDB As String

'Initialize Variables
custName = Sheets("Batch_Report").Range("D4").Value
ordID = Sheets("Batch_Report").Range("D5").Value

'Database Location
myDB = "C:\Users\intern.maxvue\Dekstop\Adhariah (Intern)\Bar Code System\ReviewReport_v1_2015.accdb"
Set cn = CreateObject("ADODB.Connection")

With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"    'For *.ACCDB Databases
    .ConnectionString = myDB
    .Open
End With

strQuery = "INSERT INTO CustomerLog ([CUSTOMER NAME], [ORDER ID]) " & _
           "VALUES (""" & custName & """, " & ordID & "); "

cn.Execute strQuery
cn.Close
Set cn = Nothing
End Sub

当我调试并且在cn.Execute strQuery处到达行时,"运行时错误:没有给出一个或多个所需参数的值"突然出现。

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试:

strQuery = "INSERT INTO CustomerLog ([CUSTOMER NAME], [ORDER ID]) " & _
           "VALUES ('" & custName & "', " & ordID & ")"