Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the nearest match without reusing results goofy11 Excel Programming 15 September 17th 07 12:31 AM
Reusing formula Tony29 Excel Discussion (Misc queries) 7 September 7th 06 03:34 AM
Reusing grouping of non-adjacent cells [email protected] Excel Discussion (Misc queries) 2 May 28th 06 01:21 PM
reusing a recordset for a pivot-table? Bart op de grote markt Excel Programming 2 March 7th 06 03:34 PM
reusing a recordset for a pivot-table [email protected] Excel Programming 2 February 28th 06 03:36 PM


All times are GMT +1. The time now is 03:10 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"