没有取回查询结果
Not getting query results back
我的查询没有 return 任何值,即使 table 有记录。我正在尝试根据输入的姓名检索员工 ID。我不断收到消息 "No employee id"。就 Access VBA 而言,我是一名新手。我使用 Access tables 和其他 tables 没有问题。我确实验证了表单字段具有正确的值并且被捕获在变量 strEmpName
中
Set cnn1 = New ADODB.Connection
mydb = "C:\accesssamp\Documents\Tasks.accdb"
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mydb
cnn1.Open strCnn
'This statement added here just to indicate that I am getting the value
strEmpName = cboEmployeeName.Text ' Getting employee name from form field
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = CurrentProject.Connection
.CommandText = "SELECT [EmployeeId] FROM [Employees] " & _
"WHERE [EmployeeName] = [strEmpName]"
.CommandType = adCmdUnknown
.Parameters.Append cmd.CreateParameter( _
"[strEmpName]", adChar, adParamInput, 50)
.Parameters("[strEmpName]") = strEmpName
End With
' Execute the Query and return the employeeId
Set rstEmp = cmd.Execute
If rstEmp.RecordCount < 1 Then
MsgBox "No Employee Id"
Else
MsgBox rstEmp(0).Value
End If
您的示例存在多个问题。我不确定这是否正是您想要的,但它在我的系统上运行正常。
With cmd
'.ActiveConnection = CurrentProject.Connection
.ActiveConnection = cnn1
.CommandText = "SELECT [EmployeeId] FROM [Employees] " & _
"WHERE [EmployeeName] = [strEmpName]"
.CommandType = adCmdUnknown ' or adCmdText; either works
.Parameters.Append cmd.CreateParameter( _
"strEmpName", adVarChar, adParamInput, 255, strEmpName)
End With
' Execute the Query and return the employeeId
Set rstEmp = cmd.Execute
'If rstEmp.RecordCount < 1 Then
If rstEmp.BOF And rstEmp.EOF Then
MsgBox "No Employee Id"
Else
MsgBox rstEmp(0).value
End If
备注:
- 我假设您想 运行 从 cnn1 连接到另一个数据库而不是
CurrentProject.Connection
. 的查询
- 为
CreateParameter
提供兼容的数据类型。对于文本参数,为其最大长度提供一个值。最后包括参数的值。
rstEmp.RecordCount
返回 -1,小于 1,因此即使记录集不为空,您的代码也会显示 "No Employee Id"。不是检查 RecordCount
,而是检查记录集是否为空。
我的查询没有 return 任何值,即使 table 有记录。我正在尝试根据输入的姓名检索员工 ID。我不断收到消息 "No employee id"。就 Access VBA 而言,我是一名新手。我使用 Access tables 和其他 tables 没有问题。我确实验证了表单字段具有正确的值并且被捕获在变量 strEmpName
中 Set cnn1 = New ADODB.Connection
mydb = "C:\accesssamp\Documents\Tasks.accdb"
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mydb
cnn1.Open strCnn
'This statement added here just to indicate that I am getting the value
strEmpName = cboEmployeeName.Text ' Getting employee name from form field
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = CurrentProject.Connection
.CommandText = "SELECT [EmployeeId] FROM [Employees] " & _
"WHERE [EmployeeName] = [strEmpName]"
.CommandType = adCmdUnknown
.Parameters.Append cmd.CreateParameter( _
"[strEmpName]", adChar, adParamInput, 50)
.Parameters("[strEmpName]") = strEmpName
End With
' Execute the Query and return the employeeId
Set rstEmp = cmd.Execute
If rstEmp.RecordCount < 1 Then
MsgBox "No Employee Id"
Else
MsgBox rstEmp(0).Value
End If
您的示例存在多个问题。我不确定这是否正是您想要的,但它在我的系统上运行正常。
With cmd
'.ActiveConnection = CurrentProject.Connection
.ActiveConnection = cnn1
.CommandText = "SELECT [EmployeeId] FROM [Employees] " & _
"WHERE [EmployeeName] = [strEmpName]"
.CommandType = adCmdUnknown ' or adCmdText; either works
.Parameters.Append cmd.CreateParameter( _
"strEmpName", adVarChar, adParamInput, 255, strEmpName)
End With
' Execute the Query and return the employeeId
Set rstEmp = cmd.Execute
'If rstEmp.RecordCount < 1 Then
If rstEmp.BOF And rstEmp.EOF Then
MsgBox "No Employee Id"
Else
MsgBox rstEmp(0).value
End If
备注:
- 我假设您想 运行 从 cnn1 连接到另一个数据库而不是
CurrentProject.Connection
. 的查询
- 为
CreateParameter
提供兼容的数据类型。对于文本参数,为其最大长度提供一个值。最后包括参数的值。 rstEmp.RecordCount
返回 -1,小于 1,因此即使记录集不为空,您的代码也会显示 "No Employee Id"。不是检查RecordCount
,而是检查记录集是否为空。