Excel VBA Access Query

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,600
Office Version
  1. 365
Platform
  1. Windows
I have an Access Table that holds the following fields with examples of their content
  • Name (A Smith)
  • Date (3rd Jan 2020)
  • Day (Friday)
  • Rate (172.50)
I need to return a list in a date range that will show the following
  • Name
  • Count of weekdays (not Saturday or Sunday)
  • Count of Rate where value is > 0.00

The list needs to show by distinct name only and to show the totals rather than a line per record.


Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Create a Query in the database that uses the fields you want and then add an expression to format the date field to show the days of the week, then set the criteria to exclude Saturday and Sunday. Finally group against the fields you want to count against rather than sum. Example of the expression is this:

VBA Code:
Expr1: Format([Date],"dddd")

Criteria for excluding the dates is this:

VBA Code:
<>"Saturday" And <>"Sunday"
 
Upvote 0
Thanks for this Trevor.

I'm building this query in Excel VBA rather than in Access.

I am OK with simple queries when doing it this way but I really struggle with returning unique values and counts, sums, etc.

The format I'm used to is as follows
SELECT Exp_Employee, Exp_Type, Nominal_Code, Item_Description, Purchase_Reason, Net, VAT, Gross FROM Expenses WHERE IsNull(Exp_Pay_Pack) OR Exp_Pay_Pack=

I know I should really write these in Access first to test and then convert to Excel VBA but it's something I never got round to learning properly.......
 
Upvote 0
If that's your preference then why not get the data in and create a pivot table via VBA to give you the results you want.
 
Upvote 0
Here is an example of the SQL code behind the query in Access to do grouping and counting. I hope it helps you find a solution.

VBA Code:
SELECT tableName.LastName, Count(tableName. Amount) AS CountOfAmount, tableName.Date

FROM tableName

GROUP BY tableName.LastName, tableName. Date

HAVING (((tableName. Date) Between #4/1/2019# And #4/30/2019#));
 
Upvote 0
This line can be used to not include Saturdays and Sundays.

VBA Code:
WHERE (((Format([Date],"dddd"))<>"Saturday" And (Format([Date],"dddd"))<>"Sunday"));
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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