VBA to query Access DB

u742884

Board Regular
Joined
Jun 23, 2002
Messages
126
I have written/recorded code to pull selected fields from an Access table based on two criteria: BuyerNo and CalYear. This code works fine to pull for one BuyerNo where CalYear = 2006. It is pulling in a row for each month of the year.

"SELECT tblBuyerPlans.BUYERNO, tblBuyerPlans.DEPTNO, tblBuyerPlans.CLASS, tblBuyerPlans.CALYEAR, tblBuyerPlans.MONTHNAME, tblBuyerPlans.MONTHNO, tblBuyerPlans.BOMSTOCK, tblBuyerPlans.SALES, tblBuyerPla" _
, _
"ns.MD, tblBuyerPlans.MDPCT, tblBuyerPlans.NETREC, tblBuyerPlans.INVADJUST, tblBuyerPlans.EOMSTOCK, tblBuyerPlans.AVGSTOCKYT, tblBuyerPlans.TURNYTD" & Chr(13) & "" & Chr(10) & "FROM `" & MyPath & "Plan" _
, "`.tblBuyerPlans tblBuyerPlans" & Chr(13) & "" & Chr(10) & "WHERE (tblBuyerPlans.BUYERNO=" & Buyer2Query & ") AND (tblBuyerPlans.CALYEAR=" & Year2Query & ")")

Problem is that we use a fiscal year running from Feb to Jan. Jan 2007 is part of fiscal 2006, but its CalYear is 2007. MonthNo for Jan is 12. I need to change this part of code:
WHERE (tblBuyerPlans.BUYERNO=" & Buyer2Query & ") AND (tblBuyerPlans.CALYEAR=" & Year2Query & ")

to something like:
WHERE (((tblBuyerPlans.BUYERNO=" & Buyer2Query & ") AND (tblBuyerPlans.CALYEAR=" & Year2Query & ")) OR ((tbllBuyerPlans.BUYERNO=" & Buyer2Query & ") AND (tblBuyerPlans.CALYEAR=" & Year2Query+1 & ") AND tblBuyerPlans.MONTHNO=12)))

but I cannot seem to get the syntax correct for the query to run. Any ideas?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
It's possible to transform the data using SQL but you may be better off going into the db and creating a query where you define Fiscal Year and Fiscal Month.
Then pull that through to Excel and your code will be much easier to work with.

Denis
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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