Darren Bartrup
Well-known Member
- Joined
- Mar 13, 2006
- Messages
- 1,297
- Office Version
- 365
- Platform
- Windows
I have a form in Excel which contains a Multipage control.
On page 2 of this control I have a frame named 'frmTeams' which has a number of text boxes and combo boxes added at runtime.
The controls are filled with data taken from a database.
Everything works beautifully: everything is filled in, the scroll bars let me see all the teams (78 at present) and their specialism (displayed in the combo box).
What I'm having trouble with is recognising when one of the combo boxes has its value changed. With a permanent combo box I'd use:
Is there any way of doing this for controls that are added at runtime?
The code I have is:
This runs when the workbook is first opened.
This code adds the controls to the frame:
As always, many thanks in advance for any help.
On page 2 of this control I have a frame named 'frmTeams' which has a number of text boxes and combo boxes added at runtime.
The controls are filled with data taken from a database.
Everything works beautifully: everything is filled in, the scroll bars let me see all the teams (78 at present) and their specialism (displayed in the combo box).
What I'm having trouble with is recognising when one of the combo boxes has its value changed. With a permanent combo box I'd use:
Code:
Private Sub ComboBox1_AfterUpdate()
End Sub
Is there any way of doing this for controls that are added at runtime?
The code I have is:
This runs when the workbook is first opened.
Code:
Static Sub Connect()
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & AdminDetails.txtRoot.Value & "\" & DBName & ";"
Exit Sub
End Sub
This code adds the controls to the frame:
Code:
Option Explicit
Public Sub Get_Teams()
Set rs = New ADODB.Recordset
'// Query database to return team names & allocated specialisms.
With rs
.Open "SELECT [tblTeamManagement].[team_name], " & _
"[tblTeamManagement].[team_specialism] " & _
"FROM [tblTeamManagement];", cn, adOpenKeyset, adLockReadOnly, adCmdText
End With
End Sub
Public Sub Show_Teams()
Dim rs1 As ADODB.Recordset
Dim tmpCtrl As Control
Dim iCtr As Integer
Set rs1 = New ADODB.Recordset
'// Query database to return available specialisms.
With rs1
.Open "SELECT [tblTeamSpecialism].[fldSpecialism] " & _
"FROM [tblTeamSpecialism];", cn, adOpenKeyset, adLockReadOnly, adCmdText
End With
rs1.MoveFirst
rs.MoveFirst
Do While Not rs.EOF
'// Add textbox to display team name.
Set tmpCtrl = AdminDetails.frmTeams.Controls.Add("Forms.TextBox.1", _
"Name" & rs.AbsolutePosition)
'// Position textbox on form.
tmpCtrl.Left = 6
tmpCtrl.Height = 18
tmpCtrl.Width = 150
tmpCtrl.Top = (rs.AbsolutePosition - 2) * 18 + 24
'// Fill textbox with value from database.
tmpCtrl = rs.Fields(0).Value
'// Add combobox to display team specialism.
Set tmpCtrl = AdminDetails.frmTeams.Controls.Add("Forms.ComboBox.1", _
"Spec" & rs.AbsolutePosition)
'// Postion combobox on form.
tmpCtrl.Left = 160
tmpCtrl.Height = 18
tmpCtrl.Width = 50
tmpCtrl.Top = (rs.AbsolutePosition - 2) * 18 + 24
'// Add options to combobox using values from database.
Do While Not rs1.EOF
With tmpCtrl
.AddItem rs1.Fields(0).Value
End With
rs1.MoveNext
Loop
'// Move back to first record in specialism table.
rs1.MoveFirst
'// Set value of combobox from database.
tmpCtrl = rs.Fields(1).Value
rs.MoveNext
Loop
'// Set scroll height for vertical scroll bar.
AdminDetails.frmTeams.ScrollHeight = (rs.RecordCount) * 18 + 24
End Sub
As always, many thanks in advance for any help.