vba 如何将单选按钮的值从 1、2、3 等更改为文本值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12481604/
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
How do I change the values of a radio button from 1, 2, 3, etc to text values?
提问by user1680893
I have created a small database in Access 2007 that consists of one table and two forms, one for entering data, and one for retrieving data.
我在 Access 2007 中创建了一个小型数据库,它由一个表和两个表单组成,一个用于输入数据,一个用于检索数据。
My problem is this: On my input form I have a group box with three radio buttons in it. The question being asked is Is the element a sensor?
The buttons represent Yes, No, and Don't Know.
我的问题是这样的:在我的输入表单上,我有一个包含三个单选按钮的组框。被问到的问题是元件是传感器吗?
按钮代表Yes、No和Don't Know。
In the database I have a column named Sensor to hold the value the user chose, but since the radio buttons return a value of 1for yes, 2for no, or 3for don't know, it makes generating a report or query that makes sense to the user very difficult.
在数据库中,我有一个名为 Sensor 的列来保存用户选择的值,但是由于单选按钮返回的值为1表示yes,2表示no,或3表示不知道,因此生成报告或查询对用户来说很困难。
At this point I'm writing huge SQL statements with nested iif's to return the data the way I want to see it.
在这一点上,我正在编写带有嵌套 iif 的大型 SQL 语句,以按照我想要的方式返回数据。
Is there a way to populate the table with data the way I want to see it (yes, no, don't know) instead of populating it with 1's 2's or 3's? This is a bound form by the way, I wish I would have done it unbound, but I can't go back now.
有没有办法以我想要查看的方式(是的,不,不知道)用数据填充表格,而不是用 1 的 2 或 3 填充它?顺便说一下,这是一种绑定形式,我希望我可以不绑定,但我现在不能回去。
回答by HansUp
"since the radio buttons return a value of 1 for yes, 2 for no, or 3 for don't know, it makes generating a report or query that makes sense to the user very difficult."
“由于单选按钮返回值 1 表示是,2 表示否,或 3 表示不知道,因此生成对用户有意义的报告或查询变得非常困难。”
Store those 3 pairs as rows in a Sensor_Values
table:
将这 3 对存储为Sensor_Values
表中的行:
sval descriptor
1 yes
2 no
3 don't know
Then you can join that table to the table which includes the stored Sensor
numbers.
然后,您可以将该表加入包含存储Sensor
数字的表。
SELECT yt.Sensor, sv.descriptor
FROM
YourTable AS yt
INNER JOIN Sensor_Values AS sv
ON yt.Sensor = sv.sval;
If you're opposed to creating and joining a lookup table, you could use a Switch()
expression in your queries to translate the numeric Sensor
values to their text forms.
如果您反对创建和加入查找表,则可以Switch()
在查询中使用表达式将数值转换Sensor
为它们的文本形式。
SELECT
Switch(
Sensor = 1, "yes",
Sensor = 2, "no",
Sensor = 3, "don't know"
) AS sensor_text
FROM YourTable;
The Switch()
approach can work, but can be more challenging to maintain compared to the lookup table approach.
该Switch()
方法可以工作,但与查找表方法相比,维护起来更具挑战性。
My intention here was to show you fairly simple methods to use the option group value as a number instead of "populate the table with data the way I want to see it (yes, no, don't know) instead of populating it with 1's 2's or 3's"
我的目的是向您展示相当简单的方法来使用选项组值作为数字而不是“以我想要查看的方式用数据填充表格(是的,不,不知道)而不是用 1 填充它2 或 3 的“
As a general rule, you will be better off working with Access controls as they were designed to be used. Break that rule whenever you have a compelling reason ... but breaking the rule then requires additional efforts from you ... like more VBA code. The approaches I suggested don't require any VBA.
作为一般规则,您最好使用 Access 控件,因为它们是为使用而设计的。每当您有令人信服的理由时就打破该规则……但是打破规则则需要您付出额外的努力……例如更多的 VBA 代码。我建议的方法不需要任何 VBA。
回答by dmarra
I would suggest not using the radio buttons, and instead opt for a combo box. You'll be able to use string values for the results directly in the combo box.
我建议不要使用单选按钮,而是选择组合框。您将能够直接在组合框中为结果使用字符串值。
Now if you are dead set on using radio buttons, try this:
现在,如果您对使用单选按钮一无所知,请尝试以下操作:
Add a new field to your table that holds text. Bind this to a hidden text box on your form. Then, add a BeforeUpdate event (or AfterUpdate depending on what you are doing) to the radio group. Add code similar to the following:
向包含文本的表格添加一个新字段。将此绑定到表单上的隐藏文本框。然后,向无线电组添加一个 BeforeUpdate 事件(或 AfterUpdate,具体取决于您在做什么)。添加类似于以下的代码:
Sub RadioGroup_BeforeUpdate(cancel As Integer)
Select Case Me.RadioGroup.Value
case 1
Me.hiddenTextField.value = "Yes"
case 2
Me.hiddenTextField.value = "No"
case else
Me.hiddenTextField.value = "Don't Know"
End Select
End Sub
Now when you save the record, the human readable value will be available in the new field you added.
现在,当您保存记录时,人类可读的值将在您添加的新字段中可用。
回答by SeanC
you could use a Select Case
when creating the select string, instead of having iif
's nested in the SQL.
您可以Select Case
在创建选择字符串时使用 a ,而不是将iif
's 嵌套在 SQL 中。
Select Case Me.rdoSensor
Case 1
sSQL=sSQL & " AND Sensor='Yes'"
Case 2
sSQL=sSQL & " AND Sensor='No'"
Case 3
sSQL=sSQL & " AND Sensor='Don''t know'"
End Select