Results 1 to 2 of 2

Thread: How do you populate an array from a database??

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    41

    Question How do you populate an array from a database??

    Ok, I have a database that has AssocNames and AssocIDs in it. I want to go to the database and put the AssocNames in a combobox and put the AssocIDs in an array. Then when someone clicks on a Name in the combobox, I want the corresponding ID to show up in a label. Below is what I have, the combobox gets populated just fine, but Im having problems with my array. It gives me errors saying something like 'Array expected' and it highlights the line where I have 'Redim Preserve....' Help!

    ' NOT SURE IF ITS DECLARED RIGHT
    Dim arrAssociates As String

    Private Sub Populate_assoclist()
    Dim srSQL As String
    Dim s As Integer
    Dim strAssocName As String
    Dim strAssocID As String

    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    srSQL = "SELECT * FROM Associates"
    With rs
    .Source = srSQL
    .ActiveConnection = conn
    .CursorType = adOpenKeyset
    .LockType = adLockOptimistic
    .Open
    rs.MoveFirst
    Do Until rs.EOF
    strAssocName = !AssocName
    strAssocID = !AssocID
    cboAssociates.AddItem !AssocName
    'IT HIGHLIGHTS THIS NEXT LINE'
    ReDim Preserve arrAssociates(s)
    arrAssociates(s) = !AssocID
    rs.MoveNext
    s = s + 1
    Loop
    .Close
    End With

    End Sub

    Private Sub cboAssociates_Change()
    t = cboAssociates.ListIndex
    If (t = -1) Then
    MsgBox "No name selected", vbInformation
    Else
    lblAssociates.Text = arrAssociates(t)
    End If
    End Sub

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Can I make a suggestion. Or Two.

    1) Use naming conventions on your variables - helps when decoding/debugging.
    2) Open the recordset as static (read only) and use the recordcount to redim the array. Something like :
    VB Code:
    1. adoRst.Open
    2. lngMaxRecs = adoRst.RecordCount
    3. Redim Ary(lngMaxRecs)
    4. For lngCurrRec = 0 to (lngMaxRecs-1)
    5.   '---- do processing here
    6.   Ary(lngCurrRec)=adorst("field")
    7.   adoRst.MoveNext
    8. Next

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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