Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 702
  • Last Modified:

Triggering an event with AppActivate

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
jdthedj
Asked:
jdthedj
  • 5
  • 4
  • 4
1 Solution
 
almond_ukCommented:
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
 
BrianWrenCommented:
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
 
almond_ukCommented:
almond_uk changed the proposed answer to a comment
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
jdthedjAuthor Commented:
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
 
BrianWrenCommented:
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
 
jdthedjAuthor Commented:
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
 
BrianWrenCommented:
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
 
BrianWrenCommented:
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
 
jdthedjAuthor Commented:
Thanks Brian - I'll test it and get back to you!

John
0
 
jdthedjAuthor Commented:
Thanks Brian!

With one or two slight modifications it works very well.

John
0
 
almond_ukCommented:
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
 
jdthedjAuthor Commented:
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
 
almond_ukCommented:
Fair enough.

Tim
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now