Results 1 to 9 of 9

Thread: Random Import/Export From Access Fatal Error

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Random Import/Export From Access Fatal Error

    I don't remember specifically changing any code that dealt with the importing and exporting of Access DB's into my Excel Project, but now when I try to import I get the following error which completely crashes Excel...

    Run-time error "-2147417848"

    Automation error: The object invoked has disconnected from its clients.

    Anyone have any idea why this would be thrown? I'm at a complete loss..

    Thanks,

    Pg

  2. #2

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Random Import/Export From Access Fatal Error

    This is one example of an import I am using... this code has been working for me for a long time and now is throwing this automation error exception..

    The strange thing is that in an older version of the workbook I am using (which happens to be located in a different directory) all this code works fine... I am trying to retrace all my steps to see what I have changed (which is a lot) from the older workbook to my current one...

    vb Code:
    1. Public Sub importEvents()
    2. MsgBox ("Please select a valid userDB")
    3. newFN = Application.GetOpenFilename(filefilter:="Access Files (*.mdb), *.mdb", Title:=vNum)
    4. If newFN = False Then
    5.     MsgBox ("No file selected")
    6.     Exit Sub
    7. End If
    8.  
    9. If checkTableExists("EVENTS", newFN) = False Then
    10.     MsgBox ("No Events exists in that database")
    11.     Exit Sub
    12. End If
    13.  
    14. For Each shtnext In Sheets
    15.    If shtnext.name = "EVENTS" Then 'Search/Delete charts w/ same name
    16.         Application.DisplayAlerts = False 'No delete prompt
    17.         Sheets("EVENTS").Delete
    18.         Application.DisplayAlerts = True
    19.    End If
    20. Next shtnext
    21.  
    22. Application.ScreenUpdating = False
    23. Sheets.Add
    24. ActiveSheet.name = "EVENTS"
    25. Application.DisplayAlerts = False
    26. Set dbs = OpenDatabase(newFN)
    27. Cells.Select
    28. Selection.Clear
    29. [A1].Select
    30.  
    31. Set rst = dbs.OpenRecordset("EVENTS", dbOpenDynaset, dbReadOnly)
    32. For Each fld In rst.Fields
    33.     i = i + 1
    34.     ActiveSheet.Cells(1, i).Value = fld.name
    35. Next fld
    36.  
    37. ActiveSheet.Cells(2, 1).CopyFromRecordset rst
    38. ActiveSheet.UsedRange.Columns.AutoFit
    39. ActiveSheet.Cells.CreateNames Top:=True, Left:=False, Right:=False, bottom:=False
    40. dbs.Close
    41. Application.ScreenUpdating = True
    42. Application.DisplayAlerts = True
    43. End Sub

  3. #3

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Random Import/Export From Access Fatal Error

    Also to add on...

    The import works the first time... but fails the second time around.

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Random Import/Export From Access Fatal Error

    I am not sure but may be memory leaks, some improvement suggestions:

    After line 18, add: Exit For : after delete the sheet you don't need to loop again.

    After line 37, add:
    rst.Close
    Set rst = Nothing


    After line 40, add:
    Set dbs = Nothing

    Question: How you do checkTableExists("EVENTS", newFN) before you open the database? Can you show that function?
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Random Import/Export From Access Fatal Error

    let me try making those changes.. thanks for the help anhn...

    Here is the checktableexists function: doesn't actually open the database.. just checks to make sure that table is named in that database.

    vb Code:
    1. Function checkTableExists(tableName As String, dbSource As Variant) As Boolean
    2. Dim db As Database
    3. Dim td As TableDef
    4.  
    5. Set db = OpenDatabase(dbSource)
    6. For Each td In db.TableDefs
    7.     If UCase(tableName) = UCase(td.name) Then
    8.         checkTableExists = True
    9.         Set db = Nothing
    10.         Exit Function
    11.     End If
    12. Next td
    13.  
    14. Set db = Nothing
    15. checkTableExists = False
    16.  
    17. End Function

  6. #6

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Random Import/Export From Access Fatal Error

    made those changes.. crashing still occurs after second execution of said code...

    Although I did change checkTableExists to

    vb Code:
    1. Function checkTableExists(tableName As String, dbSource As Variant) As Boolean
    2. Dim db As Database
    3. Dim td As TableDef
    4.  
    5. Set db = OpenDatabase(dbSource)
    6. For Each td In db.TableDefs
    7.     If UCase(tableName) = UCase(td.name) Then
    8.         Set db = Nothing
    9.         checkTableExists = True
    10.         Exit Function
    11.     End If
    12. Next td
    13.  
    14. Set db = Nothing
    15. checkTableExists = False
    16.  
    17. End Function

  7. #7

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Random Import/Export From Access Fatal Error

    would having option explicit off matter at all?

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Random Import/Export From Access Fatal Error

    Quote Originally Posted by pgag45
    Here is the checktableexists function: doesn't actually open the database.. just checks to make sure that table is named in that database.
    You actually Open the database in checktableexists() function but you never Close it.

    After Set db = Nothing, the database is still opened without any reference.
    With recordsets and databases, after finishing using them, you should remember :

    rst.Close
    Set rst = Nothing

    dbs.Close
    Set dbs = Nothing


    Closing is more critical than setting to Nothing.

    Why do you need to call a separate function to check whether a table exist and you have to Open the database twice if the table exist?

    You can combine the code of that function in to the Sub.
    or:
    Past back the database to the Sub that you won't need to open them twice and closing them twice.

    Quote Originally Posted by pgag45
    would having option explicit off matter at all?
    Without Option Explicit doesn't matter with your problem here.
    But I recommend you to use Option Explicit at all time, it will save you a lot of headache, particular when you have wrong spelling of variables and usually you can have dropdown list of properties and methods for objects/variables.

    Below is the much cleanner code for you:
    Code:
    Option Explicit
    
    Public Sub importEvents()
       Dim dbs As Database
       Dim rst As Recordset
       Dim fld As Field
       Dim ws As Worksheet
       Dim i As Integer
       Dim newFN
       Dim sTitle As String
    
       sTitle = "Please select a valid user mdb file"
       newFN = Application.GetOpenFilename( _
               FileFilter:="Access Files (*.mdb), *.mdb", Title:=sTitle)
       If newFN = False Then
          MsgBox ("No file selected")
          Exit Sub
       End If
       
       Set dbs = OpenDatabase(newFN)
       
       On Error Resume Next
       '-- Try to open "EVENTS" table
       '-- if it does not exist, an Error will happen.
       Set rst = dbs.OpenRecordset("EVENTS", dbOpenDynaset, dbReadOnly)
       If Err > 0 Then
          '-- suppose no other error can happen
          MsgBox ("No Events exists in that database")
          '-- Close dbs befor exit sub
          dbs.Close
          Set dbs = Nothing
          Exit Sub
       End If
       
       Application.ScreenUpdating = False
       '-- Try to set the worksheet "EVENTS"
       '-- if it exists, no Error will happen: Delete it
       '-- otherwise Clear the error and go on
       Set ws = ThisWorkbook.Worksheets("EVENTS")
       If Err = 0 Then
          Application.DisplayAlerts = False 'No delete prompt
          ws.Delete
          Application.DisplayAlerts = True
       Else
          Err.Clear
       End If
       On Error GoTo 0
       
       '-- Use a Worksheet variable is much better than rely on ActiveSheet
       Set ws = ThisWorkbook.Worksheets.Add
       ws.Name = "EVENTS"
       
       i = 0
       For Each fld In rst.Fields
          i = i + 1
          ws.Cells(1, i) = fld.Name
       Next
       ws.Cells(2, 1).CopyFromRecordset rst
       '-- No longer use rst and dbs: Close them
       rst.Close
       Set rst = Nothing
       dbs.Close
       Set dbs = Nothing
       
       ws.UsedRange.Columns.AutoFit
       '-- CreateNames for UsedRange instead of the whole sheet
       ws.UsedRange.CreateNames Top:=True, Left:=False, Right:=False, Bottom:=False
       
       ws.Select
       ws.Cells(1, 1).Select
       Set ws = Nothing
       
       Application.ScreenUpdating = True
       
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Random Import/Export From Access Fatal Error

    hmm.. I appreciate the cleaner code.. Definitely better and more reliable code... but this crash problem persists even with those changes..

    I'm starting to think this project is corrupted and I'm going to have to revert back to my older version

    thanks for the help though

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width