Link to home
Start Free TrialLog in
Avatar of FelixKang
FelixKang

asked on

How to tap into events???

Very simple question, How to tap or detect events that's happening on other forms?
For example I want to execute a procedure inside of form A (not in focus) when someone click a button on Form B (in focus).
Should be pretty damn easy but I just can't do it...
Help please.... :)
Avatar of mats_sthlm
mats_sthlm

Hi!

in formA:


Public Sub aSub()
    MsgBox "aSub on frmA"
End Sub


in formB:

Private Sub Command1_Click()
     frmA.aSub
End Sub


I hope this is what you need

/mats
Avatar of FelixKang

ASKER

Sorry Mats, not quite what I'm looking for but thanks heaps for the try. If no one come up with better solution, the points will be yours but at the moment I have to reject this so other can reply to it.

The main reason for this question is this, I have a form that does a search on different databases. Depending on which form it get launched, the search criteria that I passed to the database using SQL query will be different. This form will be a common form for searching, called from different modules at different times.
So when user click search, it pops up this search form, and when the user types the search criteria, it does a search. To create the same search form for different databases is very inefficient, so that's why I am asking whether I can tap into events generated on other forms. All I need is a trigger from the search form to execute the search query. Maybe there is another solution to this?

If you know what you are looking for you can extent Mats' solution a bit.
In the Common search form you have code that selects the function on the form you want to have ..
Something like :
Select Case <expression from the CommonForm like a selector or whatever>
Case "A"
  'Do something at form1
Case "B"
  'Do something at form2
End Select


I see....:) That could work, is there way to find if the form is being called from another form and give me the form the name?
For example
If I launch Form B from Form A is there way to detect (in Form B) that it was lanched from Form A?
Thanks...

Dim withevents myform as form2


sub xxx()
    set myform as new form2
    myform.show
end sub
   
Errr..Mirkwood, I am a bit confused by your answer...
care to explain? Thanks

MirkWood,
Would that work when only one of the forms is active? The other forms would not respond to Events, or do they?

FelixKang : You could include a Public string in each of your forms that you set when calling from the common form.

Like this :
Select Case <expression from the CommonForm like a selector or whatever>
Case "A"
  'Do something at form1
  form1.CalledBy = Me.Caption
Case "B"
  'Do something at form2
  form2.CalledBy = Me.Caption
End Select

If the non active forms can respond to events then is MirkWood's answer best.

Create form1 and form2
place button on form2

-- Add code below to form1
-- Click button

Dim myform2 As Form2
Dim WithEvents myform2Button As CommandButton

private Sub Form_Load()
    Set myform2 = New Form2
    Set myform2Button = myform2.Command1
    myform2.Show , Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Unload myform2
    Unload Form2
    Set myform2 = Nothing
    Set myform2Button = Nothing
End Sub

Private Sub myforms2Button_Click()
End Sub

Private Sub myform2Button_Click()
    MsgBox "Klik"
End Sub

Sorry about the delay guys, Just came back at the office. I don't have Internet at home, so can't see and test the answer.

Mirkwood : I can't get it to work. I click on the button on form 2 but the msgbox command that I put on form1 doesn't respond to the event. Later on I might experiment more on your answer but at the moment deadline is on my back. :) Thanks heaps for the answer..

Phiro: I think I'll use your answer instead, I was hoping for something a bit more sophiscated and efficient solution but my deadline is at the end of this month so I have no time for experimenting. :)) and your solution is more than enough to do the job. Thanks man... :))

Thanks heaps for the answer guys :), U guys are the best

Phiro, please put an answer and the point is yours.

This sucks... my answer works..
Mirkwood: I tried your code and Form 1 (not in focus) does not respond to the event when I click on Form 2 button. At the moment I'm rushed by deadline so I have to go with the solution that works. I will take a look at your solution at other time and find out what I did wrong but at the moment my point goes to the solution that works.
I'm very grateful for your help, but Phiro suggested workable solution so I'm afraid the point goes to him, Sorry... unless if Phiro wants to give up his point, then the point can be yours. I'm not fussy about this and I have no itention of upseting the very person who help me. Phiro, what do U think?


vb5 or vb6?
Quick and dirty always works ... <g>
I tried Mirkwood's solution earlier and found out it did not work because the non-active form does not respond to the events. So I think my solution, dirty as it is, works best. Mirkwood, do you agree or do you have another card up your sleeve, if not I will post the answer.

So explain why it works on my machine.... I use Vb6 / NT
If I could that I would prove I had successfully installed BO2K on your machine ;-)
I know for sure it did not work on my VB5 enviroment. And appearantly it does not work for FelixKang as well. I cannot say why it does not work here and work fine with you.
But we can check..
If you post the frm files of the two forms I'll try to run them on VB5 and VB6, checking whether it has a different working between versions.   I have two boxes running under win98 with VB5 and VB6 here. Paste the code and I'll test.



Hi Mirkwood and FelixKang.

Mirkwood's solution works indeed. It defines a callback to an event in form2 to be processed and catched in form1. The click event is now defined and processed in form1 (The passive form) when you click the button on Form2 (I used the example of Mirkwood.) I was thinking about the other way around and found out that didn't work.
But basicly that is what you wanted FelixKang, an event that is redirected from the active form to a passive one.
The only catch is that you need code on all your other forms to process the events. Other than that it works fine. I think that Mirkwood gets the points on this one. Though I liked my Case Select solution. It's so C... <g>

I'm using VB5 (SP3) on NT4
I tried the code, it does works. Don't know what I did wrong previously. I took me a while to understand what's going on too :)  I noticed a delay in poping up the box. Is it because,VB needs to generate a new form in the memory? If I have a lot of these, will it affect performace and memory consumption?

Anyway..It is just what I expected, Thanks Mirkwood. Thanks HEAPS! for Phiro as well.
Mirkwood please put an answer..
Thanks..

ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial