VBA Enable TextBox with ComboBox Selection (1 Viewer)

jwal

Registered User.
Local time
Today, 10:07
Joined
Dec 1, 2015
Messages
13
:banghead: I have only been working with VBA for 1 week and I am stumped at this point. I have a form where I want 2 text boxes to become visible and enabled when a selection is made on a combo box. I am working in 2013. This is what I have.
Private Sub ComboViolation_Performance_AfterUpdate()
If ComboViolation_Performance.Value = "Verbal Safety Warning" Then
Me.TxtVerbal_Warning_Violation_Performance.Visible = True
Me.LabelVerbalWarning_Violation_Performance.Visible = True
Me.TextEndResult_Violation_Performance.Visible = True
Me.LabelEndResult_Violation_Performance.Visible = True
Else
Me.TxtVerbal_Warning_Violation_Performance.Visible = False
Me.LabelVerbalWarning_Violation_Performance.Visible = False
Me.TextEndResult_Violation_Performance.Visible = False
Me.LabelEndResult_Violation_Performance.Visible = False
End If
If ComboViolation_Performance.Value = "Write-Up Safety Warning" Then
Me.TextWriteUpSafetyWarning_Violation_Performance.Visible = True
Me.LabelWriteUpSafetyWarning_Violation_Performance.Visible = True
Me.TextEndResult_Violation_Performance.Visible = True
Me.LabelEndResult_Violation_Performance.Visible = True
Else
Me.TextWriteUpSafetyWarning_Violation_Performance.Visible = False
Me.LabelWriteUpSafetyWarning_Violation_Performance.Visible = False
Me.TextEndResult_Violation_Performance.Visible = False
Me.LabelEndResult_Violation_Performance.Visible = False
End If
If ComboViolation_Performance.Value = "OSHA Write-Up" Then
Me.TextOSHAWriteUp_Violation_Performance.Visible = True
Me.LabelOSHAWriteUP_Violation_Performance.Visible = True
Me.TextEndResult_Violation_Performance.Visible = True
Me.LabelEndResult_Violation_Performance.Visible = True
Else
Me.TextOSHAWriteUp_Violation_Performance.Visible = False
Me.LabelOSHAWriteUP_Violation_Performance.Visible = False
Me.TextEndResult_Violation_Performance.Visible = False
Me.LabelEndResult_Violation_Performance.Visible = False
End If
End Sub
The first text box & label appear with the corresponding selection, however, the End Result text box & label only appear when "OSHA Write-Up" is selected. The idea is to have this be a fillable form that will create a new record on the table.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:07
Joined
Feb 19, 2013
Messages
16,553
can I suggest you repost using the code tags to preserve indenting - your code is difficult to read without it. Code tags can be found in the advanced editor - # button
 

Minty

AWF VIP
Local time
Today, 14:07
Joined
Jul 26, 2013
Messages
10,354
That's because your code will always run the last else statement Unless "OSHA Write-Up" is selected.
You need to use a SELECT Case method to more elegantly program this, or add an Exit sub to all the other true If statements
 

BlueIshDan

☠
Local time
Today, 11:07
Joined
May 15, 2014
Messages
1,122
Compressed Version of what you currently have.

PROBLEM: I do not see your design here. Any way you can screenshot your form layout, and maybe give another description of what it is that you wish to do?

Also, something that might show your problem is the red highlighted sections below.

Code:
Private Sub ComboViolation_Performance_AfterUpdate()

    Dim verbal As Boolean: verbal = (ComboViolation_Performance.Value = "Verbal Safety Warning")
    Dim write_up As Boolean: write_up = (ComboViolation_Performance.Value = "Write-Up Safety Warning")
    Dim osha As Boolean: osha = (ComboViolation_Performance.Value = "OSHA Write-Up")
    
    
    ' Verbal Safety Warning LETS
    Me.TxtVerbal_Warning_Violation_Performance.Visible = verbal
    Me.LabelVerbalWarning_Violation_Performance.Visible = verbal
    
    Me.TextEndResult_Violation_Performance.Visible = verbal
    Me.LabelEndResult_Violation_Performance.Visible = verbal
    
    
    
    ' WRITE UP SAFETY WARNING LETS
    Me.TextWriteUpSafetyWarning_Violation_Performance.Visible = write_up
    Me.LabelWriteUpSafetyWarning_Violation_Performance .Visible = write_up
    
    Me.[COLOR="red"]TextEndResult_Violation_Performanc[/COLOR]e.Visible = write_up
    Me.[COLOR="red"]LabelEndResult_Violation_Performance[/COLOR].Visible = write_up
        
        
        
    ' OSHA Write-Up LETS
    Me.TextOSHAWriteUp_Violation_Performance.Visible = osha
    Me.LabelOSHAWriteUP_Violation_Performance.Visible = osha
    
    Me.[COLOR="Red"]TextEndResult_Violation_Performance[/COLOR].Visible = osha
    Me.[COLOR="red"]LabelEndResult_Violation_Performance[/COLOR].Visible = osha

