PC Review


Reply
Thread Tools Rate Thread

Clearing text boxes based on combo selections

 
 
Bellyjeans
Guest
Posts: n/a
 
      28th Jun 2010
Hi all,

I have a situation that I'm sure is easy to fix, but I'm having a
moment and my brain won't work. I have a combo box. I have the
following code on the after update event of the combo box:

If Forms!frmStatus.cboStatusName = "Selection 1" Then
Me.txtBox1.Visible = True
Me.txtBox 2.Visible = False
Else
Me.txtBox1.Visible = False
Me.txtBox2.Visible = True
End If

If Forms!frmStatus.cboStatusName = "Selection 2" Then
Me.txtBox3.Visible = True
Me.txtBox4.Visible = True
Me.txtBox2.Visible = False
Else
Me.txtBox3.Visible = False
Me.txtBox4.Visible = False
Me.txtBox2.Visible = True
End If

If Forms!frmStatus.cboStatusName = "Selection 3" Then
Me.txtBox5.Visible = True
ElseIf Forms!frmStatus.StatusName = "Selection 4" Then
Me.txtBox5.Visible = True
Else
Me.txtBox5.Visible = False
End If

If Forms!frmStatus.cboStatusName = "Selection5" Then
Me.cboBox6.Visible = True
Else
Me.cboBox6.Visible = False
End If

Basically, in a nutshell, the coding makes a number of fields visible
or invisible, based on what is selected in the combo box
"cboStatusName". My problem is that if somebody changes the selection
in cboStatusName, the value in certain combo boxes remains there, even
if that field is made invisible. How do I make it so that if the
value in cboStatusName is changed, the fields that become invisible
also clear their values?

Thanks!
 
Reply With Quote
 
 
 
 
Jeff Boyce
Guest
Posts: n/a
 
      28th Jun 2010
One approach would be to set the value of the textbox to Null, something
like (untested):

Me!txtBox1 = Null

Good luck!

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"Bellyjeans" <(E-Mail Removed)> wrote in message
news:7e13913f-0434-4066-97f3-(E-Mail Removed)...
> Hi all,
>
> I have a situation that I'm sure is easy to fix, but I'm having a
> moment and my brain won't work. I have a combo box. I have the
> following code on the after update event of the combo box:
>
> If Forms!frmStatus.cboStatusName = "Selection 1" Then
> Me.txtBox1.Visible = True
> Me.txtBox 2.Visible = False
> Else
> Me.txtBox1.Visible = False
> Me.txtBox2.Visible = True
> End If
>
> If Forms!frmStatus.cboStatusName = "Selection 2" Then
> Me.txtBox3.Visible = True
> Me.txtBox4.Visible = True
> Me.txtBox2.Visible = False
> Else
> Me.txtBox3.Visible = False
> Me.txtBox4.Visible = False
> Me.txtBox2.Visible = True
> End If
>
> If Forms!frmStatus.cboStatusName = "Selection 3" Then
> Me.txtBox5.Visible = True
> ElseIf Forms!frmStatus.StatusName = "Selection 4" Then
> Me.txtBox5.Visible = True
> Else
> Me.txtBox5.Visible = False
> End If
>
> If Forms!frmStatus.cboStatusName = "Selection5" Then
> Me.cboBox6.Visible = True
> Else
> Me.cboBox6.Visible = False
> End If
>
> Basically, in a nutshell, the coding makes a number of fields visible
> or invisible, based on what is selected in the combo box
> "cboStatusName". My problem is that if somebody changes the selection
> in cboStatusName, the value in certain combo boxes remains there, even
> if that field is made invisible. How do I make it so that if the
> value in cboStatusName is changed, the fields that become invisible
> also clear their values?
>
> Thanks!



 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      28th Jun 2010
Set the textbox value to Null

If Forms!frmStatus.cboStatusName = "Selection 1" Then
Me.txtBox1.Visible = True
Me.txtBox 2.Visible = False
Me.txtBox 2 = Null
Else
Me.txtBox1.Visible = False
Me.txtBox1 = Null
Me.txtBox2.Visible = True
End If

Or you could just cycle through all the controls and if the visible property
is false then set them to null

The easy way to do that is to assign the relevant controls a tag property like
"HiddenToNull"

Then add this to the bottom of your current procedure
Dim ctl as Control
For each ctl in Me.Controls
If ctl.Tag = "HiddenToNull" THEN
IF ctl.Visible = False THEN
ctl.value = null
End If
End If
Next ctl

The advantage of doing it with the loop is that if you edit the controls you
are showing/hiding based on the combobox, the loop should automatically take
care of the changes. Some disadvantages:
== you must remember to tag any controls that you want to get this behavior
== the looping process is less efficient than specifically coding each item
(although with most computers is will not be noticeable to the human.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Bellyjeans wrote:
> Hi all,
>
> I have a situation that I'm sure is easy to fix, but I'm having a
> moment and my brain won't work. I have a combo box. I have the
> following code on the after update event of the combo box:
>
> If Forms!frmStatus.cboStatusName = "Selection 1" Then
> Me.txtBox1.Visible = True
> Me.txtBox 2.Visible = False
> Else
> Me.txtBox1.Visible = False
> Me.txtBox2.Visible = True
> End If
>
> If Forms!frmStatus.cboStatusName = "Selection 2" Then
> Me.txtBox3.Visible = True
> Me.txtBox4.Visible = True
> Me.txtBox2.Visible = False
> Else
> Me.txtBox3.Visible = False
> Me.txtBox4.Visible = False
> Me.txtBox2.Visible = True
> End If
>
> If Forms!frmStatus.cboStatusName = "Selection 3" Then
> Me.txtBox5.Visible = True
> ElseIf Forms!frmStatus.StatusName = "Selection 4" Then
> Me.txtBox5.Visible = True
> Else
> Me.txtBox5.Visible = False
> End If
>
> If Forms!frmStatus.cboStatusName = "Selection5" Then
> Me.cboBox6.Visible = True
> Else
> Me.cboBox6.Visible = False
> End If
>
> Basically, in a nutshell, the coding makes a number of fields visible
> or invisible, based on what is selected in the combo box
> "cboStatusName". My problem is that if somebody changes the selection
> in cboStatusName, the value in certain combo boxes remains there, even
> if that field is made invisible. How do I make it so that if the
> value in cboStatusName is changed, the fields that become invisible
> also clear their values?
>
> Thanks!

 
Reply With Quote
 
Bellyjeans
Guest
Posts: n/a
 
      28th Jun 2010
Awesome, the loop seems to be working. Thanks!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate Text or Combo Box Based on other Form Selections PPCO Microsoft Access 0 30th Sep 2009 04:46 PM
Populating text box based on selection in two combo boxes SHAWTY721 Microsoft Access Forms 3 10th Oct 2007 08:39 PM
Clearing Text Boxes of a Form with a Combo Box =?Utf-8?B?Um9u?= Microsoft Access Forms 6 21st Nov 2006 09:28 PM
Combo boxes based off a text box =?Utf-8?B?ZXNwYXJ6YW9uZQ==?= Microsoft Access Forms 0 23rd Jun 2006 12:55 AM
updating text boxes based on combo box value =?Utf-8?B?ZGF2ZQ==?= Microsoft Access 1 22nd Nov 2005 05:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:10 PM.