views:

24

answers:

1

I'm trying to populate a ListBox on MS Access Front end by querying Sybase.

I think setting the RowSource will not work here.

Will be very helpful if someone can provide me with sample code.

Many thx.

A: 

You have 3 main options

1) Link the table and then set the row source

2) Open the table using VBA, loop through and add the records to the list of values separated by a ;

3) Change it from a normal listbox to an ActiveX one that has a collection to control the items in the listbox

Option 1 is the easiest and would be the one to go for in 99% of situations. Option 2 is a good one but remember that the list is then limited to I thing 1024 characters. Option 3 I use a fair amount and can be very good. Below is some code I made that takes a SQL string and a list box and does the rest.

This is how to call it

Assign_SQl_to_listview “SELECT * FROM tblFoo”, me.lstMyListBox

And now the fucntion

Public Function Assign_SQl_to_listview(strList_SQL As String, lstTarget As ListView) As Long
'open up the SQL as a recordset and lop through adding the rows as items
If Len(strList_SQL) <= 2 Then Exit Function
Dim bFields As Byte
Dim i As Byte
Dim rst As DAO.Recordset
On Error GoTo Error_trap
Dim lstItem As ListItem
DoCmd.Hourglass True
lstTarget.ListItems.Clear
Set rst = DBEngine(0)(0).OpenRecordset(strList_SQL, dbOpenForwardOnly)

With rst
    'see how many fields this SQL statement has, then only go through that many sub items-1
    bFields = .Fields.Count
    Do Until .EOF

        ' Add items and subitems to list control.
            Set lstItem = lstTarget.ListItems.Add()
            lstItem.Text = .Fields(0)
            'loop through the fields adding them
            For i = 1 To bFields - 1
                lstItem.SubItems(i) = Nz(.Fields(i), "")
            Next i

        .MoveNext
    Loop
Assign_SQl_to_listview = lstTarget.ListItems.Count

End With

rst.Close
Set rst = Nothing

DoCmd.Hourglass False


Exit Function

Error_trap:

DoCmd.Hourglass False
Set rst = Nothing
Debug.Print strList_SQL
MsgBox "An error happened in sub Assign_SQl_to_listview, error description, " & Err.Description, vbCritical, "Tracker 3"
Debug.Print strList_SQL
End Function
Kevin Ross
Given that native Access listboxes since 2002 (2003, at the very latest) have .AddItem and .RemoveItem methods, why would you need an ActiveX listbox just to do that?
David-W-Fenton
We are still stuck in access 97 old bean, I reckon I must be the only person (or one of the few) that has linked access 97 to SQL server 2008R2! Its crazy to think they are separated by 13 years but work together just fine!
Kevin Ross
I still love A97!
David-W-Fenton
Oh don’t get me wrong I really like using access97, I think the IDE is better than later versions its just I wish some of the new functions could be included without having to brew your own.
Kevin Ross
I've got tons of legacy databases that still use my old custom functions for things that VBA now provides. If it ain't broke, don't fix it!
David-W-Fenton