LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Calculating a date within the OLE DB query

How have you implemented this query? Is it via MSQuery, or hard-coded in VBA
with ADO or DAO, or some other method?

If done in code: You must be creating the SQL as text, either explicitly
(e.g. MyRs.Open "SELECT reqID, reqDate,...") or using a string variable
(SQLStr = "SELECT reqID, reqDate,"; MyRs.Open SQLStr...). In that case
calculate your date first and store it - formatted properly - in a string
variable, and use it in the SQL text; e.g:

MyDate = NOW() - ((WEEKDAY(NOW(),3)) + 21)
SQLStr = "SELECT reqID, reqDate,... AND completeDate '" & MyDate & "' and
requestsTbl.userID = userTbl.userID..."
Note the need to put the single quotes inside the text.

If using MSQuery, calculate your date in a cell and use the cell as a
parameter. Parameters in MSQuery are designated by using a name in [] braces
inside the criteria grid; e.g. [Start Date]. When you are back in Excel you
can right-click on the query result table and select Parameters... to specify
that the value for Start Date comes from the cell where you have entered your
formula. See MSQuery help file for more info.

The other option if using MSQuery is to set up the simple query and then
manually edit the query text. This is found in the Querytables.CommandText
property, and it is read/write, so you can read the text string as prepared
by MSQuery and do a replace function to change the portion you need to change
before you update the query. E.g:

With Worksheets("SheetName").QueryTables(1)
.CommandText = Replace(.CommandText, "10/10/2005", Format(NOW() -
((WEEKDAY(NOW(),3)) + 21),"mm/dd/yyyy"))
.Refresh
End With

--
- K Dales


"Gary Sakaguchi" wrote:

Within my OLE DB query, I would like to use a calculated date for the
selection criteria. I'm using Excel to create a summary report against a
SQL database. Completion date is one of the fields, I only want to select
those records that are completed on/after the Monday three weeks ago, NOW() -
((WEEKDAY(NOW(),3)) + 21). The report shows the prior three weeks, plus the
current week's information.

Copy of the OLE DB query - I would like to replace the "10/10/2005" with the
calculated date.

SELECT reqID, reqDate, fName, lName, inputTbl.userInput, adminsTbl.admFname,
completeDate FROM requestsTbl, userTbl, InputTbl, adminsTbl WHERE
(requestsTbl.reqDate LIKE '%2005%') AND completeDate '10/10/2005' and
requestsTbl.userID = userTbl.userID AND requestsTbl.userInputID =
inputTbl.userInputID AND (requestsTbl.assignedTo LIKE adminsTbl.adminID)
ORDER BY completeDate, admFname, reqID

Thanks,

Gary Sakaguchi

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resolved my query regarding total sum calculating tushar Excel Discussion (Misc queries) 1 May 27th 09 11:32 AM
Calculating Difference Between Start Date & Time And End Date & Ti Samwar Excel Discussion (Misc queries) 2 December 19th 05 12:42 PM
Calculating recurring date in following month, calculating # days in that period Walterius Excel Worksheet Functions 6 June 4th 05 11:21 PM
Calculating days between current date and a date in future NETWORKDAYS() function Faheem Khan Excel Worksheet Functions 2 February 10th 05 07:18 PM
Using imported Query & Calculating Dates Woodkat Excel Worksheet Functions 1 December 8th 04 05:15 PM


All times are GMT +1. The time now is 12:23 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"