.AddItem method not working

G

Guest

I need a method of populating a List Box using code. I am trying to create a
form where you can see who is logged into the application. I found some code
in Oreilly ACC Cookbook but it uses a method or member not found (.AddItem).
The code below is attached to a forms on_Open() property and contains a list
box named lboConnections with two columns named Compter Name, and User Name.

my referances are(VB for App, MS 9.0 OL, OLE Auto, and MS ActiveX Data Obj
2.1 Lib)

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strComputerName As String

Set cnn = CurrentProject.Connection
Set rst = cnn.OpenSchema(adSchemaProviderSpecific, , _
"{947bb102-5d43-11d1-bdbf-00c04fb92675}")
lboConnections.RowSource = vbNullString
lboConnections.AddItem "Computer Name;Login Name"

Do While Not rst.EOF
If rst("Connected") Then
strComputerName = rst("Computer_Name")
lboConnections.AddItem _
Left(strComputerName, _
InStr(strComputerName, vbNullChar) - 1) & _
";" & rst("Login_Name")
End If
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set cnn = Nothing

I can sucessfully retreive the users via code, but I can't figure out how to
feed that to a form.
 
M

MikeB

AddItem method is AC2003 and later. Here is a replacement for AddItem:

'This is what calls the sub.. Change to suit your controls

ListAddItem TheNameOfYourListBox, SomeTextControl.Value

'or

ListAddItem TheNameOfYourListBox, "TheLiteralTextToAdd"

'or

ListAddItem TheNameOfYourListBox, VariableNameContainingTheTextToAdd

'This is the sub that adds Items to the ListBox
Private Sub ListAddItem(LB As ListBox, strItemToAdd As String)
If LB.RowSource = "" Then
LB.RowSource = IIf(LB.RowSource = "", strItemToAdd, "")
Else
LB.RowSource = LB.RowSource & ";" & strItemToAdd
End If
ListBox0.AddItem strItemToAdd
End Sub
 
D

Dirk Goldgar

MikeB said:
AddItem method is AC2003 and later. Here is a replacement for
AddItem:

Access 2002 and later, actually, but it looks like aWs is using Access
2000, so the point remains the same.
 
G

Guest

Thanks for the sub Mike. But I'm lost on how to apply it to my form.

Lines 4-6 retreive the records I believe and 9-18 fill the list box(if I was
using AC2003)

Do I place the sub ListAddItem in my forms module?
*When I tried this it fussed about ListBox0.AddItem srtItemToAdd

How do lines 7,8,12 get modified?
 
M

MikeB

You can see by following the logic of the sub that the RowSource property is
populated by a string delimited by semi colon. You can simply move through
your record set and concantenate a string delimited by a semi colon and then
set the RowSource property of your listbox to the value of the concantenated
string...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top