问题
I am having trouble using ADODB to search data in a closed workbook, narrow by team (think A, B, C, D) and Date and fill a userform in the active workbook with the data from the closed workbook. My first issue is with getting the connection string to work, the file I am trying to reach is in the same directory but ThisWorkbook.Path doesn't seem to be working. The data I am trying to pull is Team, Date, Product, Staffing, Processing Issues, and Packaging Issues. Also I am unsure how to use the data after I have selected it. This is code taken from another answer which I am having trouble fitting to my needs.
Sub ADOGetRange()
Dim lastRow As Long, x As Long
Const adOpenKeyset = 1
Const adLockOptimistic = 3
Dim conn
Dim EmployeeData
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Workbooks(ThisWorkbook.Path) & "\Line 1 - EOS Database Rev A.xlsm[Line 1 Database$]";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
conn.Open
' On Error GoTo CloseConnection
Set L1EOSData = CreateObject("ADODB.Recordset")
With L1EOSData
.ActiveConnection = conn
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Source = "SELECT * FROM [Line 1 Database$] WHERE [Team]='" & cboL1Team.Value & "" And [Date] = "#" & DateValue(cboL1Date.Value) & "#"
.Open
' On Error GoTo CloseRecordset
End With
CloseRecordset:
L1EOSData.Close
Set L1EOSData = Nothing
CloseConnection:
conn.Close
Set conn = Nothing
End Sub
回答1:
You will have to adjust the field names accordingly.
Private Sub cboL1Date_Change()
' If cboL1Team.ListIndex > -1 Then ReadWriteRecord enReadRecord
If cboL1Team.ListIndex > -1 Then ADOGetRange
End Sub
Private Sub cboL1Team_Change()
' If cboL1Date.ListIndex > -1 Then ReadWriteRecord enReadRecord
If cboL1Date.ListIndex > -1 Then ADOGetRange
End Sub
Sub ADOGetRange()
Const adOpenKeyset = 1
Const adLockOptimistic = 3
Dim conn
Dim L1EOSData
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Line 1 - EOS Database Rev A.xlsm;Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
conn.Open
' On Error GoTo CloseConnection
Set L1EOSData = CreateObject("ADODB.Recordset")
With L1EOSData
.ActiveConnection = conn
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Source = "SELECT * FROM [Line 1 Database$] WHERE [Team]='" & cboL1Team.Value & "' And [Date] = #" & DateValue(cboL1Date.Value) & "#"
.Open
' On Error GoTo CloseRecordset
If Not .BOF Or Not .EOF Then
cboL1Product.Value = .Fields("Product").Value
cboL1Staffing.Value = .Fields("Staffing").Value
txtL1Pounds.Value = .Fields("Pounds").Value
txtL1Processing.Value = .Fields("Processing").Value
txtL1Packaging.Value = .Fields("Packaging").Value
End If
End With
CloseRecordset:
L1EOSData.Close
Set L1EOSData = Nothing
CloseConnection:
conn.Close
Set conn = Nothing
End Sub
来源:https://stackoverflow.com/questions/39952543/fill-userform-with-data-from-closed-workbook-based-on-2-criteria