Query help

jcaptchaos2

Well-known Member
Joined
Sep 24, 2002
Messages
1,032
Office Version
  1. 365
Platform
  1. 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



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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I think the easiest way would be to edit the query in the database or create a new one that will only pull the columns that you want.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top