DawaR
DawaR

Reputation: 25

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

Let me explain,i have fields (text boxes) on a form as below

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 holds the Concatenated value of the labels (captions) of only those text boxes whose values are "Yes"

I have tried many possible solutions (in my mind) including below but no luck. Tested below code with OR operator also.

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

I want to assign the label's caption of those textboxes whose value is YES. Please help

Upvotes: 2

Views: 129

Answers (2)

luk2302
luk2302

Reputation: 57204

Judging from your comment you should not do a combined if but rather 4 seperate 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

Upvotes: 1

PaulFrancis
PaulFrancis

Reputation: 5819

If I understand correctly you need the concatenated value of all the TextBox labels. So one bulk AND might not be the option, maybe check every control. something like.

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

NOTE - Me.TextBoxName.Controls.Item(0) will return the associated label with the TextBoxName. If the text box does not have an association, then you might end up with errors.


EDIT - After your edit, if you simply want to use the label's caption, just replace the Me.textbox.Controls(0).Caption to Me.LableName.Caption

Upvotes: 1

Related Questions