Barkleflem
New member
- Local time
- Today, 15:16
- Joined
- Aug 16, 2023
- Messages
- 7
Hi,
I'm a beginner at VBA and SQL and I've hit an issue trying to build a function which searches and selects records in a closed workbook and pastes the results in the active workbook. The problem I'm having is that only eight records are being returned, despite more matching the criteria.
The search runs across 12 sheets, each one covering a single month, looking for matching names. If the results exceed 8 records, nothing is pasted into the active workbook. The fields aren't particularly long and I'm not interested in very many columns. Below is the code I've written so far, followed by an image of the returned records.
Any suggestions, clarification or assistance would be well received.
I'm a beginner at VBA and SQL and I've hit an issue trying to build a function which searches and selects records in a closed workbook and pastes the results in the active workbook. The problem I'm having is that only eight records are being returned, despite more matching the criteria.
The search runs across 12 sheets, each one covering a single month, looking for matching names. If the results exceed 8 records, nothing is pasted into the active workbook. The fields aren't particularly long and I'm not interested in very many columns. Below is the code I've written so far, followed by an image of the returned records.
Any suggestions, clarification or assistance would be well received.
Code:
Private Sub cmbSearch_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim SSearch As String
SSearch = (txtSearch.Text)
Sheets("DailySearch").Range("A1:Z100").ClearContents
Set cn = New ADODB.Connection
cn.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=\\DUCH-3900\Environment\ENV-SYS2\RSAFETY\SCHOOL CROSSING PATROL GROUP\SCP OPS Logs\2023 LOGS\TestData\Daily Log 2023.xlsx;" & _
"Extended Properties='Excel 12.0 Xml;HDR=No';"
cn.Open
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn
rs.Source = "SELECT * FROM [Jan$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [Feb$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [Mar$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [April$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [May$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [June$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [July$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [Aug$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [Sept$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [Oct$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [Nov$] WHERE [F3] = '" & SSearch & "'"
rs.Source = "SELECT * FROM [Dec$] WHERE [F3] = '" & SSearch & "'"
rs.Open
Sheet7.Range("A1").CopyFromRecordset rs
Sheet7.Range("A1").CurrentRegion.EntireColumn.AutoFit
rs.Close
cn.Close
DLogDisplay.ColumnCount = 18
DLogDisplay.RowSource = "DailySearch!B1: AD100"
End Sub