Call MSACCESS function from Outlook

I have code to call an Access function from Outlook when a new mail item is added to a specific folder. The Access database is always open when Outlook is open.

In ThisOutlookSession I have

Public WithEvents myOlItems As Outlook.Items

Public Sub Application_Startup()
    Set myOlItems = Application.GetNamespace("Mapi").Folders("Mailbox - Media Center").Folders("Inbox").Folders("Accepted Calls").Items
End Sub

Public Sub myOlItems_ItemAdd(ByVal Item As Object)

Dim acApp As Access.Application
   Set acApp = GetObject(, "Access.Application")
   acApp.Application.Run ("Out_Check")
End Sub

Open in new window


In Access I have

Public Sub Out_Check()
    Call Forms("Enter Calls").Check_Mail_Click
End Sub

Open in new window


When I first test the code it runs fine, but if I go back later and try it I get a  7952 "You have made an illegal function call" in Outlook  at
 
  acApp.Application.Run ("Out_Check")

Open in new window


I can't figure out why it runs OK if I redo it, but then starts giving the error again after a while.
LVL 1
AHECAsked:
Who is Participating?
 
Michael VasilevskySolutions ArchitectCommented:
Have you tried clearing the acApp variable after each time? I.e.

Public Sub myOlItems_ItemAdd(ByVal Item As Object)

   Dim acApp As Access.Application
   Set acApp = GetObject(, "Access.Application")
   acApp.Application.Run ("Out_Check")
   Set acApp = Nothing
End Sub
0
 
AHECAuthor Commented:
Adding the missing line fixed it. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.