使用表单设置Access数据库表字段复选框 [英] set Access database table field with form check box

查看:971
本文介绍了使用表单设置Access数据库表字段复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用表格上绑定复选框的值来设置表格上数字字段的值.如果选中,则该值为40;如果未选中,则为0.我是新来的,不知道从哪里开始.

I am trying to set the value of a number field on a table using the value of a bound checkbox on a form. The value needs to be 40 if checked and 0 if unchecked. I'm new to this and have no idea where to start.

否,该复选框已添加到表单,并已绑定到表中的数字字段.数据是从过时的系统导入的,该系统使用的是大多数用户不理解的代码,因此,如果表中显示40,我只希望表单显示为选中状态,所有其他值都应显示为未选中,并且我希望用户选中/取消选中复选框以相应地更新表格.

No, the checkbox was added to a form and is bound to a number field in the table. The data is imported from an antiquated system that uses codes that most users would not understand, so if it says 40 in the table I just want the form to show checked, all other values should show unchecked, and I want the user checking/unchecking the checkbox to update the table accordingly.

该复选框用于指示客户是否提供了直接付款信息. 40表示他们拥有,而40以外的任何东西则意味着他们没有.

The checkbox is to indicate whether or not a customer has provided direct debit information. 40 would mean they have, anything other than 40 would mean they haven't.

推荐答案

一种方法是在Access中使用VBA.在Access中访问VBA控制台的方法是,在窗体的设计视图中,右键单击复选框控件,然后从菜单中选择构建事件".确保在窗体设计视图中复选框的属性中,将控件的三态"设置为是".另外,请确保复选框的控制源"指向要放置数据的表中的字段.

One way do this is using VBA in Access. The way you access VBA console in Access, is when in your form's design view, right-click the checkbox box control and choose "Build Event" from the menu. Make sure your control's Triple State is set to "Yes" in the property for the checkbox in the form's design view. Also, be certain that the checkbox's Control Source is pointed to the field in the table where you're putting the data.

请注意,在执行此操作之前,为控件名称加上前缀是很有帮助的,例如chkDebitInfo ..."chk"代表复选框.您可以通过单击其他"选项卡来更改控件的属性表中的名称,并只需更改默认名称,该名称可能类似于Check625.另外,您希望将表字段的数据类型设置为整数",以便它保存该值.

Please note that prior to doing this, it is helpful to prefix your control's name... something like chkDebitInfo... "chk" stands for check box. You can change the name in the property sheet for the control, by clicking on the Other tab, and simply change the default name, which might say something like Check625. Also, you want to set the table field's data type to an "Integer" so it holds the value.

在VBA控制台中,选择Build Event之后,将为您提供控件的子过程.我为您制作了一个示例:

Once you're in the VBA console, after having chosen Build Event... you're presented with the control's sub procedure. I've done a sample for you:

Private Sub chkDebitInfo_Click()

If chkDebitInfo.Value = True Then
   chkDebitInfo.Value = 40         // Sets the checkbox to 40 if checked

ElseIf chkDebitInfo.Value = False Then
    chkDebitInfo.Value = 0        // Sets the checkbox to 0 if un-checked

End If

End Sub

我已经注释了设置复选框值(如果已选中或未选中)的语句,因此您不必添加这些值.只需将控件名称替换为您的控件名称,或者如果您没有更改默认名称,则使用默认名称,这很好.然后,如果选中或未选中此复选框,您将看到数据库表中填充的值.

I've commented the statements that set the checkbox values if checked or un-checked, so you don't have to add those. Just substitute the control name for your's, or use the default name if you haven't changed it, and you're good. Then when the check box is checked or not checked, you'll see the values populated in the database table.

或者,您可以在加载表单时将复选框设置为默认值,但是您尚未声明它,因此我不再赘述.希望这可以帮助.让我知道是否需要进一步说明.

Alternatively, you can set the checkbox to default value for when the form loads, but you haven't stated it, so I won't go into it. Hope this helps. Let me know if I need to expound further.

这篇关于使用表单设置Access数据库表字段复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