我在Access 2010中有一个表单,其中包含两个文本框(AIPIDTxt用于输入搜索条件,AIPResultTxt用于显示结果)和一个Button(搜索按钮)。我在Access中也有一个Table Table1。当我单击搜索按钮时,我需要执行一个查询,其条件在表单中的AIPIDTxt文本框中,将结果存储在记录集中,并在文本框AIPResultTxt中显示结果。所以我在Button事件处理程序中输入了以下VBA代码。
Private Sub SearchB_Click()
Dim localConnection As ADODB.Connection
Dim query As String
Dim aipid_rs As ADODB.Recordset
Dim db As Database
Set db = CurrentDb
Set localConnection = CurrentProject.AccessConnection
MsgBox "Local Connection successful"
query = "SELECT [AIP Name] FROM [Table1] WHERE [AIP ID]=
" & [Forms]![AIPIDSearchF]![AIPIDTxt] & ""
Set aipid_rs = db.OpenRecordset(query)
Me.AIPResultTxt.Text = aipid_rs![AIP Name]
End Sub
但是当我点击按钮时,我得到本地连接成功消息框,然后是行中的运行时错误3464:
Set aipid_rs= db.OpenRecordset(query)
我搜索过类似的错误并进行了更正。但错误不断涌现。我的查询有问题吗?无法弄清楚错误。该表是本地表。所以我可以直接在vba中的查询中给出[Table1]和字段名称。尝试添加分隔符,因为字段是文本字段。但这并没有奏效。我也无法提供以下查询:
query = "SELECT [AIP Name] FROM [Table1] WHERE [AIP ID]= " & [Forms]![AIPIDSearchF]!
[AIPIDTxt].Text & ""
这给了我一个运行时错误,指出无法从失去焦点的控件引用文本。我的标准是文本框中的文本。单击按钮时文本框失去焦点。但当我搜索错误时,解决方案是删除“.Text”。所以,我最终得到了上述查询。不知道这条线有什么问题:
Set aipid_rs= db.OpenRecordset(query)
答案 0 :(得分:2)
我怀疑您的代码存在多个问题。但Access只抱怨它发现的第一个问题。再看看这两行......
Dim aipid_rs As ADODB.Recordset
Set aipid_rs = db.OpenRecordset(query)
OpenRecordset
是一个返回DAO记录集的DAO方法。但是代码尝试将其分配给声明为aipid_rs
的{{1}}。那些记录集类型不兼容。
有一个ADO连接对象变量 localConnection ,以后不再使用它。虽然它不会触发错误,但它没有用处。事实上,我认为没有理由使用ADO中的任何内容来完成这项任务。
我建议您尝试使用此版本的代码...
As ADODB.Recordset
注意'Dim localConnection As ADODB.Connection
'Dim query As String ' query is a reserved word
Dim strQuery As String
'Dim aipid_rs As ADODB.Recordset
Dim aipid_rs As DAO.Recordset
Dim db As Database
Set db = CurrentDb
'Set localConnection = CurrentProject.AccessConnection
'MsgBox "Local Connection successful"
' you said [AIP ID] is text type, so include quotes around
' the text box value
strQuery = "SELECT [AIP Name] FROM [Table1] WHERE [AIP ID]= '" & _
[Forms]![AIPIDSearchF]![AIPIDTxt] & "'"
Debug.Print strQuery
DoCmd.RunCommand acCmdDebugWindow
Set aipid_rs = db.OpenRecordset(strQuery)
'Me.AIPResultTxt.Text = aipid_rs![AIP Name] ' .Text property is only
' available when control has focus; it will trigger
' an error at any other time
Me.AIPResultTxt.Value = aipid_rs![AIP Name]
将在立即窗口中显示Debug.Print strQuery
语句的文本,SELECT
将打开立即窗口。如果查询仍有问题,请复制语句文本并将其粘贴到新查询的SQL视图中进行测试。
最后,我很好奇这是否可以用更少的代码给你所需要的东西......
DoCmd.RunCommand acCmdDebugWindow
答案 1 :(得分:0)
您正在使用ADO记录集,因此您的开放语法不正确。以下内容适用于您(除非您在控件没有焦点时设置文本错误...)
Dim localConnection As ADODB.Connection
Dim query As String
Dim aipid_rs As ADODB.Recordset
Dim db As Database
Set db = CurrentDb
Set localConnection = CurrentProject.AccessConnection
MsgBox "Local Connection successful"
query = "SELECT [AIP Name] FROM [Table1] WHERE [AIP ID]= '" & [Forms]![AIPIDSearchF]![AIPIDTxt] & "'"
'Set aipid_rs = db.OpenRecordset(query)
Set aipid_rs = New ADODB.Recordset
aipid_rs.Open query, localConnection, adOpenStatic, adLockReadOnly
If Not aipid_rs.EOF Then
Me.AIPResultTxt.Text = aipid_rs![AIP Name]
Else
MsgBox "No records!!"
End If
aipid_rs.Close
Set aipid_rs = Nothing
localConnection.Close
Set localConnection = Nothing
db.Close
Set db = Nothing
答案 2 :(得分:0)
好的,所以我一直在寻找一个简单的搜索元素...... 我尝试过使用子表单,我尝试过记录集。所有这些都给了我信息。但不是我想要的紧凑性,谢谢!
使用DOA stuf很棒但不适合我的应用程序。 使用上面的内容:
Me.AIPResultTxt.Value = DLookup("[AIP Name]", "Table1", _
"[AIP ID]='" & Me.AIPIDTxt & "'")
我有正在寻找的代码的紧凑性... 谢谢!!! Ť