即使表中有記錄,我的查詢也不會返回任何值。我正在嘗試根據輸入的姓名檢索職業編號。我不斷收到消息「沒有員工ID」。就Access VBA而言,我是一個新的學習者。我曾與Access表和其他表沒有問題。我沒有驗證表單字段具有正確的值和變量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
'Debug.Print'告訴你什麼? – PaulFrancis 2015-02-10 16:42:31
@PaulFrancis我在哪裏添加該語句?我瀏覽了代碼並得到了消息框「沒有員工ID」 – rajeev 2015-02-10 16:49:24
RecordCount對ADO來說可能有點奇怪,儘管使用了立即窗口來獲得'firstEmp(0).Value'。爲什麼使用MS Access而不是DAO使用ADO? DAO是首選。 – Fionnuala 2015-02-10 16:51:34