Results 1 to 9 of 9
  1. #1
    Tyork is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Oct 2010
    Posts
    35

    Load SQL results into an array

    I would like to load the results of a sql query into an array. I am utilizing the following approach which is terribly inefficient:



    Code:
     
        'Calculate number of placements events for selected Client
        strSQL = "SELECT COUNT(*) FROM dbo_VPlacements WHERE Customer = '" & Client & "'"
        Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
        NCount = rs.Fields(0)
     
        ReDim PlacementArray(1 To NCount)
     
        'Load placement numbers into array
     
        For xx = 1 To NCount
            strSQL = "SELECT Top 1 PlacementCount FROM (SELECT Top " & xx & " PlacementCount, PlacementDate FROM (SELECT PlacementCount, PlacementDate FROM dbo_VPlacements WHERE PlacementDate = #" & PriorDay & "# ORDER BY PlacementDate ASC)) ORDER BY PlacementDate"
            Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
            PlacementArray(xx) = rs.Fields("PlacementCount")
     
        Next xx
    I get a syntax error when I just try to set the array equal to the selected recordset. Any ideas?
    Last edited by Tyork; 01-25-2011 at 07:45 AM.

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    some of the technique there looks like another language.


    but regardless, your query returns a long value as a count, not records.

    what do you want out of it, exactly? actual results of one field? or a count of the records returned?

  3. #3
    Tyork is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Oct 2010
    Posts
    35
    There are 2 queries

    1 query returns a count and the other query loop returns the individual records and places them into an array. This is part of a larger function which calculates the standard deviation of a dataset.

    I will need the individual records as well as the count for this and other calcs I'm preforming in the same sub.

  4. #4
    Tyork is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Oct 2010
    Posts
    35
    What I really want it to say:

    PlacementArray() = r.s.fields("PlacementCount")

    but I get a syntax error.

  5. #5
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    so you want to put WHAT into an array?? one field, right? as in, one field, record by record, yes?? line by line?? if that's all, you use GETROWS(), not queries and recordsets.

    in terms of standard dev, why don't you just use the built in function for that? isn't there one?

  6. #6
    Tyork is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Oct 2010
    Posts
    35
    To put it in a bit more context; the table that I'm retrieving the information from has every "placement" that we have received from hundreds of clients. Field Names:

    ID
    PlacementDate
    Customer
    PlacementCount
    PlacementValue


    I'd like to calculate the standard deviation of the number of accounts placed at the client level. Therefore getrows() would not work because I have to retrieve a specific subset of records based on specific criteria. (I've never used getrows() but from what I've read, it cannot do this. Correct me if I'm wrong though)

    I've researched it and there is not a built in vba function to calculate the standard deviation of an array.

  7. #7
    Tyork is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Oct 2010
    Posts
    35
    Actually, DstDevP may work in the SQL. I'll have to try that.

  8. #8
    Tyork is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Oct 2010
    Posts
    35
    You were right this will work

    Code:
    SELECT stDevP(PlacementCount)
    FROM dbo_VPlacements
    WHERE customer = '[Customer]'
    In terms of placing the fetched record set into an array however is there an easier way than I was using when criteria is involved?

  9. #9
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    yes. if your set is not already an object, open a recordset and fetch the rows, like:
    Code:
    dim rs as dao.recordset
    set rs = currentdb.openrecordset("sql")
    
    rs.movelast
    rs.movefirst
    
    arrayvariable = rs.getrows()

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

Similar Threads

  1. dynamic array for calendar
    By workindan in forum Programming
    Replies: 7
    Last Post: 11-12-2010, 01:20 PM
  2. can i put the result in array?
    By dada in forum Programming
    Replies: 1
    Last Post: 08-19-2010, 07:17 PM
  3. Array of labels in runtime
    By ngruson in forum Programming
    Replies: 1
    Last Post: 08-19-2010, 07:30 AM
  4. Building Array
    By jgelpi16 in forum Forms
    Replies: 12
    Last Post: 03-22-2010, 12:33 PM
  5. How to use array? [ solved] Thanks.
    By wasim_sono in forum Programming
    Replies: 0
    Last Post: 10-20-2006, 12:00 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