在列表框

时间:2016-05-30 18:57:16

标签: access-vba access

我有一个包含6个项目的列表框,我已将列表框编码为下拉列表。我的问题是,如何在选中后将所选项目置于列表框中。这是我在lostfocus事件中的代码:

'----------------------------------------------------------------
'   Save selection for update query
'----------------------------------------------------------------

 Dim frm As Form, ctl As Control
 Dim varItm As Variant
 Set frm = Forms!loginForm
 Set ctl = frm!areaLB
 For Each varItm In ctl.ItemsSelected
 areaGbl = ctl.ItemData(varItm)
 Next varItm
'----------------------------------------------------------------
'   CHeck selection to load station dropdown
'----------------------------------------------------------------
If areaGbl = "Batting Cages" Then
 Me.stationLB.RowSource = "cageTbl"
 Else
 Me.stationLB.RowSource = "RangeBays"
End If
Me.areaDsp = areaHld
Me.areaLB.Height = 500

1 个答案:

答案 0 :(得分:0)

areaLB列表框控件的rowsourcetype是什么?

如果是值列表,只需删除所有项目并从areaGbl变量中添加一个记录/值。

For nIndex = 0 To areaLB.ListCount - 1
  areaLB.RemoveItem nIndex
Next nIndex

areaLB.AddItem areaGBL

如果rowsourcetype是table / query,则更改rowsource的查询,如下所示

areaLB.rowsource = "Select <fieldName> from <table name> where     
 <fieldname> in '" & areaGBL & "'"