Solved

Triggering an event with AppActivate

Posted on 2000-03-29
13
646 Views
Last Modified: 2008-02-20
Hi

I am using AppActivate to activate another program from within Access.  When the other program calls AppActivate to return focus to the calling Ap, the calling Ap needs to respond by taking some action.  I have tried to use OnActivate and OnGotFocus, but they are apparently not triggered.  I have to have both programs open at the same time.

Any ideas?

Regards

John
0
Comment
Question by:jdthedj
  • 5
  • 4
  • 4
13 Comments
 
LVL 1

Expert Comment

by:almond_uk
ID: 2669379
Try http://www.mvps.org/access/api/api0034.htm on Dev Avish's Access Web. He has a solution for detecting the application being activated or deactivated.
0
 
LVL 9

Expert Comment

by:BrianWren
ID: 2669754
almond_uk,

It is customary in the Access Topic area to post answers as 'Comments,' instead of proposing them as 'Answers.'  This prevents the question from being locked, encouraging more robust exchanges, and avoids making the person who asked the question reject one answer so that they can accept a different one.  Would you please convert your 'Answer' to a 'Comment?'



jdthedj,

Can you give a sequence of events that you want to have take place?

Like "App 'a' is access, 'b' is Excel.  App a activates App b.  App b then . . . "

That will help to formulate an answer.

Brian
0
 
LVL 1

Expert Comment

by:almond_uk
ID: 2670059
almond_uk changed the proposed answer to a comment
0
 
LVL 3

Author Comment

by:jdthedj
ID: 2670585
Hi almond uk and Brian - thanks for the replies.

almond uk:  I am still evaluating your comment.

Brian:  We have a program written in Delphi which handles all our scanning of documents and their archiving.  I want to be able to look up ID numbers contained in Access tables, but I don't want the overhead of ODBC and another database engine.  I have looked at DAO but the interface is too complicated, and I have also looked at DDE but because the system is used by allcomers I don't want to be called in by the minutes because someone has closed the wrong form or something.

My idea is to have an mdb containing just the relevant tables with a generic query to look up IDs and place the results in a file to be read by the other program, so I want to leave the Access ap open (to save load time) and call it by AppActivating it from the other program.  Access would then AppActivate the other program to let it know the results were ready.

John
0
 
LVL 9

Expert Comment

by:BrianWren
ID: 2670799
So the Delphi program is run by Access, takes some action, closes, and Access continues?

In any event, I would use an external file, with routines in the two applications to read from, and write to this file.  

Delphi can put its request into the file, Access can detect the presence of the request, and use the value to run the query, then put the results of the query into the same file for Delphi to use.

Is that reasonable for your situation?

I think that you are going to have trouble getting the code at Dev's site to work in this case....

Brian
0
 
LVL 3

Author Comment

by:jdthedj
ID: 2670947
Hi Brian

Yes, that is the way I want it to work.  I agree that Dev's code is rather daunting, and it doesn't look very stable judging by the comments of lots of GPFs if you don't get it right.

It's just a matter of Access knowing when to act.  I thought of putting Access into an endless loop looking for the presence of the file, but that would be very sloppy and possibly unreliable.

John
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Expert Comment

by:BrianWren
ID: 2671251
John,

Create a form that will be hidden, and which will stay open, which runs its OnTimer event every 2 seconds or so, as a 'trigger' mechanism.  

So every :02 or so, Access has a look around, checks to see if it needs to take action, etc.  That's not  _too_  messy, and would not tie up the processor at all.

Since there is no real ingress to Access, you must get Access to take action on its own initiative.  The only two way to do that are through the file system, and through memory.  

Memory is very hard to use that way, because memory of one app is protected from other apps.

I'll post something in a minute...

Brian
0
 
LVL 9

Accepted Solution

by:
BrianWren earned 50 total points
ID: 2671316
In a module, put:

________________________________________________________________________________

Option Explicit

Public Const FileName = "C:\Temp\FileName.txt"
Global gLngID As Long

Public Function GetID() As Long
    GetID = gLngID
End Function

________________________________________________________________________________




