Microsoft Access中如何获得ListBox选中的记录

在一个小Access的项目中遇到需要获取ListBox选中记录的信息的问题,现将解决过程分享出来。

做一个例子来实现怎么在Access中获取ListBox选中的记录,多选的话各个记录中间用“,”分割开。

1、打开一个access数据库,模板数据库也可以。

2、创建一个Form。

3、在Form中添加下列控件,并且按下列说明设置控件的属性。

        List Box
	----------------------------------------------------
	Name             :  ListName
	Row Source Type  :  Table/Query
	Row Source       :  SELECT FieldName FROM TableName(SQL语句,可以按向导设置)
	Multi Select     :  Extended
	Width            :  3.5"
	Height           :  0.75"


	Text Box
	-----------------------
	Name    :  SelectedItem
	Width   :  3.5"
	Height  :  0.25"

	Command Button
	----------------------------------
	Name     :  testselected
	Caption  :  Display Selected Items
	Width    :  1.375"
	Height   :  0.3"
 
	Command Button
	----------------------
	Name     :  ClrList
	Caption  :  Clear List
	Width    :  1.375"
	Height   :  0.3"
4、随便选中一个控件,在属性里面选择事件,点击进去Code界面,把下面的代码粘贴到Visual Basic for Application Editer 里面。

Option Compare Database
Option Explicit

Private Sub Form_Current()
    Dim oItem As Variant
    Dim bFound As Boolean
    Dim sTemp As String
    Dim sValue As String
    Dim sChar As String
    Dim iCount As Integer
    Dim iListItemsCount As Integer
    
    sTemp = Nz(Me!SelectedItem.Value, " ")
    iListItemsCount = 0
    bFound = False
    iCount = 0

    Call clearListBox
        
    For iCount = 1 To Len(sTemp) + 1
    sChar = Mid(sTemp, iCount, 1)
        If StrComp(sChar, ",") = 0 Or iCount = Len(sTemp) + 1 Then
            bFound = False
            Do
                If StrComp(Trim(Me!ListName.ItemData(iListItemsCount)), Trim(sValue)) = 0 Then
                    Me!ListName.Selected(iListItemsCount) = True
                    bFound = True
                End If
                iListItemsCount = iListItemsCount + 1
            Loop Until bFound = True Or iListItemsCount = Me!ListName.ListCount
            sValue = ""
        Else
            sValue = sValue & sChar
        End If
    Next iCount
End Sub
    
Private Sub clearListBox()
    Dim iCount As Integer
        
    For iCount = 0 To Me!ListName.ListCount
        Me!ListName.Selected(iCount) = False
    Next iCount
End Sub

Private Sub testselected_Click()
    Dim oItem As Variant
    Dim sTemp As String
    Dim iCount As Integer
    
    iCount = 0
            
    If Me!ListName.ItemsSelected.Count <> 0 Then
        For Each oItem In Me!ListName.ItemsSelected
            If iCount = 0 Then
                sTemp = sTemp & Me!ListName.ItemData(oItem)
                iCount = iCount + 1
            Else
                sTemp = sTemp & "," & Me!ListName.ItemData(oItem)
                iCount = iCount + 1
            End If
        Next oItem
    Else
        MsgBox "Nothing was selected from the list", vbInformation
        Exit Sub  'Nothing was selected
    End If
    
    Me!SelectedItem.Value = sTemp
End Sub
    
Private Sub clrList_Click()
    Call clearListBox
    Me!SelectedItem.Value = Null
End Sub

5、关闭Visual Basic Editer 并且保存Form。

6、以Form View的形式打开这个Form,在ListBox控件里面选择记录(按Ctrl键可以多选)。

7、点击Display Selected Items按钮就可以在Text控件里面显示出各个记录。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值