954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Changing button click event within subs

Is it possible to change the event of a button click within different subs? For example, have the button take me to the main menu in one sub, but in another sub have the same button take me somewhere else.

Roldy
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

You need to maintain a flag ,and based on the flag value do what ever you want.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

How do you do that?

Roldy
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 
Dim flag As Integer
Private Sub Command1_Click()
If flag = 0 Then
Flag1
Else
Flag2
End If
End Sub
Private Sub Flag1()
MsgBox ("flag 0")
flag = 1
End Sub
Private Sub Flag2()
MsgBox ("flag 1")
flag = 0
End Sub
Vineeth K
Posting Whiz in Training
206 posts since Sep 2009
Reputation Points: 20
Solved Threads: 18
 

Is flag a variable?

Roldy
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Yes it is.

Is not that clear from the post #4 ?

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

I would declare the flag public and then give it a value based on what I need to do. Say for instance your user has added a new record to your database and you now need to take him back to the main form, do this -

'In a module, add the following...

Public flag As Integer

'In your form you will use something like...

Private Sub Command1_Click()

'You are finished with the data add, now give flag a value...
flag = 1
End Sub

Private Sub Command2_Click()

If flag = 1 Then
    Form2.Show
    
    flag = 0
    Unload Me
        Else
    'flag = 0, do something here...
    MsgBox "What do I do now?"
End If
End Sub
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: