Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reusing Form Code
Hi
I have two forms, let says they are called: * frmEmployee * frmLocation On both of these forms I have two Combo Boxes, one called cboLocation and another called cboDepartment. Both of these boxes are populated by code, so if somebody selects London then the London Departments appears in the cboDepartment, if they select Belfast another set of options appear. I would like to place this code into a separate module but how do i reference two different forms? Regards |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reusing Form Code
Are you hiding the first form or unloading it before you show the second form.
If you're hiding the form, I put this code behind the first userform: Option Explicit Private Sub CommandButton1_Click() Me.Hide frmLocation.Show Me.Show End Sub Private Sub UserForm_Initialize() Dim iCtr As Long For iCtr = 1 To 5 Me.ComboBox1.AddItem "A--" & iCtr Next iCtr End Sub Clicking on commandbutton1 would hide that userform and show the second form. This was the code I used behind the second userform: Option Explicit Private Sub UserForm_Initialize() Dim Pfx As String Dim iCtr As Long If frmEmployee.ComboBox1.ListIndex < 0 Then 'nothing selected Else Select Case LCase(frmEmployee.ComboBox1.Value) Case Is = "a--1": Pfx = "A" Case Is = "a--2": Pfx = "x" Case Is = "a--3": Pfx = "y" Case Else Pfx = "zzz" End Select For iCtr = 1 To 4 Me.ComboBox1.AddItem Pfx & "--" & iCtr Next iCtr End If End Sub As long as the first userform is still loaded (even if it's hidden), I could qualify my objects with the name of the userform. === If you're unloading the first userform, then showing the second userform, maybe you can use a public variable in a General module to store what you need. Have the first userform update that public variable. Have the second userform read (or read and update) that public variable. DMc2004 wrote: Hi I have two forms, let says they are called: * frmEmployee * frmLocation On both of these forms I have two Combo Boxes, one called cboLocation and another called cboDepartment. Both of these boxes are populated by code, so if somebody selects London then the London Departments appears in the cboDepartment, if they select Belfast another set of options appear. I would like to place this code into a separate module but how do i reference two different forms? Regards -- Dave Peterson |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reusing Form Code
Unfortantley the two forms are not in memory at the same time, but they use
the same data. "Dave Peterson" wrote: Are you hiding the first form or unloading it before you show the second form. If you're hiding the form, I put this code behind the first userform: Option Explicit Private Sub CommandButton1_Click() Me.Hide frmLocation.Show Me.Show End Sub Private Sub UserForm_Initialize() Dim iCtr As Long For iCtr = 1 To 5 Me.ComboBox1.AddItem "A--" & iCtr Next iCtr End Sub Clicking on commandbutton1 would hide that userform and show the second form. This was the code I used behind the second userform: Option Explicit Private Sub UserForm_Initialize() Dim Pfx As String Dim iCtr As Long If frmEmployee.ComboBox1.ListIndex < 0 Then 'nothing selected Else Select Case LCase(frmEmployee.ComboBox1.Value) Case Is = "a--1": Pfx = "A" Case Is = "a--2": Pfx = "x" Case Is = "a--3": Pfx = "y" Case Else Pfx = "zzz" End Select For iCtr = 1 To 4 Me.ComboBox1.AddItem Pfx & "--" & iCtr Next iCtr End If End Sub As long as the first userform is still loaded (even if it's hidden), I could qualify my objects with the name of the userform. === If you're unloading the first userform, then showing the second userform, maybe you can use a public variable in a General module to store what you need. Have the first userform update that public variable. Have the second userform read (or read and update) that public variable. DMc2004 wrote: Hi I have two forms, let says they are called: * frmEmployee * frmLocation On both of these forms I have two Combo Boxes, one called cboLocation and another called cboDepartment. Both of these boxes are populated by code, so if somebody selects London then the London Departments appears in the cboDepartment, if they select Belfast another set of options appear. I would like to place this code into a separate module but how do i reference two different forms? Regards -- Dave Peterson |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reusing Form Code
The second suggestion would work.
DMc2004 wrote: Unfortantley the two forms are not in memory at the same time, but they use the same data. "Dave Peterson" wrote: Are you hiding the first form or unloading it before you show the second form. If you're hiding the form, I put this code behind the first userform: Option Explicit Private Sub CommandButton1_Click() Me.Hide frmLocation.Show Me.Show End Sub Private Sub UserForm_Initialize() Dim iCtr As Long For iCtr = 1 To 5 Me.ComboBox1.AddItem "A--" & iCtr Next iCtr End Sub Clicking on commandbutton1 would hide that userform and show the second form. This was the code I used behind the second userform: Option Explicit Private Sub UserForm_Initialize() Dim Pfx As String Dim iCtr As Long If frmEmployee.ComboBox1.ListIndex < 0 Then 'nothing selected Else Select Case LCase(frmEmployee.ComboBox1.Value) Case Is = "a--1": Pfx = "A" Case Is = "a--2": Pfx = "x" Case Is = "a--3": Pfx = "y" Case Else Pfx = "zzz" End Select For iCtr = 1 To 4 Me.ComboBox1.AddItem Pfx & "--" & iCtr Next iCtr End If End Sub As long as the first userform is still loaded (even if it's hidden), I could qualify my objects with the name of the userform. === If you're unloading the first userform, then showing the second userform, maybe you can use a public variable in a General module to store what you need. Have the first userform update that public variable. Have the second userform read (or read and update) that public variable. DMc2004 wrote: Hi I have two forms, let says they are called: * frmEmployee * frmLocation On both of these forms I have two Combo Boxes, one called cboLocation and another called cboDepartment. Both of these boxes are populated by code, so if somebody selects London then the London Departments appears in the cboDepartment, if they select Belfast another set of options appear. I would like to place this code into a separate module but how do i reference two different forms? Regards -- Dave Peterson -- Dave Peterson |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reusing Form Code
For the same procedure to reference both forms I think you could solve your
problem by placing an argument in the procedure. E.g. Sub MyFormProc(a%) If a% = 1 then Your code here frmEmployee.show elseif a% = 2 then Your code here frmLocation.show End Sub To call procedu MyFormProc 1 (change to 2 for other form) Hope helpful -- JB "DMc2004" wrote: Hi I have two forms, let says they are called: * frmEmployee * frmLocation On both of these forms I have two Combo Boxes, one called cboLocation and another called cboDepartment. Both of these boxes are populated by code, so if somebody selects London then the London Departments appears in the cboDepartment, if they select Belfast another set of options appear. I would like to place this code into a separate module but how do i reference two different forms? Regards |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Finding the nearest match without reusing results | Excel Programming | |||
Reusing formula | Excel Discussion (Misc queries) | |||
Reusing grouping of non-adjacent cells | Excel Discussion (Misc queries) | |||
reusing a recordset for a pivot-table? | Excel Programming | |||
reusing a recordset for a pivot-table | Excel Programming |