Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to tap into events???

Posted on 1999-07-16
18
Medium Priority
?
285 Views
Last Modified: 2010-04-30
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.... :)
0
Comment
Question by:FelixKang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 5
  • +1
18 Comments
 

Expert Comment

by:mats_sthlm
ID: 1525171
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
0
 

Author Comment

by:FelixKang
ID: 1525172
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?

0
 
LVL 1

Expert Comment

by:phiro
ID: 1525173
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


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:FelixKang
ID: 1525174
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...

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1525175
Dim withevents myform as form2


sub xxx()
    set myform as new form2
    myform.show
end sub
   
0
 

Author Comment

by:FelixKang
ID: 1525176
Errr..Mirkwood, I am a bit confused by your answer...
care to explain? Thanks

0
 
LVL 1

Expert Comment

by:phiro
ID: 1525177
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.

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1525178
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

0
 

Author Comment

by:FelixKang
ID: 1525179
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.

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1525180
This sucks... my answer works..
0
 

Author Comment

by:FelixKang
ID: 1525181
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?


0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1525182
vb5 or vb6?
0
 
LVL 1

Expert Comment

by:phiro
ID: 1525183
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.

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1525184
So explain why it works on my machine.... I use Vb6 / NT
0
 
LVL 1

Expert Comment

by:phiro
ID: 1525185
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.



0
 
LVL 1

Expert Comment

by:phiro
ID: 1525186
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>

0
 

Author Comment

by:FelixKang
ID: 1525187
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..

0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 240 total points
ID: 1525188
I don't know what's causing the delay. I don't think it is because VB is generating a new form.

Sorry that I whined so much, but I was 100% sure it worked.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question