使用if条件 - MS ACCESS,基于多个文本boxex值为表单上的文本框指定值

时间:2015-06-03 11:00:25

标签: database ms-access ms-access-2003

让我解释一下,我在表格上有字段(文本框),如下所示

textbox1 = can hold Yes or NO
textbox2 = can hold Yes or NO
textbox3 = can hold Yes or NO
textbox4 = can hold Yes or NO

textboxResult仅保存值为#34;是"

的文本框的标签(标题)的连接值

我尝试了许多可能的解决方案(在我看来),包括下面但没有运气。使用OR运算符在代码下面进行测试。

If Me.textbox1.Value = "Yes" And Me.textbox2.Value = "Yes" And _
               Me.textbox3.Value = "Yes" And Me.textbox4.Value = "Yes" Then
    Me.textboxResult.Value = Me.Label1.Caption & "," & 
    Me.Label2.Caption & "," & Me.Lable3.Caption & "," & 
    Me.Label4.Caption
Else
    Me.textboxResult.Value = "NA"
End If

我想为那些值为YES的文本框分配标签的标题。请帮忙

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要所有TextBox标签的连接值。因此,可能不会选择一个批量AND,也可以检查每个控件。类似的东西。

Dim txtResult As String

If Me.textbox1 = "Yes" Then _
    txtResult = txtResult & Me.textbox1.Controls.Item(0).Caption & ","

If Me.textbox2 = "Yes" Then _
    txtResult = txtResult & Me.textbox2.Controls.Item(0).Caption & ","

If Me.textbox3 = "Yes" Then _
    txtResult = txtResult & Me.textbox3.Controls.Item(0).Caption & ","

If Me.textbox4 = "Yes" Then _
    txtResult = txtResult & Me.textbox4.Controls.Item(0).Caption & ","

If Len(txtResult) > 0 Then
    Me.textboxResult = Left(txtResult, Len(txtResult)-1)
Else
    Me.textboxResult = "NA"
End If

注意 - Me.TextBoxName.Controls.Item(0)将返回与TextBoxName相关联的标签。如果文本框没有关联,那么您最终可能会出错。

编辑 - 修改后,如果您只是想使用标签的标题,只需将Me.textbox.Controls(0).Caption替换为Me.LableName.Caption

答案 1 :(得分:1)

根据你的评论判断你不应该为每个textBox做一个合并而是4个单独的ifs:

Dim txt As String
txt = ""
If Me.textbox1.Value = "Yes" Them _
    txt = txt & Me.Label1.Caption & ", "

If Me.textbox2.Value = "Yes" Then _
    txt = txt & Me.Label2.Caption & ", "

If Me.textbox3.Value = "Yes" Then _
    txt = txt & Me.Label3.Caption & ", "

If Me.textbox4.Value = "Yes" Then _
    txt = txt & Me.Label4.Caption & ", "

If Len (txt) > 0 Then
    txt = Left(txt, Len(txt) - 2)
Else 
    txt = "NA"
End If

Me.textboxResult = txt