Results 1 to 10 of 10
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Create Array

    I am using ADO to query SQL Server and the result set that is returned I want to store in an arry. Should be pretty straight-forward, but for some reason, my count returns a -1? This is my syntax, what should I change in order to have the results store in the array? If I run my syntax directly against my SQL Server installation, there are 8 rows returned. What do I need to alter in order for the accurate data to be stored in the array?

    Code:
    Public Function PrintIT()
    Dim c As ADODB.Connection
    Dim r As ADODB.Recordset
    Dim f As ADODB.Field
    Dim i As Integer
    Dim conn As String
    Dim arrResults() As Variant
    Dim pth As Variant
    Set c = New ADODB.Connection
    Dim accapp As Access.Application
    Set accapp = New Access.Application
    With c
        .Provider = "sqloledb.1"
        With .Properties
            .Item("Data Source") = "Server"
            .Item("Initial Catalog") = "database"
            .Item("PassWord") = "password"
            .Item("User ID") = "userid"
        End With
        .Open
        Set r = c.Execute("SELECT DISTINCT(redlight) FROM [Database].[dbo].[Table]")
        i = 0
        r.MoveFirst
        Do Until r.EOF
            arrResults(i) = r.Fields(0)
            i = i + 1
        Loop
        r.Close
        Set rst = Nothing
        c.Close
        For Each pth In arrResults
    		Debug.Print (pth)
        Next pth
    End With
    End Function


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    arrays went away back in the 80s.
    A recordset IS an array,
    a collection is a smart array.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by ranman256 View Post
    arrays went away back in the 80s.
    A recordset IS an array,
    a collection is a smart array.
    How would I take the recordset returned from my query and iterate? For example, let's say that my sql query returned
    Code:
    Red
    Blue
    Green
    Orange
    How would I iterate that, like I try to do in my foreach loop below?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Second the motion on arrays. The only time I use them is when I need the Split() function to break a string down. Your Do loop does the same thing as the For loop. You'd do what you need with the items there.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by pbaldy View Post
    Second the motion on arrays. The only time I use them is when I need the Split() function to break a string down. Your Do loop does the same thing as the For loop. You'd do what you need with the items there.
    How do I access the elements in the returned result set? For example, in an array I would use
    Code:
        
    For Each dbPath In arrResults
        Debug.Print (dbPath)
    Next dbPath
    What would go here?
    Code:
    	Do Until r.EOF
            Debug.Print (?)
        Loop

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Using your own code:

    Debug.Print r.Fields(0)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    You can iterate arrResults using the following. LBound =lower bound Ubound=Upper Bound

    Code:
    For i =Lbound(arrResults) to Ubound(arrResults)
      Debug.print arrResults(i)
    Next i
    I often use arrays when creating test data. And with Split as Paul mentioned.

  8. #8
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by pbaldy View Post
    Using your own code:

    Debug.Print r.Fields(0)
    This never leaves the 1st result returned from my select statement. How do I iterate all results returned?
    Code:
        Do Until r.EOF
            Debug.Print r.Fields(0)
        Loop

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sorry, missed that you didn't have a MoveNext:

    Code:
      Do Until r.EOF
            Debug.Print r.Fields(0)
            r.MoveNext
      Loop
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    And if that misses a record I always use:

    Do While Not r.EOF
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Create objects from array or other
    By Ruegen in forum Programming
    Replies: 8
    Last Post: 03-25-2015, 05:43 AM
  2. Need help with array
    By programmingbeginner in forum Programming
    Replies: 14
    Last Post: 08-28-2014, 01:30 PM
  3. Replies: 6
    Last Post: 05-31-2013, 07:46 PM
  4. Would be an ARRAY statement in SAS.
    By arbnmedic in forum Access
    Replies: 2
    Last Post: 01-07-2012, 03:34 PM
  5. can i put the result in array?
    By dada in forum Programming
    Replies: 1
    Last Post: 08-19-2010, 07:17 PM

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