Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Is Multi click Event possible?

I have 17 CommandButtons, "Browse..." on a form.
Each one of them is suppose to open the FileOpen Dialog at a specific folder
After selecting a file, the file is to be assign to the TextBox
corresponding to each button
A few of them open to the same folder location

My question is, is it possible to write a code that doesn't require me
writing a click event for each Browse button?
And if there is such a code I would appreciate any help to point me in the
right direction.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Is Multi click Event possible?

In the userform, add this code

Option Explicit

Dim mcolEvents As Collection

Private Sub UserForm_Initialize()
Dim cBtnEvents As clsUserFormEvents
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "CommandButton" Then
Set cBtnEvents = New clsUserFormEvents
Set cBtnEvents.mButtonGroup = ctl
mcolEvents.Add cBtnEvents
End If
Next

End Sub


And create a Class module (call it clsUserFormEvents) module, with this code

Option Explicit

Public WithEvents mButtonGroup As msforms.CommandButton

Private Sub mButtonGroup_Click()
MsgBox mButtonGroup.Caption & " has been pressed"
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Ayo" wrote in message
...
I have 17 CommandButtons, "Browse..." on a form.
Each one of them is suppose to open the FileOpen Dialog at a specific
folder
After selecting a file, the file is to be assign to the TextBox
corresponding to each button
A few of them open to the same folder location

My question is, is it possible to write a code that doesn't require me
writing a click event for each Browse button?
And if there is such a code I would appreciate any help to point me in the
right direction.

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.misc
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Is Multi click Event possible?

You could put most of the code in a separate macro or function and have the
command buttons call the macro/function (passing it the folder to open and
the textbox control in which the filename s/b placed).

But you would still need a click event for each button.


"Ayo" wrote:

I have 17 CommandButtons, "Browse..." on a form.
Each one of them is suppose to open the FileOpen Dialog at a specific folder
After selecting a file, the file is to be assign to the TextBox
corresponding to each button
A few of them open to the same folder location

My question is, is it possible to write a code that doesn't require me
writing a click event for each Browse button?
And if there is such a code I would appreciate any help to point me in the
right direction.

Thanks

  #4   Report Post  
Posted to microsoft.public.excel.misc
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Is Multi click Event possible?

Thanks Bob. This is very helpful. I think I can make this work. I am very
grateful.

"Bob Phillips" wrote:

In the userform, add this code

Option Explicit

Dim mcolEvents As Collection

Private Sub UserForm_Initialize()
Dim cBtnEvents As clsUserFormEvents
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "CommandButton" Then
Set cBtnEvents = New clsUserFormEvents
Set cBtnEvents.mButtonGroup = ctl
mcolEvents.Add cBtnEvents
End If
Next

End Sub


And create a Class module (call it clsUserFormEvents) module, with this code

Option Explicit

Public WithEvents mButtonGroup As msforms.CommandButton

Private Sub mButtonGroup_Click()
MsgBox mButtonGroup.Caption & " has been pressed"
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Ayo" wrote in message
...
I have 17 CommandButtons, "Browse..." on a form.
Each one of them is suppose to open the FileOpen Dialog at a specific
folder
After selecting a file, the file is to be assign to the TextBox
corresponding to each button
A few of them open to the same folder location

My question is, is it possible to write a code that doesn't require me
writing a click event for each Browse button?
And if there is such a code I would appreciate any help to point me in the
right direction.

Thanks




  #5   Report Post  
Posted to microsoft.public.excel.misc
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Is Multi click Event possible?

Hey Bob,
Thanks again. I got that to work with the form that I have but I don't
know how to get to the next stage. Instead of showing a message box I want it
to open the Open File Dialog to a specific folder. Do I have to creat another
class for that? I am lost again. Help, please.

Ayo

"Bob Phillips" wrote:

In the userform, add this code

Option Explicit

Dim mcolEvents As Collection

Private Sub UserForm_Initialize()
Dim cBtnEvents As clsUserFormEvents
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "CommandButton" Then
Set cBtnEvents = New clsUserFormEvents
Set cBtnEvents.mButtonGroup = ctl
mcolEvents.Add cBtnEvents
End If
Next

End Sub


And create a Class module (call it clsUserFormEvents) module, with this code

Option Explicit

Public WithEvents mButtonGroup As msforms.CommandButton

Private Sub mButtonGroup_Click()
MsgBox mButtonGroup.Caption & " has been pressed"
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Ayo" wrote in message
...
I have 17 CommandButtons, "Browse..." on a form.
Each one of them is suppose to open the FileOpen Dialog at a specific
folder
After selecting a file, the file is to be assign to the TextBox
corresponding to each button
A few of them open to the same folder location

My question is, is it possible to write a code that doesn't require me
writing a click event for each Browse button?
And if there is such a code I would appreciate any help to point me in the
right direction.

Thanks






  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Is Multi click Event possible?

No, just change my throw-away example code, the MsgBox, with your real code,
the FileDialog.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Ayo" wrote in message
...
Hey Bob,
Thanks again. I got that to work with the form that I have but I don't
know how to get to the next stage. Instead of showing a message box I want
it
to open the Open File Dialog to a specific folder. Do I have to creat
another
class for that? I am lost again. Help, please.

