Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 12-01-2015, 08:27 AM   #1
jwal
Newly Registered User
 
Join Date: Dec 2015
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jwal is on a distinguished road
Angry VBA Enable TextBox with ComboBox Selection

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.Visibl e = 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.Visibl e = 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.

jwal is offline   Reply With Quote
Old 12-01-2015, 08:43 AM   #2
CJ_London
Super Moderator
 
Join Date: Feb 2013
Location: UK
Posts: 11,343
Thanks: 40
Thanked 3,670 Times in 3,538 Posts
CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light CJ_London is a glorious beacon of light
Re: VBA Enable TextBox with ComboBox Selection

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
__________________
CJ_London
_______________________
A little thanks goes a long way. If you have found this post useful, please tick the thanks button
CJ_London is offline   Reply With Quote
Old 12-01-2015, 08:49 AM   #3
Minty
AWF VIP
 
Minty's Avatar
 
Join Date: Jul 2013
Location: UK - Wiltshire
Posts: 6,437
Thanks: 166
Thanked 1,738 Times in 1,707 Posts
Minty is a jewel in the rough Minty is a jewel in the rough Minty is a jewel in the rough
Re: VBA Enable TextBox with ComboBox Selection

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

__________________
If we have helped please add to our reputation - click the scales symbol on the left, tick 'I approve' and leave a comment.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Minty is offline   Reply With Quote
Old 12-01-2015, 08:55 AM   #4
BlueIshDan
 
Join Date: May 2014
Posts: 1,122
Thanks: 155
Thanked 160 Times in 156 Posts
BlueIshDan is on a distinguished road
Re: VBA Enable TextBox with ComboBox Selection

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.TextEndResult_Violation_Performance.Visible = write_up
    Me.LabelEndResult_Violation_Performance.Visible = write_up
        
        
        
    ' OSHA Write-Up LETS
    Me.TextOSHAWriteUp_Violation_Performance.Visible = osha
    Me.LabelOSHAWriteUP_Violation_Performance.Visible = osha
    
    Me.TextEndResult_Violation_Performance.Visible = osha
    Me.LabelEndResult_Violation_Performance.Visible = osha

End Sub
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Blue's Age Poll:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Animated Sorting:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Mining File Directories:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
GetSubFolders:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

GetFileLines:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

FindSimilarValues:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Move (Avoid moving objects game):
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by BlueIshDan; 12-01-2015 at 09:17 AM.
BlueIshDan is offline   Reply With Quote
Old 12-01-2015, 09:10 AM   #5
jwal
Newly Registered User
 
Join Date: Dec 2015
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jwal is on a distinguished road
Re: VBA Enable TextBox with ComboBox Selection

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.
jwal is offline   Reply With Quote
Old 12-01-2015, 09:16 AM   #6
BlueIshDan
 
Join Date: May 2014
Posts: 1,122
Thanks: 155
Thanked 160 Times in 156 Posts
BlueIshDan is on a distinguished road
Re: VBA Enable TextBox with ComboBox Selection

what's with these being in every case?
Code:
Me.TextEndResult_Violation_Performance.Visible = False
Me.LabelEndResult_Violation_Performance.Visible = False
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Blue's Age Poll:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Animated Sorting:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Mining File Directories:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
GetSubFolders:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

GetFileLines:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

FindSimilarValues:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Move (Avoid moving objects game):
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
BlueIshDan is offline   Reply With Quote
Old 12-01-2015, 09:21 AM   #7
The_Doc_Man
Happy Retired Curmudgeon
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 14,693
Thanks: 93
Thanked 1,703 Times in 1,576 Posts
The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold
Re: VBA Enable TextBox with ComboBox Selection

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.

__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over one year and survived being home all day with the wife. She must really love me.
If I have helped you, please either click the thanks or click the scales.
The_Doc_Man is offline   Reply With Quote
Old 12-01-2015, 09:23 AM   #8
jwal
Newly Registered User
 
Join Date: Dec 2015
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
jwal is on a distinguished road
Re: VBA Enable TextBox with ComboBox Selection

This code gave me the same results except now the text boxes "OSHA Write-Up" & "End Result" are visible with before the combobox update.
Quote:
Originally Posted by BlueIshDan View Post
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.TextEndResult_Violation_Performance.Visible = write_up
    Me.LabelEndResult_Violation_Performance.Visible = write_up
        
        
        
    ' OSHA Write-Up LETS
    Me.TextOSHAWriteUp_Violation_Performance.Visible = osha
    Me.LabelOSHAWriteUP_Violation_Performance.Visible = osha
    
    Me.TextEndResult_Violation_Performance.Visible = osha
    Me.LabelEndResult_Violation_Performance.Visible = osha

End Sub
jwal is offline   Reply With Quote
Old 12-01-2015, 09:41 AM   #9
BlueIshDan
 
Join Date: May 2014
Posts: 1,122
Thanks: 155
Thanked 160 Times in 156 Posts
BlueIshDan is on a distinguished road
Re: VBA Enable TextBox with ComboBox Selection

Quote:
Originally Posted by jwal View Post
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.

__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Blue's Age Poll:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Animated Sorting:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Mining File Directories:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
GetSubFolders:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

GetFileLines:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

FindSimilarValues:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Move (Avoid moving objects game):
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
BlueIshDan is offline   Reply With Quote
Reply

Tags
combo box fields text box , vba , vba access 2013

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
populate textbox after MULTIPLE combobox selection PvL Forms 1 04-01-2013 07:33 PM
populate textbox after MULTIPLE combobox selection PvL Forms 1 04-01-2013 07:32 PM
Enable/Disable textbox based on combobox value Ratib Hussaini Forms 2 01-07-2013 12:20 AM
Do not allow entry in textbox depending on Combobox Selection mikerea90 Forms 5 01-29-2010 09:37 AM
Auto Fill data in textbox from Combobox selection Arvin Queries 22 07-24-2008 06:02 AM




All times are GMT -8. The time now is 03:23 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World