AddItem in ListBox (Access) using VBA

  • Thread starter Thread starter March
  • Start date Start date
M

March

Hello,

I have a question about how to addItem in List box (Access) using VBA.

I have one combo box and one list box. The values in the list box will
change when the value in combo box changes.

I try the code below:

Dim rst As DAO.Recordset
Dim db As DAO.Database

If Me.ComboBox = "A" Then
sqlCmd = "SELECT A FROM TableName;"
Set rst = db.OpenRecordset(sqlCmd)
Do Until rst.EOF
If Not IsNull(rst!A) Then
With Me.ListBox
.RowSource = vbNullString
.AddItem rst!A
End With
End If
rst.MoveNext
Loop


TableName contains field "A" that I need all records in "A" show in the
List Box. I also need to use list box array.
 
Get rid of the line

..RowSource = vbNullString

That erases what's already in the list!

However, I don't see the point of looping through a recordset. Simply use

If Me.ComboBox = "A" Then
Me.ListBox.RowSource = _
"SELECT A FROM TableName " & _
"WHERE A IS NOT NULL" & _
"ORDER BY A"
End If
 
What do you mean by "ListBox Array"?

Are you saying that you want to be able to programmatically refer to the
value in the third column of the second row of the list box? That would be

Me!MyListBox.Column(2, 1)

(numbering starts at 0)
 
In my project, I use 2 list boxes. Let say Left(L) and Right (R). Both have
only one column. L has a list of item, R has nothing when loading form.

I have one bottom that when I click the bottom the selected multiple-item
from L, will copy to R.


Thank you in advanced;

March
 
Back
Top