确定是否选中了“访问"复选框 [英] Determine whether a Access checkbox is checked or not

查看:58
本文介绍了确定是否选中了“访问"复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样一个简单的问题,但我找不到答案(Google,MS帮助,SO)

Such a simple question, but I cannot find the answer (Google, MS help, SO):

如何通过VBA检查Access表单上的未绑定复选框是否已被用户选中?找不到合适的属性.

How can I check by VBA whether an unbound checkbox on an Access form is checked by the user or not? Can't find the right property.

提前谢谢!

更新:

我在@HansUp和@RC的建议之后使用了这段代码:

I used this code after the suggestions of @HansUp and @RC:

Private Sub CmdTest_Click()
    MsgBox "Check1.Value = " & Me.Check1.Value
    MsgBox "Check2.Value = " & Me.Check2.Value
End Sub

Private Sub Form_Load()
    Me.Check1.Value = 25
    Me.Check2.Value = 50
End Sub

第二次更新:

代码应该是这样(由于@ David-W-Fenton):

The code should be this (thanks to @David-W-Fenton):

Private Sub CmdTest_Click()
    If Me.Check1 = True Then
        MsgBox "Check1 IS CHECKED"
    Else
        MsgBox "Check1 IS NOT CHECKED"
    End If
    If Me.Check2 = True Then
        MsgBox "Check2 IS CHECKED"
    Else
        MsgBox "Check2 IS NOT CHECKED"
    End If    
End Sub

Private Sub Form_Load()
    ' set first checkbox default checked
    Me.Check1.Value = True 
    ' set second checkbox default unchecked
    Me.Check2.Value = False
End Sub

推荐答案

复选框是一种控件类型,设计用于一个目的:确保布尔值的有效输入.

Checkboxes are a control type designed for one purpose: to ensure valid entry of Boolean values.

在Access中,有两种类型:

In Access, there are two types:

  1. 2状态-可以选中或不选中,但不能为Null.值为True(选中)或False(未选中).在Access和VBA中,True的值为-1,False的值为0.对于使用1表示True的环境的可移植性,您可以始终测试False或Not False,因为对于所有环境,False的值为0知道的.

  1. 2-state -- can be checked or unchecked, but not Null. Values are True (checked) or False (unchecked). In Access and VBA, the value of True is -1 and the value of False is 0. For portability with environments that use 1 for True, you can always test for False or Not False, since False is the value 0 for all environments I know of.

3状态-类似于2状态,但可以为Null.单击它可在True/False/Null之间循环.这是用于绑定到允许Null的整数字段.对于布尔字段,它是没有用的,因为它永远不能为Null.

3-state -- like the 2-state, but can be Null. Clicking it cycles through True/False/Null. This is for binding to an integer field that allows Nulls. It is of no use with a Boolean field, since it can never be Null.

小问题和答案:

几乎不需要使用Access控件的.Value属性,因为它是默认属性.这两个是等效的:

There is almost never a need to use the .Value property of an Access control, as it's the default property. These two are equivalent:

  ?Me!MyCheckBox.Value
  ?Me!MyCheckBox

这里唯一要注意的是,在测试复选框的值时,请务必不要创建隐式引用,这一点很重要.代替这个:

The only gotcha here is that it's important to be careful that you don't create implicit references when testing the value of a checkbox. Instead of this:

  If Me!MyCheckBox Then

...编写以下选项之一:

...write one of these options:

  If (Me!MyCheckBox) Then  ' forces evaluation of the control

  If Me!MyCheckBox = True Then

  If (Me!MyCheckBox = True) Then

  If (Me!MyCheckBox = Not False) Then

同样,在编写从布尔控件获取值的子例程或函数时,除非您确实想操纵该控件,否则始终将布尔参数声明为ByVal.在这种情况下,参数的数据类型应该是Access控件,而不是布尔值.其他任何事情都有隐式引用的风险.

Likewise, when writing subroutines or functions that get values from a Boolean control, always declare your Boolean parameters as ByVal unless you actually want to manipulate the control. In that case, your parameter's data type should be an Access control and not a Boolean value. Anything else runs the risk of implicit references.

最后,如果您在代码中设置了复选框的值,则实际上可以将其设置为任何数字,不仅是0和-1,而且任何非0的数字都将被视为True(因为它不是False) .尽管您可能会以HTML形式使用这种东西,但是对于Access应用程序来说,这不是正确的UI设计,因为用户无法查看控件中实际存储了什么值,这违背了选择它来编辑数据.

Last of all, if you set the value of a checkbox in code, you can actually set it to any number, not just 0 and -1, but any number other than 0 is treated as True (because it's Not False). While you might use that kind of thing in an HTML form, it's not proper UI design for an Access app, as there's no way for the user to be able to see what value is actually be stored in the control, which defeats the purpose of choosing it for editing your data.

这篇关于确定是否选中了“访问"复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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