Sub GetItFromAccess()
Application.ScreenUpdating = False
Sheets("Import").Activate
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim xlApp As Object
' Dim xlWb As Object
Dim xlWs As Object
Dim recArray As Variant
Dim strDB As String
Dim fldCount As Integer
Dim recCount As Long
Dim iCol As Integer
Dim iRow As Integer
' Set the string to the path of the database
strDB = "H:\Payroll Stuff\My Payroll Database.mdb;" & "Jet OLEDB
atabase Password=klasflkd"
' Open connection to the database
cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDB & ";"
' Open recordset based on Access Payroll XYZ table
rst.Open "SELECT Social,[Unit Type], val(format(Units,""0.00""))AS Units,val(format(Wages,""0.00""))AS Wages,SubAcct AS Sub "
&
_
" FROM [Prod Rpt Data] ;", cnt '& _
'"GROUP BY [Acct],SubAcct,Social,[Unit Type];"
Range("Payroll1").Activate
Range(ActiveCell, ActiveCell.End(xlDown).End(xlToRight)).Select
Selection.Cells.Clear
' Copy field names to the first row of the worksheet
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
ActiveSheet.Cells(3, iCol).Value = rst.Fields(iCol - 1).Name
Next
' Copy the recordset to the worksheet, starting in cell A2
ActiveSheet.Cells(4, 1).CopyFromRecordset rst
ActiveSheet.Cells(3, 1).Activate
Names.Add Name:="Payroll1", RefersTo:=ActiveCell
' Auto-fit the column widths and row heights
Selection.CurrentRegion.Columns.AutoFit
Selection.CurrentRegion.Rows.AutoFit
Range(ActiveCell, ActiveCell.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.MergeCells = False
End With
Selection.Font.Bold = True
Range("Payroll1").Offset(1, 2).Activate
Range(ActiveCell, ActiveCell.Offset(0, 1).End(xlDown)).Select
Selection.NumberFormat = "#,##0.00"
' Close ADO objects
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
' Cells(4, 3).Activate
' Range(ActiveCell, ActiveCell.Offset(0, 1).End(xlDown)).Select
' Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
Cells(3, 1).Activate
Range(ActiveCell, ActiveCell.Offset(0, 4).End(xlDown)).Select
Names.Add Name:="Payroll", RefersTo:=Selection
End Sub
HTH