jcaptchaos2
Well-known Member
- Joined
- Sep 24, 2002
- Messages
- 1,032
- Office Version
- 365
- Platform
- Windows
Hello,
I have this query that pulls data from an access database (all the data), I need help modifying it to pull only certain columns or fields, here is the code, the field names I only want are:
Emp#
Employee Name
Time On
Date Off
Time Off
Pay Time
shiftdate
I have this query that pulls data from an access database (all the data), I need help modifying it to pull only certain columns or fields, here is the code, the field names I only want are:
Emp#
Employee Name
Time On
Date Off
Time Off
Pay Time
shiftdate
Code:
Sub GetData()
'Step 1: Declare your variables
Dim MyDatabase As DAO.Database ' DAO
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset 'DAO
Dim i As Integer
'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase _
("N:\Fishbowl Databases\Time Clock Min.accdb")
Set MyQueryDef = MyDatabase.QueryDefs("Test Query3")
'Step 3: Define the Parameters
With MyQueryDef
.Parameters("[Start Date]") = Range("E2").Value
.Parameters("[End Date]") = Range("E3").Value
End With
'Step 4: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset
'Step 5: Clear previous contents
Sheets("Sheet1").Select
ActiveSheet.Range("A6:Z10000").ClearContents
'Step 6: Copy the recordset to Excel
ActiveSheet.Range("A7").CopyFromRecordset MyRecordset
'Step 7: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(6, i).Value = MyRecordset.Fields(i - 1).Name
Next i
Range("E7:E1000").Select
Selection.NumberFormat = "[$-409]h:mm AM/PM;@"
Columns("G:G").Select
Selection.NumberFormat = "[$-409]h:mm AM/PM;@"
Columns("P:P").Select
Selection.NumberFormat = "[$-409]h:mm AM/PM;@"
Columns("R:R").Select
Selection.NumberFormat = "[$-409]h:mm AM/PM;@"
Range("A7").Select
ActiveWindow.SmallScroll Down:=-9
Rows("7:7").Select
ActiveWindow.FreezePanes = True
Range("A7").Select
MsgBox "Your Query has been Run"
End Sub