Ayo

"Bob Phillips" wrote:

In the userform, add this code

Option Explicit

Dim mcolEvents As Collection

Private Sub UserForm_Initialize()
Dim cBtnEvents As clsUserFormEvents
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "CommandButton" Then
Set cBtnEvents = New clsUserFormEvents
Set cBtnEvents.mButtonGroup = ctl
mcolEvents.Add cBtnEvents
End If
Next

End Sub


And create a Class module (call it clsUserFormEvents) module, with this
code

Option Explicit

Public WithEvents mButtonGroup As msforms.CommandButton

Private Sub mButtonGroup_Click()
MsgBox mButtonGroup.Caption & " has been pressed"
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my
addy)



"Ayo" wrote in message
...
I have 17 CommandButtons, "Browse..." on a form.
Each one of them is suppose to open the FileOpen Dialog at a specific
folder
After selecting a file, the file is to be assign to the TextBox
corresponding to each button
A few of them open to the same folder location

My question is, is it possible to write a code that doesn't require me
writing a click event for each Browse button?
And if there is such a code I would appreciate any help to point me in
the
right direction.

Thanks






  #7   Report Post  
Posted to microsoft.public.excel.misc
Ayo Ayo is offline
external usenet poster
 
Posts: 489
Default Is Multi click Event possible?

Thanks for everything so far Bob, but I am going to ask for your help once
more if you don't mind. It seems I take 5 steps forward and three steps back.
It looked like I got it to work the way it should then all of a sudden, it's
not working any more. I guess I need someone else to look the code over for
me and maybe see what I don't see. I would reaally appreciate it if you can
look at this and maybe tell me what I am doing wrong.

Thanks
Ayo

Option Explicit

Public WithEvents mButtonGroup As msforms.CommandButton

Private Sub mButtonGroup_Click()
Dim btn_num As String
'MsgBox mButtonGroup.Caption & " has been pressed"
btn_num = Right(mButtonGroup.Name, Len(mButtonGroup.Name) - 6)
assignFiles btn_num
End Sub

Sub assignFiles(btn_num As String)
Dim getFolder As String
Dim fl As FileDialog
Dim ctl As Control
Dim vrtSelectedItem As Variant

Set fl = Application.FileDialog(msoFileDialogFilePicker) '.InitialFileName

With fl
For Each ctl In frmOpenFiles.Controls
If TypeName(ctl) = "TextBox" Then
If Len(ctl.Name) = 6 Then
If Right(ctl.Name, 1) = btn_num Then GoTo Get_File

ElseIf Len(ctl.Name) = 7 Then
If Right(ctl.Name, 2) = btn_num Then GoTo Get_File

End If
End If
Exit Sub
Next ctl
Get_File:
.InitialFileName = ctl.Value
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
getFolder = vrtSelectedItem
Next vrtSelectedItem
End If
If getFolder = Empty Then
MsgBox "You Must select a file to continue. Please Try Again."
Exit Sub
ElseIf getFolder < Empty Then
ctl.Text = getFolder
End If
End With

End Sub

"Bob Phillips" wrote:

No, just change my throw-away example code, the MsgBox, with your real code,
the FileDialog.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Ayo" wrote in message
...
Hey Bob,
Thanks again. I got that to work with the form that I have but I don't
know how to get to the next stage. Instead of showing a message box I want
it
to open the Open File Dialog to a specific folder. Do I have to creat
another
class for that? I am lost again. Help, please.

Ayo

"Bob Phillips" wrote:

In the userform, add this code

Option Explicit

Dim mcolEvents As Collection

Private Sub UserForm_Initialize()
Dim cBtnEvents As clsUserFormEvents
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "CommandButton" Then
Set cBtnEvents = New clsUserFormEvents
Set cBtnEvents.mButtonGroup = ctl
mcolEvents.Add cBtnEvents
End If
Next

End Sub


And create a Class module (call it clsUserFormEvents) module, with this
code

Option Explicit

Public WithEvents mButtonGroup As msforms.CommandButton

Private Sub mButtonGroup_Click()
MsgBox mButtonGroup.Caption & " has been pressed"
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my
addy)



"Ayo" wrote in message
...
I have 17 CommandButtons, "Browse..." on a form.
Each one of them is suppose to open the FileOpen Dialog at a specific
folder
After selecting a file, the file is to be assign to the TextBox
corresponding to each button
A few of them open to the same folder location

My question is, is it possible to write a code that doesn't require me
writing a click event for each Browse button?
And if there is such a code I would appreciate any help to point me in
the
right direction.

Thanks






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
ListBox Click Event BHatMJ Excel Discussion (Misc queries) 6 June 21st 07 09:34 PM
On click button event [email protected] Excel Worksheet Functions 1 November 30th 06 04:39 PM
Click event for checkbox from Forms toolbar Carolyn Excel Discussion (Misc queries) 6 September 11th 06 08:16 PM
call a function on control click event tkraju via OfficeKB.com Excel Discussion (Misc queries) 7 August 25th 06 07:22 AM


All times are GMT +1. The time now is 08:20 PM.

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"