BustyAshley
Registered User.
- Local time
- Today, 14:36
- Joined
- Sep 22, 2015
- Messages
- 22
I'm currently using the Code below to export a table from Access into a new workbook in excel.
Is there a way to modify this code to do a couple things;
Part A: I want to put a cbBox on my form tab that has a contract (First Column on master table), then use that as an "activate filter" on the Master Table and just export that detail for that one specific contract.
Part B: There are six other tables; table1, table2, table3, table4, table5, table6... I want this same cbBox to filter each of these tables and export the results into the same workbook as Part A, then name the worksheets in the workbook from the table it was taken from.
The end Result, would be a 7 tab workbook, Master Table; table1, table2 table3, table4, table5, table6, showing only the contract selected in the cbBox
Is there a way to modify this code to do a couple things;
Part A: I want to put a cbBox on my form tab that has a contract (First Column on master table), then use that as an "activate filter" on the Master Table and just export that detail for that one specific contract.
Part B: There are six other tables; table1, table2, table3, table4, table5, table6... I want this same cbBox to filter each of these tables and export the results into the same workbook as Part A, then name the worksheets in the workbook from the table it was taken from.
The end Result, would be a 7 tab workbook, Master Table; table1, table2 table3, table4, table5, table6, showing only the contract selected in the cbBox
Code:
Private Sub Command313_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Master Table", dbOpenSnapshot)
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With oSheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
db.Close
End Sub