多选列表框 [英] Multiselect Listbox
问题描述
我有一张名为
tbl_Questions
的表名为Region
我总共有8个地区
我试图在名为frm_DH的表单上设置一个Unbound多选列表框
我需要用户能够选择和取消选择8个区域的任意组合和在splitform数据表中显示的记录并更新问题表。
到目前为止,我可以选择数据,但它不会填充qry_DH和tbl_Questions,并且在表单关闭后无法保存。
这是我的代码:
[代码/]
私有子cmd_lst_Region_Click()
错误GoTo PROC_ERR
''声明项目
Dim iItem As Integer
Dim Region As String
Dim db作为DAO.Database
Dim rs作为DAO.Recordset
''保存当前记录,如果它不是保存
如果Me.Dirty = True那么
Me.Dirty = False
结束如果
''根据表格打开一个记录集
设置db = CurrentDb
设置rs = db.OpenRecordset(" tbl_Questions",dbOpenDynaset)
With Me!lst_Region
''遍历列表框中的所有行
For iItem = 0 To .ListCount - 1
Region = .Column(0,iItem)
''确定表中的这个组合目前是否为
'' />
rs.FindFirst" [ID] =" &安培;表格!frm_DH!lst_Region& " ; " _
& [Region] =" &安培; lst_Region.AddItem
如果rs.NoMatch那么
''此项目尚未添加
如果。选中(iItem)然后
''添加它
rs.AddNew
rs!ID = Me.txt_ID
rs!lst_Region = lst_Region
rs.Update
结束如果
''如果没有被选中,请忽略它
否则
如果不是。选择(iItem)然后
''删除此记录,如果它已被取消选中
rs.Delete
结束如果
''如果被选中,请不要管它
结束如果
下一个iItem
结束
rs.Close
设置rs = Nothing
设置db = Nothing
PROC_EXIT:
退出子
PROC_ERR:
MsgBox"错误" &安培; Err.Number& "在cmdProcess_Click中: _
& vbCrLf& Err.Description
简历PROC_EXIT
结束子
[End_Code /]
我相信我已经把它搞砸了,另外我确信我正试图做不可能的事情。
感谢您的帮助,我希望我提供了足够的信息。我目前正在使用access 2007。
I have a table named
tbl_Questions
with a column called Region
I have a total of 8 regions
I am attempting to set an Unbound multiselect listbox on a form called frm_DH
I need the user to be able to select and deselect any combination of 8 regions and the record to show in the splitform datasheet and update the questions table.
So far I can select the data but it does not populate the qry_DH nor the tbl_Questions and fails to save once the form is closed.
here is my code:
[Code/]
Private Sub cmd_lst_Region_Click()
On Error GoTo PROC_ERR
''Declare items
Dim iItem As Integer
Dim Region As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
'' Save the current record if it''s not saved
If Me.Dirty = True Then
Me.Dirty = False
End If
'' Open a Recordset based on the table
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl_Questions", dbOpenDynaset)
With Me!lst_Region
'' Loop through all rows in the Listbox
For iItem = 0 To .ListCount - 1
Region = .Column(0, iItem)
'' Determine whether this combination is currently
'' in the table
rs.FindFirst "[ID] = " & Forms!frm_DH!lst_Region & " ; " _
& "[Region] = " & lst_Region.AddItem
If rs.NoMatch Then
'' this item has not been added
If .Selected(iItem) Then
'' add it
rs.AddNew
rs!ID = Me.txt_ID
rs!lst_Region = lst_Region
rs.Update
End If
'' if it wasn''t selected, ignore it
Else
If Not .Selected(iItem) Then
'' delete this record if it''s been deselected
rs.Delete
End If
'' if it was selected, leave it alone
End If
Next iItem
End With
rs.Close
Set rs = Nothing
Set db = Nothing
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in cmdProcess_Click:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
[End_Code/]
I am sure I have messed this up royally in addition I am sure I am attempting to do the impossible.
thank you for any assistance and I hope I have offered enough information. I am using access 2007 at this time.
推荐答案
除非我误读了这个线程,否则你将创建一个半结肠定界,选定区域的字符串的基本逻辑( s)离开了。以下代码将从所选区域生成所需的字符串:
Unless I am misreading this Thread, you basic Logic for creating a String of Semi-Colon Delimited, Selected, Region(s) is way off. The following Code will generate the desired String from the Region(s) chosen:
ADezii
我相信你正确地阅读它。感谢您的帮助,我会在上午回复。我会发布结果。
谢谢,
ADezii
I am sure you are reading it correctly. I appreciate your assistance and will get back to it in the AM. I will post the outcome.
Thank you,
ADezii,
我感谢您的回复和代码剪辑,但是这个不适合我。
我需要表格来更新表格,一旦我离开表格,信息就不会被更新或保存。作为新手,我不确定如何解释它。
我会再试一次。
表名:tbl_questions
查询名称:qry_DH(选择查询)
拆分表格名称:frm_DH由qry_DH提供
MultiSelect列表框名称:lst_Regions(有8个区域)
我需要列表框使用frm_DH填充tbl_Questions上的列区域。如果它可以动态地做这将是一个加号,但我会在我离开表单或按下保存按钮之后更新它。现在我的代码和上面的代码都正常工作。我可以选择他们不保存的项目或填充表格。
有什么想法吗?
ADezii,
I appreciate your response and code snip, but this did not work for me.
I need the form to update the table and the information is not being updated or saved once I leave the form. As a novice to this I am unsure how else to explain it.
I will try again.
Table Name: tbl_questions
Query Name: qry_DH (Select Query)
Split-Form Name: frm_DH being fed by qry_DH
MultiSelect Listbox Name: lst_Regions (there are 8 Regions)
I need the listbox to populate the column region on the tbl_Questions using the frm_DH. If it could do it dynamically that would be a plus, but I would settle for it updating after I leave the form or after I press the save button or something. Right now my code nor the above snip is working. I can choose the items they just do not save or populate the table.
Any ideas?
这篇关于多选列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!