vba 如何使用列表框值更新文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20623265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:58:29  来源:igfitidea点击:

how to use the listbox value to update textbox

vbams-accessms-access-2007access-vba

提问by user1921704

I have an Access form with a list-box consisting of two columns and its MultiSelect property is set to None. I need to update 2 text-box using this list-box where if the user select an item from it the value of its first column is used to update one text-box and the value of the second column is used to update another text-box , something like :

我有一个 Access 表单,其中包含一个由两列组成的列表框,其 MultiSelect 属性设置为 None。我需要使用此列表框更新 2 个文本框,如果用户从中选择一个项目,则其第一列的值用于更新一个文本框,第二列的值用于更新另一个文本-框,类似于:

Private Sub listbox_AfterUpdate()

Dim colval1 As String
Dim colval2 As String

colval1 = Me.listbox.column(1).Value
colval2 = Me.listbox.column(2).Value

Me.[textbox1] = colval1 
Me.[textbox2] = colval2 


End Sub

I just don't know how to get the value of this list-box.

我只是不知道如何获取此列表框的值。

回答by HansUp

List box column numbering starts from zero, so the first column value is accessible as Me.listbox.Column(0). Access throws an error ("object required") when you append .Valueafter the column.

列表框列编号从零开始,因此第一列值可作为Me.listbox.Column(0). 在列之后追加时,Access 会引发错误(“需要对象”.Value

Also you shouldn't need variables to store the column values before you assign them to the text boxes. You can assign the column values to the text boxes directly.

此外,在将列值分配给文本框之前,您不应该需要变量来存储列值。您可以直接将列值分配给文本框。

Private Sub listbox_AfterUpdate()
    Me.[textbox1] = Me.listbox.Column(0)
    Me.[textbox2] = Me.listbox.Column(1)
End Sub

回答by David

Crazy VBA:

疯狂的VBA:

Me.[textbox3] =Me.listbox

will give you your value.

会给你你的价值。