Results 1 to 10 of 10

Thread: Module level combobox fill routine

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington, DC
    Posts
    422

    Module level combobox fill routine

    Hi all. I have a quick question. I think this should be simple, but I can't get it to work. Throughout my app, I have a number of combo boxes that display all of the US states. The states are loaded from a database, and I'm trying to write a module level procedure that I could call to fill the state combo boxes when I need to, but I cant' get it to work. Any suggestions?

    Thanks

    FLL

  2. #2
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    VB Code:
    1. Public Sub PopulateStates(lst As Control)
    2. Dim i As Integer
    3. Dim RST As Recordset
    4. Dim sSQL As String
    5.  
    6. ' declare the lst as a control so you can pass a combo or a list
    7.  
    8. On Error Goto errHandler
    9.  
    10. sSQL = "SELECT State, StateID FROM States ORDER BY State"
    11. Set RST = db.OpenRecordset(sSQL, dbOpenSnapshot)
    12.  
    13. lst.Clear
    14.  
    15. With RST
    16.     Do Until .EOF
    17.         lst.AddItem .Fields("State")
    18.         lst.ItemDate(lst.NewIndex) = .Fields("StateID")
    19.         .MoveNext
    20.      Loop
    21. End With
    22.  
    23. RST.Close
    24. Set RST = Nothing
    25.  
    26. Exit Sub
    27.  
    28. errHandler:
    29. msgbox Error
    30.  
    31. End Sub

  3. #3
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I don't get why the forum is smashing all my code together, but here's a more generic version that I like to use:

    VB Code:
    1. Public Sub LoadTableIntoList(lst As Control, sTable As String, sField As String, sID As String)
    2. Dim i As Long
    3. Dim RST As Recordset
    4. Dim sSQL As String
    5.  
    6.  
    7. On Error GoTo errHandler
    8.  
    9.  
    10. ' Refreshes list only if changes are found
    11.  
    12.  
    13. sSQL = "SELECT " & sField & "," & sID & " FROM " & sTable & " ORDER BY " & sField & " ASC"
    14. Set RST = DB.OpenRecordset(sSQL, dbOpenSnapshot)
    15.  
    16.  
    17. If RecordsetEmpty(RST) Then
    18.   lst.Clear
    19.   GoTo CloseRST
    20. End If
    21.  
    22.  
    23. With RST
    24.   If .Recordcount <> lst.ListCount Then GoTo RefreshList
    25.   .MoveFirst
    26.   For i = 0 To .Recordcount - 1
    27.     If lst.List(i) <> .Fields(sField) Then GoTo RefreshList
    28.     .MoveNext
    29.   Next i
    30.   ' No changes were found so leave list as it is
    31.   GoTo CloseRST
    32. End With
    33.  
    34.  
    35. RefreshList:
    36.  
    37.  
    38. lst.Clear
    39.  
    40.  
    41. With RST
    42.   .MoveFirst
    43.   Do Until .EOF
    44.     lst.AddItem .Fields(sField)
    45.     lst.ItemData(lst.NewIndex) = .Fields(sID)
    46.     .MoveNext
    47.   Loop
    48. End With
    49.  
    50.  
    51. CloseRST:
    52. RecordsetClose RST
    53.  
    54.  
    55. Exit Sub
    56.  
    57.  
    58. errHandler:
    59. LogError Error, Err, vbNullString, "bDatabase.LoadTableIntoList"
    60. Resume CloseRST
    61.  
    62.  
    63. End Sub
    64.  
    65.  
    66. Public Function RecordsetEmpty(ByRef RST As Recordset) As Boolean
    67.  
    68.  
    69. ' Checks to see if a recordset is populated
    70.  
    71.  
    72. On Error GoTo errHandler
    73.  
    74.  
    75. ' Assume the recordset is nothing or unpopulated
    76. RecordsetEmpty = True
    77.  
    78.  
    79. If RST Is Nothing Then Exit Function
    80. If RST.BOF And RST.EOF Then Exit Function
    81.  
    82.  
    83. ' Recordset exists and is populated
    84. RecordsetEmpty = False
    85.  
    86.  
    87. Exit Function
    88.  
    89.  
    90. errHandler:
    91. LogError Error, Err, vbNullString, "bDbUtilities.RecordsetEmpty"
    92.  
    93.  
    94. End Function

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington, DC
    Posts
    422
    Thanks Cafeenman. I've been trying to get that to work for a while. Another question that you guys may know.

    I'm in the process of trying to optimize this app before it goes out. Quicker to load the data into a module level array and eat the memory for the entire time the app is running or quicker to hit the db each time I need to fill the combo boxes?

    Thoughts appreciated. Again, thanks for the code.

    Flewis

  5. #5
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by fllewis
    Thanks Cafeenman. I've been trying to get that to work for a while. Another question that you guys may know.

    I'm in the process of trying to optimize this app before it goes out. Quicker to load the data into a module level array and eat the memory for the entire time the app is running or quicker to hit the db each time I need to fill the combo boxes?

    Thoughts appreciated. Again, thanks for the code.

    Flewis
    Well, I actually do load the stuff into an array for the static tables (states, etc). But for the more dynamic tables, I still have to check the database for updates in a multi-user environment, so loading arrays and then loading combos just adds a step. So I optimized based on what table goes into what combo.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington, DC
    Posts
    422
    In this case, it's always going to be the fifty states in alphabetical order. What do you think would optimize speed in this case? The static array? Or multiple trips to the db?

  7. #7
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Just fill it once from the db and leave it. You don't have to worry about the contents changing any time soon.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington, DC
    Posts
    422
    But I will be filling several combos with the same data through the instance of the app...not all at the same time, either. The combos reside on different forms, and depending on when the users accesses the forms will depend on when the combos are filled.

    So would it be better to fill each different combo when the forms are loaded, or store the data in an array and use the array data to fill the combos.

    Array memory vs. trip to the db each time a different States combo must be filled?

  9. #9
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    You're right. When I posted my last response I forgot that you are filling combos on several forms. Just create two arrays (if you need to track the ID's as well). One for the state and one for the id. Then use that to load the combos. That will be much faster than making trips to the server to retrieve the data.

    VB Code:
    1. ' Don't dimension the array if you don't know how many there are
    2. ' for example, are you loading territories as well?
    3.  
    4. Public State() As String
    5. Public StateID() As Long
    6.  
    7. Sub GetStates
    8. Dim RST As Recordset
    9. Dim sSQL As String
    10. Dim iPos as Long
    11.  
    12. sSQL = "SELECT State, StateID FROM States ORDER BY State"
    13. Set RST = db.OpenRecordSet(sSQL, dbOpenDynaset)
    14.  
    15. With RST
    16.     .MoveLast ' Needed to get recordcount
    17.     .MoveFirst
    18.     Redim State(RecordCount - 1)
    19.     Redim StateID(.RecordCount - 1)
    20.     Do Until .EOF
    21.         State(iPos) = .Fields("State")
    22.         StateID(iPos) = .Field("StateID")
    23.         iPos = iPos + 1
    24.         .MoveNext
    25.      Loop
    26. End With
    27.  
    28. RST.Close
    29. Set RST = Nothing
    30.  
    31. End Sub      
    32.  
    33. Sub PopulateStateList(lst As Control)
    34. Dim i As Long
    35.  
    36. lst.Clear
    37.  
    38. For i = LBound(State) To UBound(State)
    39.     lst.AddItem State(i)
    40.     lst.ItemData(lst.NewIndex) = StateID(i)
    41. Next I
    42.  
    43. End Sub

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington, DC
    Posts
    422
    Thanks for the input. I will go with the array here. Advice appreciated.

    FLL

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