In the code behind the hidden form, put:
________________________________________________________________________________

Option Compare Database
Option Explicit

Private Sub Form_Load()

    On Error GoTo F_Load_Err
   
    Dim fNum As Integer
   
    If Dir$(FileName) <> "" Then Kill FileName
   
    fNum = FreeFile
    Open FileName For Output As fNum
   
    Print fNum, "<Blank>"
    Close fNum

F_Load_Res:
    Close
    Exit Sub

F_Load_Err:
    ' Probably would be good to put a test here for
    ' trying to delete an open file...
    MsgBox Err.Number & ", " & Err.Description
    Resume F_Load_Res

End Sub

Private Sub Form_Timer()

    On Error GoTo F_Timer_Err
   
    Dim fNum      As Integer
    Dim GetStr7   As String * 7
    Dim GetStrTmp As String
    Dim GetStr    As String
    Dim d As Database
    Dim r As Recordset
   
    fNum = FreeFile
    Open FileName For Input As fNum
   
    Get fNum, , GetStr7
    Close fNum
    If GetStr7 = "<Blank>" Then Exit Sub
   
    ' If we get to here, the file's been changed.  Get the contents.
    fNum = FreeFile
    Open FileName For Input As fNum
    Do Until EOF(fNum)
        Get fNum, , GetStrTmp
        GetStr = GetStr & GetStrTmp
    Loop
    Close fNum
   
    gLngID = Val(GetStr)    ' Store the value from the file so the query can use it.
   
    ' Open the query.  The query would be something like:
    ' SELECT * FROM tblWhatEver WHERE ID = GetID();
    Set d = CurrentDb
    Set r = d.OpenRecordset("qryGetContents")
   
    fNum = FreeFile         ' Print the contents of the filed in the query.
    Open FileName For Output As fNum
   
    Print fNum, r!Contents
    Close fNum
    r.Close                 ' At this point, the file is setup and
                            ' ready for Delphi to use it.
F_Timer_Res:
    Set r = Nothing
    Set d = Nothing
    Close
    Exit Sub

F_Timer_Err:
    ' Probably would be good to put a test here for
    ' trying to delete an open file...
    MsgBox Err.Number & ", " & Err.Description
    Resume F_Timer_Res

End Sub

Private Sub Form_Unload(Cancel As Integer)

    On Error GoTo F_UnLoad_Err

    If Dir$(FileName) <> "" Then Kill FileName

F_UnLoad_Res:
    Close
    Exit Sub

F_UnLoad_Err:
    ' Probably would be good to put a test here for
    ' trying to delete an open file...
    MsgBox Err.Number & ", " & Err.Description
    Resume F_UnLoad_Res

End Sub
________________________________________________________________________________



Set the form's Timer interval to 2000, and the code will run once every two seconds.  Set the form's TimerInterval to 0, and it won't run at all.  So you can, on a button-click or whatever, set the form's timer interval to start this routine, and when you're done, set it back to 0 to get the form to cease and desist.

Brian
0
 
LVL 3

Author Comment

by:jdthedj
ID: 2671326
Thanks Brian - I'll test it and get back to you!

John
0
 
LVL 3

Author Comment

by:jdthedj
ID: 2671607
Thanks Brian!

With one or two slight modifications it works very well.

John
0
 
LVL 1

Expert Comment

by:almond_uk
ID: 2672385
Comment for jdthedj

Excuse me, but my comment was a solution to the problem. If you mistype it, then that's your lookout. Are you saying that correctly cut-and-paste code from that page will not work.

Furthermore, there was no entry specifying that you could modify the other application, in which case, my answer would work, whereas Brian's would not.
0
 
LVL 3

Author Comment

by:jdthedj
ID: 2678459
almond_uk

I cut and pasted the code from the site you mentioned and also picked up the AddressOf code which was required.  As mentioned in the accompanying article the code was prone to GPFs etc, and indeed I did suffer two GPFs and a Fatal Exception error when attempting to use the code.

Therefore I went with Brian's option which works without hassle.

John
0
 
LVL 1

Expert Comment

by:almond_uk
ID: 2680681
Fair enough.

Tim
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now