【发布时间】:2016-07-12 23:16:30
【问题描述】:
运行访问 2016
我正在尝试从 Excel 的 MS Access .mdb 表中导入数据。 (我的客户端使用的专有软件只能识别 *.mdb 文件。)当我在表关闭时运行此代码时,我收到错误:
Run-Time Error 3061
Too few parameters - Expected 2
如果我在 Access 中打开表时运行代码,一半的时间,我得到那个错误和一半的时间:
Run-Time error '3008'
The table 'Daily_Logs_of_Flows' is already opened exclusively by
another user, or it is already open through the user interface
and cannot be manipulated programmatically.
这似乎表明 VBA 有时会通过第一个错误。
由于this post on StackOverflow,我检查了变量名称,并在monthToImport 前后使用了单引号和数字符号(#)。错误来自
Expected 3
到
Expected 2
这里是代码
Sub importPLCDataFromAccess(monthToImport As Date)
Dim myDbLocation As String
myDbLocation = "K:\Users\WWTP Computer\Documents\POV_Projects\PLC Interface\PLC_Data.mdb"
DIM mySQLCall as String
Set myWorkbook = ActiveWorkbook
Set myDataSheet = myWorkbook.Worksheets("Page 1")
Set myEngine = New DAO.DBEngine
'Set myWorkspace = myEngine.Workspaces(0)
Set myDB = myEngine.OpenDatabase(myDbLocation)
' I deleted the workspace
' Set myDB = myWorkspace.OpenDatabase(myDbLocation)
mySQLCall = "SELECT Time_Stamp, GolfVolume, CreekVolume, InfluentVolume FROM Daily_Logs_of_Flows "
' Limit records to month requested...
mySQLCall = mySQLCall & "WHERE (DATEPART(m,Time_Stamp) = DATEPART(m,#" & monthToImport & "#)) "
' ... during the year requested
mySQLCall = mySQLCall & "AND (DATEPART(yyyy,Time_Stamp) = DATEPART(yyyy,#" & monthToImport & "#)) "
mySQLCall = mySQLCall & "ORDER BY Time_Stamp"
Debug.Print "mySQLCall = " & mySQLCall
Debug.Print "monthToImport: " & monthToImport
'Error occurs on next line where execute query & populate the recordset
Set myRecordSet = myDB.OpenRecordset(mySQLCall, dbOpenSnapshot)
'Copy recordset to spreadsheet
Application.StatusBar = "Writing to spreadsheet..."
Debug.Print "RecordSet Count = " & myRecordSet.recordCount
If myRecordSet.recordCount = 0 Then
MsgBox "No data retrieved from database", vbInformation + vbOKOnly, "No Data"
GoTo SubExit
End If
'....
End Sub
这是当前读取的 SQL 语句的 Debug.Print:
mySQLCall = SELECT Time_Stamp, GolfVolume, CreekVolume, InfluentVolume FROM Daily_Logs_of_Flows WHERE (DATEPART(m,Time_Stamp) = DATEPART(m,#6/1/2016#)) AND (DATEPART(yyyy,Time_Stamp) = DATEPART(yyyy,#6/1/2016#)) ORDER BY Time_Stamp
对我在这里缺少什么有什么想法吗?提前感谢您的帮助。
【问题讨论】:
-
我不知道答案,但是“排他性”错误实际上是“第一个”错误,所以当您收到运行时错误 3061 时,您实际上已经克服了那个错误,而不是另一个一路走来。
-
拿走SQL语句的Debug.Print直接在Access中运行会发生什么?