我无法使用ADODB在已关闭的工作簿中搜索数据,按团队缩小(考虑A,B,C,D)和日期,并使用已关闭工作簿中的数据填充活动工作簿中的用户表单。我的第一个问题是让连接字符串工作,我试图访问的文件位于同一目录但ThisWorkbook.Path似乎没有工作。我想要提取的数据是团队,日期,产品,人员配置,处理问题和包装问题。我也不确定在选择数据后如何使用数据。这是从另一个答案中获取的代码,我无法满足我的需求。
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
答案 0 :(得分:0)
您必须相应地调整字段名称。
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