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

本文介绍如何在Access数据库中通过创建Form并配置ListBox、TextBox和CommandButton控件,实现获取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控件里面显示出各个记录。


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值