我正在尝试从MS Access访问mySQL数据库中的某些行,并且我在这一行上一直收到无效的参数错误:
Set rs = CurrentDb.OpenRecordset("SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = prodSelect AND EndTime is NULL AND StartTime < dateAdd('h', 3, NOW())", [dbSeeChanges])
这是不对的吗?
Private Sub cmdClockEnd_Click()
'Check if a group has been selected.
If frmChoice.value = 0 Then
MsgBox "Please select a production line."
End
End If
'Setup form for user input.
lblEnd.Visible = True
'Save end of lunch value.
lblEnd.Caption = Format(Now, "MMM/DD/YY hh:mm:ss AMPM")
'Declare database variables.
Dim dbName As DAO.Database
Dim strValuesQuery As String
Dim rs As DAO.Recordset
Dim prodSelect As String
Dim sSQL As String
Dim timeValue As String
Set dbName = CurrentDb
'Get values of Production Line.
If frmChoice.value = 1 Then
prodSelect = "L2"
ElseIf frmChoice.value = 2 Then
prodSelect = "L3"
End If
'Get the last TimeID with the following parameters.
sSQL = "SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = prodSelect AND EndTime is NULL AND StartTime < #" & DateAdd("h", 3, Now()) & "#"
Set rs = dbName.OpenRecordset(sSQL, dbSeeChanges)
strValuesQuery = _
"UPDATE tblLunchTime " & _
"SET EndTime = '" & Now & "'" & _
"WHERE TimeID = " & rs![TimeID] & " "
'Turn warning messages off.
DoCmd.SetWarnings False
'Execute Query.
DoCmd.RunSQL strValuesQuery
'Turn warning messages back on.
DoCmd.SetWarnings True
End Sub
答案 0 :(得分:5)
您需要将prodSelect放在引号之外:
"WHERE ProductionID = " & prodSelect & " AND ...
几乎总是最好说:
sSQL="SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = " & prodSelect & _
" AND EndTime is NULL AND StartTime < dateAdd('h', 3, NOW())"
''Debug.print sSQL
Set rs = CurrentDb.OpenRecordset(sSQL)
您可以看到使用Debug.Print的优势。
AHA prodSelect是正文!你需要报价!
sSQL="SELECT TimeID " & _
"FROM tblLunchTime " & _
"WHERE ProductionID = '" & prodSelect & _
"' AND EndTime is NULL AND StartTime < dateAdd('h', 3, NOW())"
答案 1 :(得分:1)
我想我也会做客户端的日期标准连接,因为它还有一件事可能会出错:
"...StartTime < #" & DateAdd("h", 3, Now()) & "#"
我不知道SQL Server没有DateAdd()和Now()函数,也不知道它们的行为与Access中的行为完全不同,但我不会抓住机会 - 我会在客户端上进行此计算,而不是将其交给服务器。
答案 2 :(得分:1)
似乎有关于tblLunchTime的混淆......无论是本机Jet / ACE表还是指向另一个数据库中的表的链接。请告诉我们这个命令的输出:
Debug.Print CurrentDb.TableDefs("tblLunchTime").Connect
您可以将该行粘贴到立即窗口中,然后按Enter键显示响应。 (您可以使用CTRL + g击键组合打开立即窗口。)
如果响应以“ ODBC ”开头,建议您在代码中尝试以下行:
Set rs = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset, dbSeeChanges)
更新:现在您已经过了这个障碍,建议您使用UPDATE语句更改方法。不要关闭警告;尝试这样的事情:
'Execute Query. '
CurrentDb.Execute strValuesQuery, dbFailOnError
并添加一个错误处理程序来处理dbFailOnError捕获的任何错误。