VBA 中的复选框值是/否

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15517013/
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-08 15:12:00  来源:igfitidea点击:

Checkbox value in VBA yes/no

vbaaccess-vba

提问by Sergio

I have a form where I have a combobox and 4 checkboxes. I want to insert value from combobox and from chechboxes, when it is checked it is true and when not it is false.

我有一个表单,其中有一个组合框和 4 个复选框。我想从组合框和 chechboxes 插入值,当它被选中时它是真的,当它不是假时。

When I put check on checkbox should insert value YES in database and when I don't put check it will be NO.

当我勾选复选框时,应该在数据库中插入值 YES,当我不勾选时,它将是 NO。

Private Sub Command12_Click()
Dim strSQL As String
Dim rst As New ADODB.Recordset
Dim myval As String

If Me.Check2 Or Me.Check6 Or Me.Check8 Or Me.Check10 = -1 Then
myval = "Yes"
Else
myval = "No"

strSQL = "INSERT INTO Declaratie (Код_предр, Декларация, Данные_фирмы,   
Список_траспрота, Список_водителей) " & _
         " VALUES ('" & Me.НаимПредпр & "','" & Me.Check2 & "','" & Me.Check6 & "','" 
& Me.Check8 & "','" & Me.Check10 & "')"

Call AttServ(strSQL, rst) 'выполнение запроса
End If
End Sub

回答by Fionnuala

You can simply format a YesNo (boolean) control to get a Yes or a No, but this is probably not such a good idea. You would be better storing the YesNo as it is in a YesNo field and just formatting your form to show a Yes or No. Note that the code below will not work with a YesNo data type, because you are storing text.

您可以简单地格式化 YesNo(布尔值)控件以获得 Yes 或 No,但这可能不是一个好主意。您最好将 YesNo 存储在 YesNo 字段中,然后将表单格式化为显示 Yes 或 No。请注意,下面的代码不适用于 YesNo 数据类型,因为您存储的是文本。

Private Sub Command12_Click()
Dim strSQL As String
Dim rst As New ADODB.Recordset

    strSQL = "INSERT INTO Declaratie (Код_предр, Декларация, Данные_фирмы,   
    Список_траспрота, Список_водителей) " & _
             " VALUES ('" & Me.НаимПредпр & "','" _
             & Format(Me.Check2, "Yes/No") & "','" _
             & Format(Me.Check6, "Yes/No") & "','" 
             & Format(Me.Check8, "Yes/No") & "','" _
             & Format(Me.Check10, "Yes/No") & "')"

    Call AttServ(strSQL, rst) 'выполнение запроса
End Sub

回答by Kasnady

If Me.Check2.VALUE = 1 Then
myval = "Yes"
Else
myval = "No"
END IF

Add this code to every check box, cause as i know that you cannot using a code to made all checkbox or radiobutton do same thing. You must using a checkbox or radiobutton own code even it's same

将此代码添加到每个复选框,因为我知道您不能使用代码使所有复选框或单选按钮执行相同的操作。您必须使用复选框或单选按钮自己的代码,即使它是相同的