2015-06-03 17 views
2

让我解释一下,我有一个窗体上的字段(文本框),如下赋值到表单中的文本框基于与如果条件多个文本boxex值 - 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持有级联只有那些值为“是”的文本框的标签值(标题)

我已经尝试了许多可能的解决方案(在我的脑海中),包括下面但没有运气。也使用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的文本框指定标签的标题。请帮助

+1

你在说什么文本框的Caption? Texboxes没有标题!你只提到一次“标签”。你应该清楚'标签'和'文本框'之间以及'值'和'标题'之间的区别! **和**包括一个预期结果的例子**和**“但没有运气”是什么意思?它不起作用吗,它是否会崩溃,if-body没有达到? – luk2302

+0

感谢您luk2302的及时回复。为了清楚理解,我编辑了问题主体和代码。它的工作原理,但不是我想要它工作的方式。我只想连接(加入)那些带有YES值的文本框标签。 – DawaR

回答

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).CaptionMe.LableName.Caption

+1

这就是我所说的类似解决方案:D – luk2302

+0

感谢PaulFracis,它按照期望工作。 – DawaR

+0

很高兴帮助。 :)祝你好运 – PaulFrancis

1

从您的评论来看,你不应该做一个组合,如果而是4独立ifs for each textBox:

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 
+0

绝对或类似的*** ***伟大的思想都相同***;) – PaulFrancis

+0

谢谢你luk2302太,为你的帮助和解决方案:) – DawaR

相关问题