PC Review


Reply
Thread Tools Rate Thread

Array of comboboxes

 
 
Dennis Benjamin
Guest
Posts: n/a
 
      12th Dec 2006
Hi All

I'm working with a spreadsheet where I want to present the user with several
comboboxes, the contents of which are pulled from an Access database. As the
project grew, I kept adding Comboboxes, and today decided that instead of
working one by one with the tablenames and the Comboboxes I would make an
array of each and then use one loop to step throgh them. The one-by-one code
that worked looked like:

'Instantiate the Connectionobject.
Set cnt = New ADODB.Connection

'Create the connectionstring.
stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & stDB & ";"

'Create the SQL-statement.
stTableName = "[Animal Species]"
stSQL1 = "SELECT * FROM " & stTableName
With cnt
.CursorLocation = adUseClient 'Necesary for creating disconnected
recordset.
.Open stConn 'Open connection.
'Instantiate the Recordsetobject and execute the SQL-state.
Set rst = .Execute(stSQL1)
With rst
Set .ActiveConnection = Nothing 'Disconnect the recordset.
k = .Fields.Count
'Populate the array with the whole recordset.
vaSpeciesData = .GetRows
End With
End With

'Close the connection.
cnt.Close

'Manipulate the Combobox's properties and show the form.
With Worksheets(1).Species_Combo
.Clear
.ColumnCount = 2
.ColumnWidths = "0;20"
.BoundColumn = 1
.TextColumn = k
.List = Application.Transpose(vaSpeciesData)
' .ListIndex = -1
End With

So I made an array of strings for the Table names, and an array of
comboboxes like so:

Dim sTables(1 To 3) As String
Dim cmbBoxes(1 To 3) As ComboBox

sTables(1) = "[Animal Species]"
sTables(2) = "[tbl Projects]"
sTables(3) = "tblCellLines"

Set cmbBoxes(1) = Worksheets(1).Species_Combo.Object
Set cmbBoxes(2) = Worksheets(1).Project_Combo.Object
Set cmbBoxes(3) = Worksheets(1).Model_Combo.Object

and everything goes up until I try

With cmbBoxes(i)
.Clear
.ColumnCount = 2
.ColumnWidths = "0;20"
.BoundColumn = 1
.TextColumn = k
.List = Application.Transpose(vaData)
' .ListIndex = -1
End With


The .Clear statement crashes Excel. So, can I do what I'm trying to, and if
so, where did I go wrong?

Thanks for any help!

Dennis


 
Reply With Quote
 
 
 
 
NickHK
Guest
Posts: n/a
 
      13th Dec 2006
Dennis,
This works for me, with Combos from the Controls tool box. Think you are
missing the "MSForms" :

Private Sub CommandButton1_Click()
Dim cbo(1 To 3) As MSForms.ComboBox
Dim i As Long

Set cbo(1) = Worksheets(1).ComboBox1
Set cbo(2) = Worksheets(1).ComboBox2
Set cbo(3) = Worksheets(1).ComboBox3

For i = 1 To 3
With cbo(i)
.Clear
Debug.Print .ListCount
End With
Next

End Sub

NickHK

"Dennis Benjamin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All
>
> I'm working with a spreadsheet where I want to present the user with

several
> comboboxes, the contents of which are pulled from an Access database. As

the
> project grew, I kept adding Comboboxes, and today decided that instead of
> working one by one with the tablenames and the Comboboxes I would make an
> array of each and then use one loop to step throgh them. The one-by-one

code
> that worked looked like:
>
> 'Instantiate the Connectionobject.
> Set cnt = New ADODB.Connection
>
> 'Create the connectionstring.
> stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & stDB & ";"
>
> 'Create the SQL-statement.
> stTableName = "[Animal Species]"
> stSQL1 = "SELECT * FROM " & stTableName
> With cnt
> .CursorLocation = adUseClient 'Necesary for creating

disconnected
> recordset.
> .Open stConn 'Open connection.
> 'Instantiate the Recordsetobject and execute the SQL-state.
> Set rst = .Execute(stSQL1)
> With rst
> Set .ActiveConnection = Nothing 'Disconnect the recordset.
> k = .Fields.Count
> 'Populate the array with the whole recordset.
> vaSpeciesData = .GetRows
> End With
> End With
>
> 'Close the connection.
> cnt.Close
>
> 'Manipulate the Combobox's properties and show the form.
> With Worksheets(1).Species_Combo
> .Clear
> .ColumnCount = 2
> .ColumnWidths = "0;20"
> .BoundColumn = 1
> .TextColumn = k
> .List = Application.Transpose(vaSpeciesData)
> ' .ListIndex = -1
> End With
>
> So I made an array of strings for the Table names, and an array of
> comboboxes like so:
>
> Dim sTables(1 To 3) As String
> Dim cmbBoxes(1 To 3) As ComboBox
>
> sTables(1) = "[Animal Species]"
> sTables(2) = "[tbl Projects]"
> sTables(3) = "tblCellLines"
>
> Set cmbBoxes(1) = Worksheets(1).Species_Combo.Object
> Set cmbBoxes(2) = Worksheets(1).Project_Combo.Object
> Set cmbBoxes(3) = Worksheets(1).Model_Combo.Object
>
> and everything goes up until I try
>
> With cmbBoxes(i)
> .Clear
> .ColumnCount = 2
> .ColumnWidths = "0;20"
> .BoundColumn = 1
> .TextColumn = k
> .List = Application.Transpose(vaData)
> ' .ListIndex = -1
> End With
>
>
> The .Clear statement crashes Excel. So, can I do what I'm trying to, and

if
> so, where did I go wrong?
>
> Thanks for any help!
>
> Dennis
>
>



 
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
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R Microsoft Excel Programming 3 13th Nov 2007 04:08 PM
getting data from an array of varying amounts of comboboxes lar.thegreat@gmail.com Microsoft Excel Programming 1 20th Jun 2007 06:39 PM
Comboboxes =?Utf-8?B?bXItYmVhcg==?= Microsoft Excel New Users 1 21st Nov 2006 10:05 AM
meaning of : IF(Switch; Average(array A, array B); array A) =?Utf-8?B?RFhBVA==?= Microsoft Excel Worksheet Functions 1 24th Oct 2006 06:11 PM
Read comboboxes into array Steph Microsoft Excel Programming 3 11th Aug 2004 04:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 PM.