根据带有 if 条件的多个文本框值将值分配给表单上的文本框 - MS ACCESS

Assign value to a text box on form based on multiple text boxex value with if condition - MS ACCESS

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

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 仅保留值为 "Yes"

的那些文本框的标签(标题)的串联值

我尝试了很多可能的解决方案(在我看来),包括以下但没有成功。还使用 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 的文本框分配标签的标题。请帮忙

如果我理解正确,您需要所有 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) 将 return 与 TextBoxName 关联的标签。如果文本框没有关联,那么您可能会遇到错误。


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

从您的评论来看,您不应该对每个文本框执行组合 if,而是 4 个单独的 if:

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