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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic control array and Data Control 1

Status
Not open for further replies.

apkohn

Technical User
Joined
Nov 27, 2000
Messages
62
Location
AU
I am writing an app in VB5.

Part of my UI design calls for an array of Combo boxes (normal or DBCombo, I'm not fussed) that will have as the list, the contents of a field in an access db.

What I am currently doing, is to have a Data Control linked to the table, called datMaterials.

No I can only place the first combobox at design time, and this is called cmbMaterial(0).

A subroutine then determines (according to other factors) how many controls are needed and loads them. The problem I am having though, is that even though the ListField, RowField properties etc... are inherited by the newly loaded controls, the lists are always empty, so that only cmbMaterials(0) has a populated list.

Does anyone have any ideas, remembering that the lists have to be updatable if the user chooses to add a new definition, through a seperate process.
 
Hi

Since you're using VB5 you basically stuck unless you re-think your design.
The problem is VB5 cannot dynamically create controls at run-time. VB6 can however.

Unless you upgrade you either going to have to add as much controls as you think is possible (i doubt that'll work) or change your design.

You could for example play around with the flex grid or any other as an alternative. I'm not quite sure why you chose that design so bear with me.
THe columns of the grid can be the amount of combos you'd add & the rows the field contents.

Hope this helps
caf

"Of course I can totally off the point"
 
Actually, you are able to create controls at runtime to some extent. While VB5 doesn't have the asbility to use the control.add statement, it is possible to add elements to a control array.

Hence only one control needs to be created, and an Index assigned. from then on using the statement: Load MyControl(i), will create a new control. the control can also be unloaded in a similar fashion... Unload MyControl(i).

The way the load seems to work, is the new control is created with the properties of the first control in the array (eg MyControl(0)). This is indeed happening with with the DBCombos created have all the same properties as the control placed in design time, except the drop down list doesn't populate.
 
How about trying this:

Do not use combo boxes bound to a data control. Instead, when you create the controls, create the required recordset in code and loop through the recordset using the AddItem method of the combo boxes.

To use the code below, create a project and add one combo box, called cboBox, to the form, and set the combo boxes Index property to 0.
Create a database and add a table named Table1, with 5 fields, named Field0 through to Field4[/b].
Paste the code into the code window of the form.

Private Sub Form_Load()

Dim i As Integer
Dim sSql As String
Dim db As Database
Dim rs As Recordset

' open the database
Set db = Workspaces(0).OpenDatabase("C:\aa\db1.mdb", False, False)
' load 4 other combo boxes
For i = 0 To 4
If i <> 0 Then
Load cboBox(i) ' create control
cboBox(i).Top = cboBox(i - 1).Top + 500 ' move control down
End If
' set sql statement to use to fill the combo
sSql = &quot;SELECT Field&quot; & i & &quot; FROM Table1 ORDER BY Field&quot; & i
' open the recordset
Set rs = db.OpenRecordset(sSql, dbOpenSnapshot)
' loop through the records to EOF
Do Until rs.EOF
cboBox(i).AddItem rs(&quot;Field&quot; & i)
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cboBox(i).Visible = True
Next i
' close the database
db.Close
Set db = Nothing
End Sub


Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top