PC Review


Reply
Thread Tools Rate Thread

Create Combo Box and Populate

 
 
=?Utf-8?B?T3NzaWVNYWM=?=
Guest
Posts: n/a
 
      28th Oct 2007
I am trying to create a combo box and populate it in a macro. If I create the
combo box in one sub and then run a separate sub to populate it then it works
fine.

If I try to create and populate the combo in the same sub then it fails. It
creates and names the combo box but when it tries to populate it returns
Runtime Error 438. Will very much appreciate any help on what I am missing
here because I want to be able to put it into a workbook open routine.

My example code as follows:-

The following works if I run the subs separately:-

Dim objCombo As Object
Sub Create_Combo()

With Sheets("Sheet1")
.OLEObjects.Add _
(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=288, Top:=39, _
Width:=193.5, Height:=26.25) _
.Select
End With
Set objCombo = Selection
objCombo.Name = "MyCombo"

End Sub

Sub Combo_Populate()

Set objCombo = Sheets("Sheet1").MyCombo
objCombo.AddItem "A - AA"
objCombo.AddItem "B - BB"
objCombo.AddItem "C - CC"
objCombo.AddItem "D - DD"
objCombo.AddItem "E - EE"
objCombo.AddItem "F - FF"

End Sub


The following does not work as a single sub:-

Sub CreateAndPopulate()

With Sheets("Sheet1")
.OLEObjects.Add _
(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=288, Top:=39, _
Width:=193.5, Height:=26.25) _
.Select
End With

Set objCombo = Selection
objCombo.Name = "MyCombo"

'Runtime error 438 occurs with the following
objCombo.AddItem "A - AA"
objCombo.AddItem "B - BB"
objCombo.AddItem "C - CC"
objCombo.AddItem "D - DD"
objCombo.AddItem ("E - EE")
objCombo.AddItem ("F - FF")

End Sub

Regards,

OssieMac
 
Reply With Quote
 
 
 
 
=?Utf-8?B?ZXhjZWxlbnQ=?=
Guest
Posts: n/a
 
      28th Oct 2007
try add the Userformname

Userformname.objCombo.AddItem "A - AA"


"OssieMac" skrev:

> I am trying to create a combo box and populate it in a macro. If I create the
> combo box in one sub and then run a separate sub to populate it then it works
> fine.
>
> If I try to create and populate the combo in the same sub then it fails. It
> creates and names the combo box but when it tries to populate it returns
> Runtime Error 438. Will very much appreciate any help on what I am missing
> here because I want to be able to put it into a workbook open routine.
>
> My example code as follows:-
>
> The following works if I run the subs separately:-
>
> Dim objCombo As Object
> Sub Create_Combo()
>
> With Sheets("Sheet1")
> .OLEObjects.Add _
> (ClassType:="Forms.ComboBox.1", _
> Link:=False, _
> DisplayAsIcon:=False, _
> Left:=288, Top:=39, _
> Width:=193.5, Height:=26.25) _
> .Select
> End With
> Set objCombo = Selection
> objCombo.Name = "MyCombo"
>
> End Sub
>
> Sub Combo_Populate()
>
> Set objCombo = Sheets("Sheet1").MyCombo
> objCombo.AddItem "A - AA"
> objCombo.AddItem "B - BB"
> objCombo.AddItem "C - CC"
> objCombo.AddItem "D - DD"
> objCombo.AddItem "E - EE"
> objCombo.AddItem "F - FF"
>
> End Sub
>
>
> The following does not work as a single sub:-
>
> Sub CreateAndPopulate()
>
> With Sheets("Sheet1")
> .OLEObjects.Add _
> (ClassType:="Forms.ComboBox.1", _
> Link:=False, _
> DisplayAsIcon:=False, _
> Left:=288, Top:=39, _
> Width:=193.5, Height:=26.25) _
> .Select
> End With
>
> Set objCombo = Selection
> objCombo.Name = "MyCombo"
>
> 'Runtime error 438 occurs with the following
> objCombo.AddItem "A - AA"
> objCombo.AddItem "B - BB"
> objCombo.AddItem "C - CC"
> objCombo.AddItem "D - DD"
> objCombo.AddItem ("E - EE")
> objCombo.AddItem ("F - FF")
>
> End Sub
>
> Regards,
>
> OssieMac

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      28th Oct 2007
Try something like the following:

