Results 1 to 5 of 5
  1. #1
    cbh35711 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    40

    Uploading xls to OLE Object in table

    Hello fine folks,



    I am trying to upload an xls file as an OLE object. I would need to create a new record for each upload. I'm using the code i found here http://stackoverflow.com/questions/6...file-ms-access

    After enabling the correct reference(MS ActiveX Data Objects 2.5 Library), it compiles fine. When stepping thorugh the code, it causes Access to crash on this line
    Code:
    Set cn = CurrentProject.Connection
    Any idea what i am doing wrong?

    Code:
    Private Sub Import_Click()
    
    
    On Error GoTo ErrHandler
    
    
        sFilePathAndName = "C:\CashFlow.xls"
        'Test to see if the file exists. Exit if it does not.
        If Dir(sFilePathAndName) = "" Then Exit Sub
    
    
        'LoadPicIntoDatabase = True
    
    
        'Create a connection object
        Dim cn As ADODB.Connection
        Set cn = CurrentProject.Connection
    
    
        'Create our other variables
        Dim rs As ADODB.Recordset
        Dim mstream As ADODB.Stream
        Set rs = New ADODB.Recordset
    
    
        'Configure our recordset variable and open only 1 record (if one exists)
        With rs
            .LockType = adLockOptimistic
            .CursorLocation = adUseClient
            .CursorType = adOpenDynamic
            .Open "SELECT TOP 1 * FROM CashFlowFile", cn
        End With
    
    
        'Open our Binary Stream object and load our file into it
        Set mstream = New ADODB.Stream
        mstream.Open
        mstream.Type = adTypeBinary
        mstream.LoadFromFile sFilePathAndName
    
    
        'add a new record and read our binary file into the OLE Field
        rs.AddNew
        rs.Fields("ole_object") = mstream.Read
        rs.Update
    
    
        'Edit: Removed some cleanup code I had inadvertently left here.
    
    
    
    
    Cleanup:
        On Error Resume Next
        rs.Close
        mstream.Close
        Set mstream = Nothing
        Set rs = Nothing
        Set cn = Nothing
    
    
        Exit Sub
    
    
    ErrHandler:
        MsgBox "Error: " & Err.Number & " " & Err.Description
        'LoadPicIntoDatabase = False
        Resume Cleanup
    
    
    End Sub
    Thanks!!

    Chris

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Don't know if will help but I use 'MS ActiveX Data Objects 2.8 Library' in Access 2007.

    I would open recordset like (never used Cursor Location parameter):

    rs.Open "SELECT TOP 1 * FROM CashFlowFile;", cn, adOpenDynamic, adLockOptimistic

    I tested code with these edits and it ran without error.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    cbh35711 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    40
    Thanks June!

    2.8 clearly seems better than 2.5, so i enabled that instead.

    I commented out
    Code:
    Set cn = CurrentProject.Connection
    because it wasn't used anywhere else. When getting to your updated line of code, it throws this error "Error 3001 Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another." I thought this might be due to commenting of the Connection line. After putting it back in, it crashes again. Looking in the locals window, I do not see
    Me.Connection
    as a possibility. Maybe it's not called Connection?

    Does anyone have another way they've tried before to suggest?

    All your help is much appreciated.

    Chris

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    The Set cn = Connection line is needed because you use the cn variable in opening the recordset. Without variable, can do:

    rs.Open "SELECT TOP 1 * FROM CashFlowFile;", CurrentProject.Connection, adOpenDynamic, adLockOptimistic

    Connection is correct and works for me. I cannot replicate your issue. Do you want to provide project for analysis? Follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    cbh35711 is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    40
    Not sure how i missed that one. Before uploading the project, let me put one more thing out there.

    Here's a sub i created from the part i understood and something online

    Code:
    Private Sub Command20_Click()    
       
        'different way to open the record
        Set db = CurrentDb
        Set rs = db.OpenRecordset("CashFlowFile", dbOpenDynaset)
        
    
        sFilePathAndName = "C:\CashFlow.xls"    
        
        Set mstream = New ADODB.Stream
        mstream.Open
        mstream.Type = adTypeBinary
        mstream.LoadFromFile sFilePathAndName
        
        rs.AddNew
        rs.Fields("ole_object") = mstream.Read
        rs.Update
    End Sub
    This creates a new record in the table. Unfortunately instead of saying MS Excel, it says Long Binary Data. It doesn't seem to contain the same functionality it did before. I think it has something to do with mstream.Type = adTypeBinary.

    Thanks June,

    Chris

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Uploading data from email to database?
    By MikeWP318 in forum Forms
    Replies: 1
    Last Post: 11-01-2011, 03:14 PM
  2. Replies: 1
    Last Post: 09-03-2011, 07:01 PM
  3. Replies: 1
    Last Post: 05-19-2011, 10:42 PM
  4. Problem linking subforms, filtering and uploading
    By sameerk0286 in forum Access
    Replies: 1
    Last Post: 12-09-2010, 09:18 AM
  5. Send Object to addresses in table
    By cm-net in forum Access
    Replies: 1
    Last Post: 04-26-2010, 02:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums