Option Explicit
'*******************************************************************
'
'Written by Andrew Fergus (andrew93) in response to this question :
'http://www.mrexcel.com/board2/viewtopic.php?p=1125318
'on MrExcel.com
'
'Give credit where due if you copy this code! Cheers :-)
'
'*******************************************************************
'Public Variables
Dim objExcel As Object, objBook As Object, objSheet As Object
Dim objDBase As Database, rsDBase As Recordset
Dim dbFileName As String, xlFileName As String, RowCount As Double
Private Sub cmdGetDB_Click()
Dim Counter As Integer
On Error GoTo Error_Handler
'Open the 'file open' dialogue box
cdiagdb.ShowOpen
'Get / set the Access database file name and location
lbldbFileName.Caption = cdiagdb.FileName
dbFileName = cdiagdb.FileName
'Open the database and get the table names
Set objDBase = OpenDatabase(dbFileName, False, False)
For Counter = 0 To objDBase.TableDefs.Count - 1
If Left((objDBase.TableDefs(Counter).Name), 4) <> "MSys" Then
List1.AddItem (objDBase.TableDefs(Counter).Name)
End If
Next Counter
Exit Sub
Error_Handler:
If Err.Number = 32755 Then
'User pressed Cancel
DoEvents
Else
MsgBox Err.Description, vbCritical, "Error " & Err.Number
List1.Clear
lbldbFileName.Caption = ""
dbFileName = ""
End If
Exit Sub
End Sub
Private Sub cmdGetSS_Click()
On Error GoTo Error_Handler
'Open the 'file open' dialogue box
cdiagxl.ShowOpen
'Get /set the spreadsheet name and location
lblxlFileName.Caption = cdiagxl.FileName
xlFileName = cdiagxl.FileName
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Open(xlFileName)
'Select the first worksheet in the spreadsheet
Set objSheet = objBook.Worksheets(1)
Exit Sub
Error_Handler:
If Err.Number = 32755 Then
'User pressed Cancel
DoEvents
Else
MsgBox Err.Description, vbCritical, "Error " & Err.Number
lblxlFileName.Caption = ""
xlFileName = ""
End If
Exit Sub
End Sub
Private Sub cmdCopy_Click()
Dim Counter As Integer, InnerLoop As Integer
On Error GoTo Err_Handler
'Exit if nothing has been selected
If List1.SelCount = 0 Then
Exit Sub
ElseIf dbFileName = "" Or xlFileName = "" Then
'Exit if either a database or a spreadsheet have not been selected
MsgBox "Please select a database and spreadsheet", vbInformation, "Error"
Exit Sub
End If
'Set the global row counter
RowCount = 1
'Activate the workbook/worksheet
objSheet.Activate
Range("A1").Activate
'Select all cells
Cells.Select
'Clear the contents
Selection.ClearContents
Selection.Font.Bold = False
Range("A1").Activate
'Loop through the list of tables / recordsets
For Counter = 0 To List1.ListCount - 1
If List1.Selected(Counter) = False Then
'Item was not selected
DoEvents
Else
'Item was selected
'Get the file name and open the recordset
Set rsDBase = objDBase.OpenRecordset(List1.List(Counter))
'Write the table name into the spreadsheet
objSheet.Cells(RowCount, 1).Value = "Table : " & rsDBase.Name
'Bold the table name
objSheet.Cells(RowCount, 1).Font.Bold = True
'Proceed to the next row
RowCount = RowCount + 1
'Loop through the field name and copy them into the spreadsheet
For InnerLoop = 0 To rsDBase.Fields.Count - 1
objSheet.Cells(RowCount, InnerLoop + 1).Value = rsDBase.Fields(InnerLoop).Name
Next InnerLoop
'Bold the field names
objSheet.Range(objSheet.Cells(RowCount, 1), objSheet.Cells(RowCount, rsDBase.Fields.Count)).Font.Bold = True
'Proceed to the next row
RowCount = RowCount + 1
'Copy the recordset into the spreadsheet
objSheet.Cells(RowCount, 1).CopyFromRecordset rsDBase
'Reset the row counter
RowCount = RowCount + 1 + rsDBase.RecordCount
'Clear the recordset
Set rsDBase = Nothing
End If
Next Counter
'Turn off the resume.xlw pop up alert
objExcel.DisplayAlerts = False
'Save the spreadsheet
objExcel.Save
'Close the spreadsheet
objExcel.ActiveWorkbook.Close True
MsgBox "Finished copying the selected table(s)", vbInformation, "Done"
Exit Sub
Err_Handler:
MsgBox Err.Description, vbCritical, "Error " & Err.Number
OrderlyClose
Exit Sub
End Sub
Private Sub OrderlyClose()
On Error Resume Next
'Clear the GUI items on the form
List1.Clear
lbldbFileName.Caption = ""
lblxlFileName.Caption = ""
'Clear the objects and recordset
Set objSheet = Nothing
Set objBook = Nothing
Set objExcel = Nothing
Set rsDBase = Nothing
Set objDBase = Nothing
'Close Excel
objExcel.Quit
End Sub
Private Sub cmdExit_Click()
OrderlyClose
End
End Sub