Dim objCombo As OLEObject

Sub CreateAndPopulate()

With Sheets("Sheet1")
Set objCombo = .OLEObjects.Add _
(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=288, Top:=39, _
Width:=193.5, Height:=26.25)
End With
With objCombo
.Name = "MyCombo"
With .Object
.AddItem "A - AA"
.AddItem "B - BB"
.AddItem "C - CC"
.AddItem "D - DD"
.AddItem ("E - EE")
.AddItem ("F - FF")
.ListIndex = 0
End With
End With
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"OssieMac" <(E-Mail Removed)> wrote in message
news:EBC30494-9D9B-4456-9F18-(E-Mail Removed)...
>I am trying to create a combo box and populate it in a macro. If I create
>the
> combo box in one sub and then run a separate sub to populate it then it
> works
> fine.
>
> If I try to create and populate the combo in the same sub then it fails.
> It
> creates and names the combo box but when it tries to populate it returns
> Runtime Error 438. Will very much appreciate any help on what I am missing
> here because I want to be able to put it into a workbook open routine.
>
> My example code as follows:-
>
> The following works if I run the subs separately:-
>
> Dim objCombo As Object
> Sub Create_Combo()
>
> With Sheets("Sheet1")
> .OLEObjects.Add _
> (ClassType:="Forms.ComboBox.1", _
> Link:=False, _
> DisplayAsIcon:=False, _
> Left:=288, Top:=39, _
> Width:=193.5, Height:=26.25) _
> .Select
> End With
> Set objCombo = Selection
> objCombo.Name = "MyCombo"
>
> End Sub
>
> Sub Combo_Populate()
>
> Set objCombo = Sheets("Sheet1").MyCombo
> objCombo.AddItem "A - AA"
> objCombo.AddItem "B - BB"
> objCombo.AddItem "C - CC"
> objCombo.AddItem "D - DD"
> objCombo.AddItem "E - EE"
> objCombo.AddItem "F - FF"
>
> End Sub
>
>
> The following does not work as a single sub:-
>
> Sub CreateAndPopulate()
>
> With Sheets("Sheet1")
> .OLEObjects.Add _
> (ClassType:="Forms.ComboBox.1", _
> Link:=False, _
> DisplayAsIcon:=False, _
> Left:=288, Top:=39, _
> Width:=193.5, Height:=26.25) _
> .Select
> End With
>
> Set objCombo = Selection
> objCombo.Name = "MyCombo"
>
> 'Runtime error 438 occurs with the following
> objCombo.AddItem "A - AA"
> objCombo.AddItem "B - BB"
> objCombo.AddItem "C - CC"
> objCombo.AddItem "D - DD"
> objCombo.AddItem ("E - EE")
> objCombo.AddItem ("F - FF")
>
> End Sub
>
> Regards,
>
> OssieMac


 
Reply With Quote
 
=?Utf-8?B?T3NzaWVNYWM=?=
Guest
Posts: n/a
 
      28th Oct 2007
Thanks Chip. Very much appreciated.

Regards,

OssieMac

"Chip Pearson" wrote:

