从Excel查询访问权限时出现错误3001(参数类型错误)

从Excel查询访问权限时出现错误3001(参数类型错误),excel,ms-access-2010,vba,Excel,Ms Access 2010,Vba,我想以编程方式将Access中表(表1)中的数据导入Excel。 我在用户表单上有两个搜索框(txtCr1和txtCr2)和一个执行代码的按钮。每个搜索框对应于表1中的一个字段。我想使用“和”搜索这两个字段。我不知道我的代码中缺少了什么,但是当我运行它时,我得到了错误3001。请建议如何修复此问题 提前谢谢你的帮助 Private Sub CommandButton5_Click() 'Declaring the necessary variables. Dim cnn As ADODB.Co

我想以编程方式将Access中表(表1)中的数据导入Excel。 我在用户表单上有两个搜索框(txtCr1和txtCr2)和一个执行代码的按钮。每个搜索框对应于表1中的一个字段。我想使用“和”搜索这两个字段。我不知道我的代码中缺少了什么,但是当我运行它时,我得到了错误3001。请建议如何修复此问题

提前谢谢你的帮助

Private Sub CommandButton5_Click()

'Declaring the necessary variables.
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rs As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath As String
Dim SQLwhere As String
Dim SrtSql As String
Dim i As Integer
Dim var
Dim var2
'add error handling
On Error GoTo errHandler:
'Disable screen flickering.
Application.ScreenUpdating = False
'clear the values from the worksheet
Sheet5.Range("A2:AD499").ClearContents

'get the path to the database
dbPath = Sheet5.Range("H500").Value

'set the search variable
var = UserForm1.txtCrt1
var = UserForm1.txtCrt2

Set cnn = New ADODB.Connection ' Initialise the collection class variable

'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath


'Create the SQL statement to retrieve the data from table.


SQLwhere = "WHERE "

If Len(var & vbNullString) <> 0 Then

SQLwhere = SQLwhere & "[Table1].[RunDate] = '" & var & "' AND "

End If

If Len(var2 & vbNullString) <> 0 Then
SQLwhere = SQLwhere & "[Table1].[SampleID] = '" & var2 & "' AND "
End If

StrSql = "SELECT * FROM [Table1] "

 'Remove the last AND applicable

If SQLwhere = "WHERE " Then
    SQLwhere = ""
Else
    SQLwhere = Left(SQLwhere, Len(SQLwhere) - 4)
End If

StrSql = StrSql & SQLwhere


'Create the ADODB recordset object.
Set rs = New ADODB.Recordset 'assign memory to the recordset

'ConnectionString Open '—-5 aguments—-
'Source, ActiveConnection, CursorType, LockType, Options
rs.Open sql, cnn

'Check if the recordset is empty.
If rs.EOF And rs.BOF Then
'Close the recordet and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing
'Enable the screen.
Application.ScreenUpdating = True
'In case of an empty recordset display an error.
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
'Me.lstDataAccess.RowSource = ""
Exit Sub
End If

'Write the reocrdset values in the sheet.
Sheet5.Range("A2").CopyFromRecordset rs

'Close the recordset and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing

'Enable the screen.
Application.ScreenUpdating = True


'Inform the user that the macro was executed successfully.
MsgBox "Congratulation the data has been successfully Imported", vbInformation, "Import successful"
'error handler
On Error GoTo 0
Exit Sub
errHandler:
'clear memory
Set rs = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Import_Data"