End Sub
 
Last edited:

jwal

Registered User.
Local time
Today, 10:07
Joined
Dec 1, 2015
Messages
13
I have a combobox "ComboViolation_Performance" with 3 options, "Verbal Safety Warning", "Write-Up Safety Warning" and "OSHA Write-Up" & defaulted to NULL. When a specific item is selected two text boxes would become visible. "Verbal Safety Warning", "Write-Up Safety Warning", "OSHA Write-Up" and "End Result" are each individual columns in a table that would need to be updated as a new entry (autonumber) upon completing this form. The text boxes would remain hidden until the selection was made.
 

BlueIshDan

☠
Local time
Today, 11:07
Joined
May 15, 2014
Messages
1,122
what's with these being in every case?
Code:
Me.TextEndResult_Violation_Performance.Visible = False
Me.LabelEndResult_Violation_Performance.Visible = False
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:07
Joined
Feb 28, 2001
Messages
26,999
Think along these lines:

Code:
Private Sub ComboViolation_Performance_AfterUpdate()

Dim boState as Boolean
Dim cboValue as String

cboValue = ComboViolation_Performance     'get value

boState = ( cboValue = "Verbal Safety Warning" ) OR _
               ( cboValue = Write-Up Safety Warning ) OR _
               ( cboValue = "OSHA Write-Up" )    'one of the warning cases?

Me.TxtVerbal_Warning_Violation_Performance.Visible = boState
Me.LabelVerbalWarning_Violation_Performance.Visible = boState
Me.TextEndResult_Violation_Performance.Visible = boState
Me.LabelEndResult_Violation_Performance.Visible = boState

End Sub

Also, do yourself a really BIG favor. Shorten those names. The longer the name, the higher the odds of a typo. Yes, you will find them when you try to compile, but why make yourself type quite so much?

Notes: You don't have to ask for the .Value of a control that has a value because the default in any expression is to return the contents of .VALUE in the expression that uses the control name that way.

I might also use the .LostFocus event rather than the .AfterUpdate event, because if you don't make a change to the current contents of the form, you won't HAVE an .AfterUpdate event - but you will always have a .LostFocus event based on what I see as your usage. Of course, from .LostFocus, you would have to determine whether anything is selected, but you can test the .ListIndex value for the combo box and if it is -1, nothing is selected.
 

jwal

Registered User.
Local time
Today, 10:07
Joined
Dec 1, 2015
Messages
13
This code gave me the same results except now the text boxes "OSHA Write-Up" & "End Result" are visible with before the combobox update.
Compressed Version of what you currently have.

PROBLEM: I do not see your design here. Any way you can screenshot your form layout, and maybe give another description of what it is that you wish to do?

Also, something that might show your problem is the red highlighted sections below.

Code:
Private Sub ComboViolation_Performance_AfterUpdate()

    Dim verbal As Boolean: verbal = (ComboViolation_Performance.Value = "Verbal Safety Warning")
    Dim write_up As Boolean: write_up = (ComboViolation_Performance.Value = "Write-Up Safety Warning")
    Dim osha As Boolean: osha = (ComboViolation_Performance.Value = "OSHA Write-Up")
    
    
    ' Verbal Safety Warning LETS
    Me.TxtVerbal_Warning_Violation_Performance.Visible = verbal
    Me.LabelVerbalWarning_Violation_Performance.Visible = verbal
    
    Me.TextEndResult_Violation_Performance.Visible = verbal
    Me.LabelEndResult_Violation_Performance.Visible = verbal
    
    
    
    ' WRITE UP SAFETY WARNING LETS
    Me.TextWriteUpSafetyWarning_Violation_Performance.Visible = write_up
    Me.LabelWriteUpSafetyWarning_Violation_Performance .Visible = write_up
    
    Me.[COLOR="red"]TextEndResult_Violation_Performanc[/COLOR]e.Visible = write_up
    Me.[COLOR="red"]LabelEndResult_Violation_Performance[/COLOR].Visible = write_up
        
        
        
    ' OSHA Write-Up LETS
    Me.TextOSHAWriteUp_Violation_Performance.Visible = osha
    Me.LabelOSHAWriteUP_Violation_Performance.Visible = osha
    
    Me.[COLOR="Red"]TextEndResult_Violation_Performance[/COLOR].Visible = osha
    Me.[COLOR="red"]LabelEndResult_Violation_Performance[/COLOR].Visible = osha

End Sub
 

BlueIshDan

☠
Local time
Today, 11:07
Joined
May 15, 2014
Messages
1,122
This code gave me the same results except now the text boxes "OSHA Write-Up" & "End Result" are visible with before the combobox update.

That's because its the same as the code you posted, just shortened.
 

Users who are viewing this thread

Top Bottom