Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set ComboBox DataSource Dynamically

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I'm creating combo boxes at run time. I would like to set the data source dynamically like this.
Code:
Cob1.DataSource = SomeView
where SomeView is a variable containing the name of the view I want to use. I have the following code but obviously I still have to code the data source manually. Ideally I want the data source name stored in a table I use to create the combo boxes.
Code:
Private Sub setComboBoxDataSource(ByRef cbo1 As ComboBox, ByVal cboNbr As Integer)
  Select Case cboNbr
    Case 1
      cbo1.DataSource = UnitTypeView
    Case 2
      cbo1.DataSource = MakeView
    Case 3
      cbo1.DataSource = ModelView
    Case 4
      cbo1.DataSource = DoorView
  End Select
End Sub

Auguy
Sylvania/Toledo Ohio
 
You've stated what you want to do, but I'm not sure what your question is. Are you looking for a general guide on how to do this, or do you need to know something more specific?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I'm adding combo boxes at run time. I want to store the name of the combo box data source (the view name) and other associated data in an SQL table. When creating the combo box I want do do something like
Code:
Cbo1.DataSource = SomeView
where SomeView is the name of the view in the table. All of the views will be available before the creation of the combo boxes. I'm trying to avoid the Select Case statement in my original post

Auguy
Sylvania/Toledo Ohio
 
Ah, I get it now. If you store cboNbr in the database as well, you can do something like this:

Code:
'Assume a database connection object named 'conn'

Private Sub setComboBoxDataSource(ByRef cbo1 As ComboBox, ByVal cboNbr As Integer)
[indent]Dim cmd As SqlCommand[/indent]
[indent]Dim SQLStr As String = "Select DataSource from ComboDataSources where cboNbr=" & cboNbr.ToString[/indent]

[indent]cmd = New SqlCommand(SqlStr, conn)[/indent]

[indent]If Not IsDbNull(cmd.ExecuteScalar) Then[/indent]
[indent][indent]cbo1.DataSource = cmd.ExecuteScalar[/indent][/indent]
[indent]EndIf[/indent]
End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
OK, thanks. I will play around a bit to see if I can get it to work

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top