End Sub
Private子命令按钮5_单击()
'声明必要的变量。
Dim cnn作为ADODB.Connection“Dim ADO集合类
Dim rs作为ADODB.Recordset“Dim ADO记录集类
将dbPath设置为字符串
Dim SQLwhere As字符串
作为字符串的Dim SrtSql
作为整数的Dim i
模糊变量
Dim var2
'添加错误处理
错误转到错误处理程序时:
'禁用屏幕闪烁。
Application.ScreenUpdating=False
'清除工作表中的值
表5.范围(“A2:AD499”).ClearContents
'获取数据库的路径
dbPath=Sheet5.范围(“H500”).值
'设置搜索变量
var=UserForm1.txtCrt1
var=UserForm1.txtCrt2
Set cnn=New ADODB.Connection'初始化集合类变量
'连接类配备了一个名为Open的-方法
'--4个aguments--连接字符串、用户ID、密码、选项
'连接字符串公式--Key1=Value1;键2=值2;键=值;
cnn.Open“Provider=Microsoft.ACE.OLEDB.12.0;数据源=”&dbPath
'创建SQL语句以从表中检索数据。
SQLwhere=“WHERE”
如果Len(var&vbNullString)为0,则
SQLwhere=SQLwhere&“[Table1].[RunDate]='”&var&“AND”
如果结束
如果Len(var2&vbNullString)为0,则
SQLwhere=SQLwhere&“[Table1].[SampleID]='”&var2&“AND”
如果结束
StrSql=“从[表1]中选择*
'删除最后一个和适用的
如果SQLwhere=“WHERE”,则
SQLwhere=“”
其他的
SQLwhere=Left(SQLwhere,Len(SQLwhere)-4)
如果结束
StrSql=StrSql&SQLwhere
'创建ADODB记录集对象。
Set rs=New ADODB.Recordset'为记录集分配内存
“连接字符串打开”-5个aguments--
'源、ActiveConnection、游标类型、锁类型、选项
美国有线电视新闻网
'检查记录集是否为空。
如果是rs.EOF和rs.BOF,则
'关闭记录集和连接。
rs.Close
有线电视新闻网,结束
“清除内存
设置rs=无
设置cnn=无
'启用屏幕。
Application.ScreenUpdating=True
'如果记录集为空,则显示错误。
MsgBox“记录集中没有记录!”,vbCritical,“没有记录”
'Me.lstDataAccess.RowSource=“”
出口接头
如果结束
'在工作表中写入reocrdset值。
表5.范围(“A2”).从记录集rs复制
'关闭记录集和连接。
rs.Close
有线电视新闻网,结束
“清除内存
设置rs=无
设置cnn=无
'启用屏幕。
Application.ScreenUpdating=True
'通知用户宏已成功执行。
MsgBox“祝贺数据已成功导入”,vbInformation,“导入成功”
'错误处理程序
错误转到0
出口接头
错误处理程序:
“清除内存
设置rs=无
设置cnn=无
MsgBox“错误”和错误号(“&Err.Description&”)在过程导入_数据中
端接头

我猜错误发生在这一行:
rs.opensql,cnn
在这一点上,
sql
是一个未声明的空变量。如果使用这行代码,您是否得到了预期的结果,或者至少得到了不同的错误
rs.打开StrSql,cnn
选项显式
添加到模块的声明部分。然后从VB编辑器的主菜单运行调试->编译。修复编译器抱怨的任何问题。然后再次尝试编译。重复,直到不再出现编译错误。如果您仍然需要帮助,请向我们展示您的代码,该代码使用显式
选项编译时不会出错。我假设
var=UserForm1.txtCrt2
应该是
var2=UserForm1.txtCrt2
?我猜错误发生在这一行:
rs.Open sql,cnn
在这一点上,
sql
是一个未声明的空变量。如果使用这行代码,您是否得到了预期的结果,或者至少得到了不同的错误
rs.打开StrSql,cnn
选项显式
添加到模块的声明部分。然后从VB编辑器的主菜单运行调试->编译。修复编译器抱怨的任何问题。然后再次尝试编译。重复,直到不再出现编译错误。如果您仍然需要帮助,请向我们展示您的代码,该代码使用显式
选项编译时不会出错。我假设
var=UserForm1.txtCrt2
应该是
var2=UserForm1.txtCrt2
?我猜错误发生在这一行:
rs.Open sql,cnn
在这一点上,
sql
是一个未声明的空变量。如果使用这行代码,您是否得到了预期的结果,或者至少得到了不同的错误
rs.打开StrSql,cnn
选项显式
添加到模块的声明部分。然后从VB编辑器的主菜单运行调试->编译。修复编译器抱怨的任何问题。然后再次尝试编译。重复,直到不再出现编译错误。如果您仍然需要帮助,请向我们展示您的代码,该代码使用显式
选项编译时不会出错
我假设
var=UserForm1.txtCrt2
应该是
var2=UserForm1.txtCrt2