> Try something like the following:
>
> Dim objCombo As OLEObject
>
> Sub CreateAndPopulate()
>
> With Sheets("Sheet1")
> Set objCombo = .OLEObjects.Add _
> (ClassType:="Forms.ComboBox.1", _
> Link:=False, _
> DisplayAsIcon:=False, _
> Left:=288, Top:=39, _
> Width:=193.5, Height:=26.25)
> End With
> With objCombo
> .Name = "MyCombo"
> With .Object
> .AddItem "A - AA"
> .AddItem "B - BB"
> .AddItem "C - CC"
> .AddItem "D - DD"
> .AddItem ("E - EE")
> .AddItem ("F - FF")
> .ListIndex = 0
> End With
> End With
> End Sub
>
>
> --
> Cordially,
> Chip Pearson
> Microsoft MVP - Excel, 10 Years
> Pearson Software Consulting
> www.cpearson.com
> (email on the web site)
>
> "OssieMac" <(E-Mail Removed)> wrote in message
> news:EBC30494-9D9B-4456-9F18-(E-Mail Removed)...
> >I am trying to create a combo box and populate it in a macro. If I create
> >the
> > combo box in one sub and then run a separate sub to populate it then it
> > works
> > fine.
> >
> > If I try to create and populate the combo in the same sub then it fails.
> > It
> > creates and names the combo box but when it tries to populate it returns
> > Runtime Error 438. Will very much appreciate any help on what I am missing
> > here because I want to be able to put it into a workbook open routine.
> >
> > My example code as follows:-
> >
> > The following works if I run the subs separately:-
> >
> > Dim objCombo As Object
> > Sub Create_Combo()
> >
> > With Sheets("Sheet1")
> > .OLEObjects.Add _
> > (ClassType:="Forms.ComboBox.1", _
> > Link:=False, _
> > DisplayAsIcon:=False, _
> > Left:=288, Top:=39, _
> > Width:=193.5, Height:=26.25) _
> > .Select
> > End With
> > Set objCombo = Selection
> > objCombo.Name = "MyCombo"
> >
> > End Sub
> >
> > Sub Combo_Populate()
> >
> > Set objCombo = Sheets("Sheet1").MyCombo
> > objCombo.AddItem "A - AA"
> > objCombo.AddItem "B - BB"
> > objCombo.AddItem "C - CC"
> > objCombo.AddItem "D - DD"
> > objCombo.AddItem "E - EE"
> > objCombo.AddItem "F - FF"
> >
> > End Sub
> >
> >
> > The following does not work as a single sub:-
> >
> > Sub CreateAndPopulate()
> >
> > With Sheets("Sheet1")
> > .OLEObjects.Add _
> > (ClassType:="Forms.ComboBox.1", _
> > Link:=False, _
> > DisplayAsIcon:=False, _
> > Left:=288, Top:=39, _
> > Width:=193.5, Height:=26.25) _
> > .Select
> > End With
> >
> > Set objCombo = Selection
> > objCombo.Name = "MyCombo"
> >
> > 'Runtime error 438 occurs with the following
> > objCombo.AddItem "A - AA"
> > objCombo.AddItem "B - BB"
> > objCombo.AddItem "C - CC"
> > objCombo.AddItem "D - DD"
> > objCombo.AddItem ("E - EE")
> > objCombo.AddItem ("F - FF")
> >
> > End Sub
> >
> > Regards,
> >
> > OssieMac

>

 
Reply With Quote
 
 
 
Reply

« ppic | sum if »
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
Re: Auto Populate a Combo Box based on the user selection of another Combo Box John W. Vinson Microsoft Access Forms 0 2nd Mar 2010 04:56 AM
Combo Box selection / AUTO POPULATE combo box in a different form kealaz@starbase74.com Microsoft Access Form Coding 2 14th Apr 2009 12:30 PM
Combo Box selection / AUTO POPULATE combo box in a different form kealaz Microsoft Access 0 13th Apr 2009 06:31 PM
Populate distinct one combo box depending on selection in another combo box Tony Girgenti Microsoft Access 5 9th Jan 2006 01:45 PM
Re: Create & Populate Combo Box Graham Mayor Microsoft Word Document Management 0 7th Jul 2003 08:27 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:37 AM.