簡體   English   中英

SQL查詢從訪問表中提取員工詳細信息

[英]SQL query to extract employee details from access table

我是Excel VBA的新手。 我有一個用戶表單,試圖在其中填充AMO雇員的姓名。 我有一個名為Ofc的數據庫。 在下面有一個表EmployeeDetails 主鍵是PeoplesoftId

這是Employee表的結構和內容:

PeoplesoftId  Nameofemployee  RacifId  Employeeid  Designation
43243309      Maddala         V43309   99651823    AMO
43243310      Abhishek        A43301   99651824    AMO
43243311      Atanu           A43311   99651825    MO
43243312      Rajiv           R43312   99651826    CSE

這是我到目前為止編寫的代碼:

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 SQL As String
Dim i As Integer
Dim var
'add error handling
On Error GoTo errHandler:
'Disable screen flickering.
Application.ScreenUpdating = False
dbPath = "E:\office_hsbc\ofc.accdb"
var = "AMO"
Set cnn = New ADODB.Connection ' Initialise the collection class variable
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
cnn.Open
SQL = "SELECT Nameofemployee FROM EmployeeDetails where Designation= '" & var & "'"
Set rs = New ADODB.Recordset 'assign memory to the recordset
rs.Open SQL, cnn
If rs.EOF And rs.BOF Then
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing
'Enable the screen.
Application.ScreenUpdating = True
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
For i = 0 To rs.Fields.Count - 1
    comboamo.AddItem rs.Fields(i).Value, i
Next
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
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"

您需要遍歷記錄集中的每個記錄。 當前,您正在嘗試從一條記錄中讀取所有字段,但是您的查詢僅返回一個字段。 嘗試以下方法:

MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
i = 0
Do Until rs.EOF
    comboamo.AddItem rs.Fields("Nameofemployee").Value, i
    rs.MoveNext
    i = i + 1
Loop
rs.Close

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM