vba 访问组合框存储 1 个值,显示另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6256562/
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
Access combobox store 1 value, display another
提问by MAW74656
I'm have a query that returns the following columns:
我有一个返回以下列的查询:
Code
LastName
FirstName
代码
姓氏
名字
I have a combobox where all of this info is displayed in the dropdown. But when I select a row, all I see in the combobox is the Code (its an employee number). What I'd like to do is display:
我有一个组合框,所有这些信息都显示在下拉列表中。但是当我选择一行时,我在组合框中看到的只是代码(它的员工编号)。我想做的是显示:
"[Code] - [LastName], [FirstName]"
“[代码] - [姓氏],[名字]”
as the selected item when a value is selected, and still store just the [Code] in the combobox's .Value property.
当一个值被选中时作为被选中的项目,并且仍然只在组合框的 .Value 属性中存储 [Code]。
How is this done? I'm used to C#.NET where a dropdown has 2 properties (displayValue and selectedValue).
这是怎么做的?我习惯于 C#.NET,其中下拉列表有 2 个属性(displayValue 和 selectedValue)。
回答by RolandTumble
1.In this method you won't be able to have the formatting (dash or comma):
1.在这种方法中,您将无法使用格式(破折号或逗号):
Set the column count to 3.
Set the Bound column to 1 (it's one-based, even though the .Column property is zero-based).
Adjust column widths to a pleasing arrangement.
Set RowSourceType to "Table/Query".
Set RowSource to your query.
Do notset a Control Source (leave blank--this leaves the .Value unbound from underlying data).
将列数
设置为 3。将绑定列设置为 1(它从 1 开始,即使 .Column 属性从零开始)。
将列宽调整为令人满意的排列。
将 RowSourceType 设置为“表/查询”。
将 RowSource 设置为您的查询。
不要不设置控制源(留空-这使得.value的从底层数据解开)。
You can do all the above in Design View.
您可以在设计视图中执行上述所有操作。
2.This method is more work, but gets exactly what you asked for:
2.这种方法工作量更大,但完全符合您的要求:
In Design View:
Set column count to 2.
Set Bound Column to 1,
SetColumn Widths to 0";2"
(accepts inches or cm, and if you just enter undecorated numbers will read them as inches (or as set in Options(?))).
Set RowSourceType to "Value List".
Do notset a Control Source (leave blank--this leaves the .Value unbound from underlying data).
在设计视图中:
将列数设置为 2。
将绑定列设置为 1,将列
宽度设置为0";2"
(接受英寸或厘米,如果您只是输入未修饰的数字,则会将它们读取为英寸(或在 Options(?)) 中设置)。
将 RowSourceType 设置为“值列表”。
不要不设置控制源(留空-这使得.value的从底层数据解开)。
Write this code:
写下这段代码:
Private Sub Form_Load()
'declare variables & open query as recordset--left as exercise
With Combo1
.Clear
Do Until rs.EOF
.AddItem rs.Code & ";" & rs!Code & " - " & rs!LastName & ", " & rs!FirstName
rs.MoveNext
Loop
End With
'close rs & clean up--another exercise
End Sub
The semicolon between the rs!Code
instances in the string concatenation is what points them into the appropriate columns.
rs!Code
字符串连接中实例之间的分号将它们指向适当的列。
回答by Lance Roberts
In the BeforeUpdate event (there might be a better one, that's just what I tested on), set the .Text property to the string you want.
在 BeforeUpdate 事件中(可能有更好的事件,这正是我测试过的),将 .Text 属性设置为您想要的字符串。
Me.ComboBox1.Text = [Code] & " - " & [LastName] & ", " & [FirstName]
Note that you may have to play with the string construction.
请注意,您可能必须使用字符串构造。
回答by Harag
The following might help to add information to the combo box with 3 columns. Note that the column widths at the bottom is used to "hide" the first column
以下内容可能有助于向具有 3 列的组合框添加信息。请注意,底部的列宽用于“隐藏”第一列
For x = 1 To 10
ComboBox1.ColumnCount = 3
With ComboBox1
.AddItem "Code" ' Column 1 data
.List(.ListCount - 1, 1) = "LastName" ' Column 2 data
.List(.ListCount - 1, 2) = "FirstName" ' Column 3 data
'etc.
End With
ComboBox1.ColumnWidths = "0cm;2.5cm;2.0cm"
Next
Hope this helps
希望这可以帮助
EDIT:
编辑:
DisplayString = code & " - " & Lastname & ", " & Firstname
ComboBox1.ColumnCount = 2
With ComboBox1
.AddItem "Code" ' Column 1 data
.List(.ListCount - 1, 1) = DisplayString ' Column 2 data
End With
ComboBox1.ColumnWidths = "0cm;4.5cm;"
回答by MAW74656
I have it working the way I want now. Here's what I did:
我让它按照我现在想要的方式工作。这是我所做的:
-Did the concat in the sql, so the query returns columns [Code] and [DisplayName]. -Bound = 1, Columns = 2, Column Widths = 0:1"
-在sql中做了concat,所以查询返回列[Code]和[DisplayName]。- 边界 = 1,列数 = 2,列宽 = 0:1"
Now the display value is what I specified in my SQL and the selected value (tested and confirmed) is just the code.
现在显示值是我在 SQL 中指定的值,所选值(经过测试和确认)只是代码。
Thanks for the help, I'm not well versed in Access vba.
感谢您的帮助,我不太精通 Access vba。