Results 1 to 9 of 9
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284

    Using The Results of a Select Query To Provide Value For Variable

    Howdy...

    Not sure I'm in the right place for this one guys & gals - So if I am lost please kindly redirect - Thanks

    I'm developing a SQL select statement in Excel to provide a value for a variable used in a second SQL select statement.

    My approach to this is to get both SQL statements to work and then combine them...They both work but trying to combine them has led me here

    Below is my first SQL statement - Which, when test separately works perfectly as it places the TOP (1) or (2) or (3) in my excel sheet
    Code:
    StrSQL1 = "SELECT TOP (1) [BatchID]" & _
                    "FROM [MyServer].[Schema].[TblData]" & _
                    "WHERE BatchID >= '20190114_050000 AND BatchID<= '20190114_060000'"
    What I'm trying to design here is a way to pull just the first data load in the 5 AM hour

    where, depending on the day and volume can be 3-4 runs at various times within the hour.

    The below also works perfectly as it places the entire RecordSet in my excel sheet as well...
    Code:
    StrSQL2 = "SELECT  [ReportDate], [SalesType], [TotalSales]" & _
                    "FROM [MyServer].[Schema].[TblData]" & _
                    "WHERE BatchID = '20190114_052356'"
    As you can see the immediate above is hard coded

    What I'm trying to get to work is something like the below
    Code:
    StrSQL2 = "SELECT  [ReportDate], [SalesType], [TotalSales]" & _
                    "FROM [MyServer].[Schema].[TblData]" & _
                    "WHERE BatchID = 'StrSQL1' "
    I've tried several variations of assigning the RecordSet value of StrSQL1 to a variable (Dim'ing Strings, Collections, Etc) then replacing the hard coded BatchID with the Variable

    And, well, I'm here so you know how well that's working...

    Thanks As always for any help

    RT91

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you could use a form with 4 text boxes..
    txtDate
    txtStart hour
    txtEnd hour
    txtTop# (manual)

    a timer could run the query 2 or 3 times an hour
    every top of hour, it resets the boxes
    txtDate = date
    time = 6am
    txtStart
    = 5 am
    txtEnd = 6 am

    select * from table where
    WHERE BatchID >= '" & txtDate & "_" & txtStart & "' AND BatchID<= '" &
    txtDate & "_" & txtEnd & "'"

  3. #3
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Hey Ran...

    Genius Solution and something I can use in another project I'm working on - Would have never thought of that...

    But for this project, I'm kinda looking for something a little more tailored - I'm sure there is a way to do this using the SELECT as the means to grab the value of the WHERE Clause variable

    I was just playing with it and got it to work - But on my second attempt -it failed, so I know I'm close or perhaps light years away.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    This is aircode, but should give the right idea.
    You need to create a recordset to extract the BatchID:
    Code:
    dim sBatch as string
    dim sSQL as string
    dim StrSQL2 as string
    dim rst as dao.recordset
    dim db as dao.database
    set db=currentdb
    sSQL="SELECT TOP (1) [BatchID] " & _                
    "FROM [MyServer].[Schema].[TblData] " & _
                    "WHERE BatchID >= '20190114_050000 AND BatchID<= '20190114_060000'"
    set rst =db.createrecordset (sSQL)
    rst movelast
    rst movefirst
    if rst.eof = true and rst.bof = true then
        msgbox "Select is empty"
        exit sub
    endif
    sBatch = rst!BatchID
    StrSQL2 = "SELECT  [ReportDate], [SalesType], [TotalSales] " & _
                    "FROM [MyServer].[Schema].[TblData] " & _
                    "WHERE BatchID = '" & sBatch & "'"
    Last edited by davegri; 01-15-2019 at 11:26 AM. Reason: format/syntax

  5. #5
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Hey Dave - Thanks pitchin' I think this is pretty close - I'm not able to get it to work

    but I think this may be due to my using an ADODB Recordset.

    It wont let me use the CreateRecordset Method - and any workarounds I've come up with have failed

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Should be db DOT Createrecordset as below... Also edited in post #4
    Code:
    set rst =db.createrecordset (sSQL)

  7. #7
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Success!!!

    Ranman & Dave - Thank You!!

    The following seems to work perfectly - However, if anyone sees any potential future flaw - please feel free to say something - Thanks

    Otherwise here is the code
    Code:
    StrSQL1 = "SELECT TOP (1)[BatchID]" & _
              "FROM [RAOCStats].[tri].[tblCRTimeliness]" & _
              "WHERE BatchID >= '20190115_050000' AND BatchID <= '20190115_060000'"
    
    Con.ConnectionString = StrCon
    Con.Open
    Rs.Open StrSQL1, Con
    
    Set Rs = Con.Execute(StrSQL1)
    
    If Rs.EOF = False Then
      Boo = Rs!BatchID
    End If

  8. #8
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Success!!!

    Ranman & Dave - Thank You!!

    The following seems to work perfectly - However, if anyone sees any potential future flaw - please feel free to say something - Thanks

    Otherwise here is the code
    Code:
     StrSQL1 = "SELECT TOP (1)[BatchID]" & _
               "FROM [RAOCStats].[tri].[tblCRTimeliness]" & _
               "WHERE BatchID >= '20190115_050000' AND BatchID <= '20190115_060000'"
    
     Con.ConnectionString = StrCon
     Con.Open
    
     Rs.Open StrSQL1, Con
    
     Set Rs = Con.Execute(StrSQL1)
    
     If Rs.EOF = False Then
       Boo = Rs!BatchID
     End If
    
    StrSQL = "SELECT [ReportDate] ,[WorkType] ,[TotalCases]" & _
             "FROM [RAOCStats].[tri].[tblCRTimeliness]" & _
             "WHERE BatchID =  '" & Boo & "' "
    Rs2.Open StrSQL, Con
    
    Set Rs2 = Con.Execute(StrSQL)
    
    Ws.Range("A1").CopyFromRecordset Rs2
    *Resubmitted first attempt crashed

  9. #9
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Success!!!

    Ranman & Dave - Thank You!!

    The following seems to work perfectly - However, if anyone sees any potential future flaw - please feel free to say something - Thanks

    Otherwise here is the code
    Code:
     StrSQL1 = "SELECT TOP (1)[BatchID]" & _
               "FROM [RAOCStats].[tri].[tblCRTimeliness]" & _
               "WHERE BatchID >= '20190115_050000' AND BatchID <= '20190115_060000'"
    
     Con.ConnectionString = StrCon
     Con.Open
    
     Rs.Open StrSQL1, Con
    
     Set Rs = Con.Execute(StrSQL1)
    
     If Rs.EOF = False Then
       Boo = Rs!BatchID
     End If
    
    StrSQL = "SELECT [ReportDate] ,[WorkType] ,[TotalCases]" & _
             "FROM [RAOCStats].[tri].[tblCRTimeliness]" & _
             "WHERE BatchID =  '" & Boo & "' "
    Rs2.Open StrSQL, Con
    
    Set Rs2 = Con.Execute(StrSQL)
    
    Ws.Range("A1").CopyFromRecordset Rs2
    *Resubmitted first attempt crashed

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 01-04-2019, 10:20 AM
  2. Replies: 4
    Last Post: 05-27-2016, 01:40 PM
  3. Replies: 5
    Last Post: 05-14-2014, 01:17 PM
  4. Provide Ranking & Select on the basis of Rank
    By dolovenature in forum Programming
    Replies: 3
    Last Post: 09-11-2012, 04:46 AM
  5. Replies: 5
    Last Post: 05-18-2012, 07:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums