Results 1 to 5 of 5

Thread: Reusing SQL Select Statement

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    3

    Reusing SQL Select Statement

    Is there a way to reuse a SQL criteria string on the corresponding ADO recordset?

    I have created a query for all employees who have either a 10, 20, 25, 30, or 40 year anniversary in the calendar year. I created seperate SQL criteria strings for each of the anniversary dates which I incorporated into my SQL Select statement. I have succesfully created an ADO recordset of all the above employees.

    My problem is when I go to compile the report (using Excel 2003), I only want to show those anniversary dates in the corresponding columns e.g. 10, 20, 25, 30 or 40.

    If I already have a SQL criteria string for each of the anniveraries, is there a way to say..."If sCriteria10Yr is TRUE then print anniversary date in this column (the 10 year anniversary column) otherwise leave blank".

    Nor sure if you can reuse a criteria string within an If/Then in VBA.

    I appreciate any insight anyone has to offer.
    Thanks

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Reusing SQL Select Statement

    Nor sure if you can reuse a criteria string within an If/Then in VBA.
    of course, but may be slow

    you may be able to filter the recordset for each annivesary and process in turn
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    3

    Re: Reusing SQL Select Statement

    WestConn1, thanks for the reply.

    If it can be done, how would it be done in vba?
    My current code does not want to cooperate e.g. "If sCriteria10Yr then"
    As far as speed is concerened, the If/Then will be performed on the existing ADO Recordset, so the speed issue should be unnoticeable to the naked eye.

    Thanks again for all your help.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Reusing SQL Select Statement

    what is your complete sql query to produce the recordset?
    what is the content of scriteria10yr?
    is it a boolean, date or other?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    3

    Re: Reusing SQL Select Statement

    Code:
    rsEmp.Open "SELECT * FROM UPEMPL WHERE " + sEmpCriteria, cnEmp, adOpenStatic, adLockBatchOptimistic
    
    sEmpCriteria = (CLASS1='S1') 
    AND (STATUS=1) 
    AND (((substring(cast(HIREDATE as CHAR(8)),5,2)>='01') 
    AND (substring(cast(HIREDATE as CHAR(8)),5,2)<='12') 
    AND (substring(cast(HIREDATE as CHAR(8)),7,2)>='01') 
    AND (substring(cast(HIREDATE as CHAR(8)),7,2)<='31') 
    AND (substring(cast(HIREDATE as CHAR(8)),1,4)+10>='2011') 
    AND (substring(cast(HIREDATE as CHAR(8)),1,4)+10<='2011')) 
        OR ((substring(cast(HIREDATE as CHAR(8)),5,2)>='01') 
    AND (substring(cast(HIREDATE as CHAR(8)),5,2)<='12') 
    AND (substring(cast(HIREDATE as CHAR(8)),7,2)>='01') 
    AND (substring(cast(HIREDATE as CHAR(8)),7,2)<='31') 
    AND (substring(cast(HIREDATE as CHAR(8)),1,4)+20>='2011') 
    AND (substring(cast(HIREDATE as CHAR(8)),1,4)+20<='2011')))
    sEmpCriteria is a concatenation of sCriteria1 through sCriteria8 representing all the various possiblities (user report selections).
    Code:
    sCriteria4 = (substring(cast(HIREDATE as CHAR(8)),5,2)>='01') 
    AND (substring(cast(HIREDATE as CHAR(8)),5,2)<='12') 
    AND (substring(cast(HIREDATE as CHAR(8)),7,2)>='01') 
    AND (substring(cast(HIREDATE as CHAR(8)),7,2)<='31') 
    AND (substring(cast(HIREDATE as CHAR(8)),1,4)+10>='2011') 
    AND (substring(cast(HIREDATE as CHAR(8)),1,4)+10<='2011')
    sCriteria4 represents 10 year anniversary criteria (hence the "+10"). I want to use this same criteria (sCriteria4) in my VBA code to decide whether or not to insert the 10 year anniversary date into the 10 Year Column on my report. My code is simply.."If sCriteria4 then (insert date into column) Else (leave balnk)...", but it doesn't want to cooperate.

    I would repeat the same process for each of the anniversary dates only inserting those dates which meet the criteria e.g. sCriteria4, sCriteria5, ...etc.

    I appreciate your help.
    Thanks
    Last edited by Hack; Apr 26th, 2011 at 11:08 AM. Reason: Added Formatting And Code Tags For Readibility

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width