Wrox Programmer Forums
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2004, 02:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default Clear Combo box

How do you clear contents of a combo box so it can have diferent items. This is the scenario:

I have two combo boxes. The selection in the first combo box will determine what items will be displayed in the second combo box. What is happening is, everytime I change the selection of the first combo box, the second combo box will get appended with items even though the items getting appended are already there.

Basically, I just want the second combo box to be cleared then populated everytime I make a selection in the first combo box.

Thanks,
Judy

 
Old February 23rd, 2004, 03:02 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 205
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dim lngCount as long

For lngCount = 0 To ComboBox1.listCount - 1
      ComboBox1.RemoveItem(0)
Next
 
Old February 23rd, 2004, 03:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you explain yourself a little better?

... append .. even though the items already there ...?

... populated whenever they select an item from combo box 1 ...?

If the item is already there why do you want to append it?

If you clear the combobox2 each time a selection is made in combobox1 what do you want to populate it with, the item selected from combobox1?

You can clear a combobox using the clear method: combobox.clear

You don't need to loop through each item.

If you could give us a bit more information.


Larry Asher
 
Old February 23rd, 2004, 03:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

Hi Larry,

Yehuda's code worked perfect. Thanks Yehuda.

When I select an item from the first combo box, the second combo box should be populated with items related to the first combo box selection. Ex:

First Combo Box selections :
1)Section A
  items under Section A to show in 2nd Combo Box :
      a) CEO
      b) Manager
      c) Supervisor
2)Section B
   items under Section B to show in 2nd Combo Box :
      a) Vendor
      b) Marketing Rep
      c) Employee


Second Combo box :

If Section A is selected in the first combo box then it should only have CEO, Manager & supervisor displayed in the second combo box.
If Section B is selected in the first combo box then it should only have Contractor, Vendor & Employee displayed.

Now...I chose section A then section B. What I have in the 2nd combo box now are both items from section A & B when it should only show items from Section B since it was the last selection from the 1st combo box.

The idea is to just display the items related to the 1st combo box.
I hope this was a little helpful.

If it still does not explain my problem better, then look at Yehuda's reply and the code used was the answer to this question.

Thanks,
Judy

 
Old February 23rd, 2004, 04:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default

I found out that you can also accomplish the same task by issuing the ADO command clear.

Combo1.clear

Thanks,
Judy

 
Old February 24th, 2004, 09:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Actually, you don't have to clear the second combo box at all. All you have to do is reset its rowsource depending on the selection of the first combo box.
Code:
   Me.cboCombo2.RowSourceType = "Value List"
   Select Case Me.cboCombo1
      Case "Section A"
         Me.cboCombo2.RowSource = "CEO; Manager; Supervisor"
      Case "Section B"
         Me.cboCombo2.RowSource = "Vendor; Marketing Rep; Employee"
      Case Else
         'Something else here.
   End Select


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old October 19th, 2006, 08:42 PM
Registered User
 
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to indexplus
Default

Yehuda, this isn't working for me. I am unable to chnage my mind after i make a selection in the first combo box (unless I close the form and start over). Here is my code (mega is first combo, major is second based on the choice of Mega)

--------------------------------------------
Private Sub cbMega_Name_Click()
Dim lngCount As Long

For lngCount = 0 To cbMega_Name.ListCount - 1
        cbMega_Name.RemoveItem (0)
Next

End Sub

---------------------------------------
my e-mail:
-----------------------------


Quote:
quote:Originally posted by Yehuda
 Dim lngCount as long

For lngCount = 0 To ComboBox1.listCount - 1
     ComboBox1.RemoveItem(0)
Next

Last edited by indexplus; February 21st, 2011 at 09:26 PM..
 
Old October 20th, 2006, 06:12 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well... in a loop of this type you should use the index to get each value.

this:
Code:
Dim lngCount as long

For lngCount = 0 To ComboBox1.listCount - 1
      ComboBox1.RemoveItem(0)
Next
Should actually be this:
Code:
Dim lngCount as long

For lngCount = 0 To ComboBox1.listCount - 1
      ComboBox1.RemoveItem(lngCount)
Next
However... the typical way to do this is as suggested previously:
ComboBox1.Clear

Woody Z http://www.learntoprogramnow.com
 
Old December 19th, 2007, 09:12 AM
Registered User
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Where do I type this in my form? I don't know anything about coding and very little about Access, so please please be specific.

Quote:
quote:Originally posted by jmss66
 I found out that you can also accomplish the same task by issuing the ADO command clear.

Combo1.clear

Thanks,
Judy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combo box to display items from parent combo box Gini Visual Studio 2008 0 June 18th, 2008 12:30 AM
Count in combo box(display results in text box) mboyisis Access 4 April 4th, 2008 07:08 AM
Combo box choice creating filtered combo box stevensj5 Access 11 September 13th, 2007 11:33 AM
How to clear all text box in a form at once time richie86 ASP.NET 1.0 and 1.1 Basics 1 October 31st, 2005 04:57 AM
Clear the top of combo box akibaMaila VB.NET 2002/2003 Basics 2 July 14th, 2005 04:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.