Connecting Tech Pros Worldwide Forums | Help | Site Map

Outlook/Access connection help

Newbie
 
Join Date: Sep 2009
Posts: 2
#1: Sep 29 '09
Please help! I have an Outlook module opening a connection to Access, writing in some data, and then pulling out some data. But for some reason the code only works the first time and after that you have to close outlook and reopen it for it to work. After you run the code you can not even open the Access database until you close Outlook. I suspected it was a problem with the connection not being closed, or closed correctily, but I have tried every thing I can find on how to clean up at the end with no luck. Here is the code.

The code in my error handler also fires every run if I have it active, but with no error description or anaything. that is not my big concern though.

Expand|Select|Wrap|Line Numbers
  1. Sub GetTickets()
  2.     'On Error GoTo ErrHand:
  3.  
  4.     Dim StrTmp As String
  5.     Dim Filez() As String
  6.     Dim EmlAdd() As String
  7.     Dim TckNum() As String
  8.     Dim TicDir As String
  9.     Dim NumFiles As Long
  10.     Dim i As Long
  11.     Const acAppendData = 2
  12.     Dim AccApp As Access.Application
  13.  
  14.     TicDir = "C:\Documents and Settings\g43692\Desktop\Jeff's Stuff\Ticketing System\"
  15.  
  16.     Set AccApp = CreateObject("Access.Application")
  17.     AccApp.OpenCurrentDatabase TicDir & "Tickets.mdb"
  18.     StrTmp = Dir$(TicDir & "Tickets\*.xml")
  19.  
  20.     Do While Len(StrTmp)                                                 'Get all file names
  21.         NumFiles = NumFiles + 1
  22.         ReDim Preserve Filez(1 To NumFiles)
  23.         Filez(NumFiles) = StrTmp
  24.         StrTmp = Dir$()
  25.         'MsgBox Filez(NumFiles) & NumFiles
  26.     Loop
  27.  
  28.     If (NumFiles > 0) Then
  29.         ReDim EmlAdd(1 To NumFiles)
  30.         ReDim TckNum(1 To NumFiles)
  31.  
  32.         For i = 1 To NumFiles                                            'Import XML for all files into Access
  33.             AccApp.ImportXML DataSource:=TicDir & "Tickets\" & Filez(i), ImportOptions:=acAppendData
  34.             TckNum(i) = DMax("TicketNumber", "Tickets")
  35.             EmlAdd(i) = DMax("Email", "Tickets", "TicketNumber = " & TckNum(i))
  36.             MsgBox "Email: " & EmlAdd(i) & ", Ticket #:" & TckNum(i)
  37.         Next
  38.  
  39.         'Kill (TicDir & "Tickets\" & "*.xml")
  40.     End If
  41.  
  42.     Erase Filez()
  43.     Erase EmlAdd()
  44.     Erase TckNum()
  45.     AccApp.CloseCurrentDatabase
  46.     Set AccApp = Nothing
  47. 'ErrHand:
  48.     'AccApp.CloseCurrentDatabase
  49.     'MsgBox "ERROR: " & Err.Description
  50.     'Exit Sub
  51. End Sub
  52.  
  53.  

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: 2 Weeks Ago

re: Outlook/Access connection help


Have you tried using Debug, and step through it?
Newbie
 
Join Date: Sep 2009
Posts: 2
#3: 2 Weeks Ago

re: Outlook/Access connection help


Thanks for the tip I actually figured it out. I needed to add a line to refresh the current database before DMax would work the second time. Don't know why this would be needed. DMax seems to have a lot of quirks.
Reply

Tags
access, connections, outlook, vba