Question

I have a stored query below in Access call "1PreviousEWBS":

SELECT f.ewbs AS EWBS, SUM(f.[total hours]) AS [Total Hours], SUM(f.[total cost]) AS [Total Cost],                                 T.description AS Activity, T.AWBS AS AWBS
FROM FindHrCostByEWBS AS f, AllProjectsCodes AS T
WHERE (f.project)=Forms!x!cboProjects 
And f.[period end date]<Forms!x!cboDate     
And f.ewbs=T.oldewbs     
GROUP BY f.ewbs,T.description, T.AWBS;

Bascially I wrote this so users can use the combo box to select their data.

But now, I am curious to see how can i set the parameters directly through VBA. I had something like this set up.

Sub TestSeePreviousData

     Dim rS As DAO.Recordset
     Dim db As DAO.Database
     Dim queryDef As DAO.queryDef

    Dim ProjectNumber As String
    Dim FiscalDate As Date
    FiscalDate = #4/25/2014#

    Set db = CurrentDb
    Set queryDef = db.QueryDefs("1PreviousEWBS")

    'Parameter 0 is to input the Project number
     queryDef.Parameters(0) = "173221"
     'Parameter 1 is to give the fiscal period end date
     queryDef.Parameters(1) = "'" & FiscalDate & "


   Set rS = queryDef.OpenRecordset

    Do While Not rS.EOF
        'The lines for 1PreviousEWBS
        Debug.Print rS.Fields("EWBS") & vbTab & rS.Fields("AWBS") & vbTab & rS.Fields("Total Hours") & vbTab &     rS.Fields("Total Cost")


        rS.MoveNext
    Loop


End Sub

My problem is, I want to recreate the initial "less than" condition, ie

f.[period end date] < [Input Date], and this:

queryDef.Parameters(1) < "'" & FiscalDate & "'"

does not work, it returns an error message of "Invalid Use of Properties"

my question is, how can i show inequal condition when setting parameters for SQL in vba?

Was it helpful?

Solution

I believe the comparison operator should be hard-coded in the QueryDef SQL, and the Parameter which represents the fiscal date is the only one which needs to be set at runtime. Have you tried that?

And not sure if your query 1PreviousEWBS is using the same exact SQL you listed, but you would change the Form reference (Forms!x!cboDate) to a standard parameter reference - [pFiscalDate] - for example.

Your code should look to replace [pFiscalDate] at run-time, and the < should be part of the QueryDef code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top