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