Solved

Read Write an OLE object to Access 97

Posted on 1998-08-04
5
201 Views
Last Modified: 2012-06-27
i have an Access 97 table with an OLE object (a word 97 doc) in one of the fields.  I want to read that field into an OLE object on my form.  I know how to do it using a data control but I dont want to use data controls.
how do i set my ole object  (OLEWord) to display the data in my recordset and how do i write it back
after I have made changes to it?

eg.
set RS = dbtemp.openrecordset("mydatabase", dbOpenDynaset)
OLEWord= ????  rs.fields("Myfieldname")
...make changes...
rs.fields("Myfieldname") = OLEWord

i know this last part is wrong, but that the idea of what i need to do.
0
Comment
Question by:LewisB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:clifABB
ID: 1467860
I've never done this before, so I'm not sure it will work.
But, as an idea, when you assign an object to a variable, you need to use the Set keyword.  This might work:
Set rs.fields("Myfieldname") = OLEWord

If it does, let me know and I'll repost as an answer.
0
 

Author Comment

by:LewisB
ID: 1467861
set rs.fields("MyFieldName") = OLEWord gives an  "invalid use of property" error
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1467862
Sorry, it was just a thought.
0
 

Author Comment

by:LewisB
ID: 1467863
Adjusted points to 200
0
 
LVL 2

Accepted Solution

by:
mtoft earned 200 total points
ID: 1467864
This solution uses the OLE Controls method "SaveToFile"  and "ReadFromFile".


If you have any trouble, please let me know...

/Mtoft

-------------------------------------------------



Private mDB As Database

Private Const M_DATABASE = "D:\Privat\Experts Exchange Answers\Load & Save OLE\DATA.MDB"
Private Const M_TEMPFILE = "D:\Privat\Experts Exchange Answers\Load & Save OLE\TEMPFILE.DAT"

Private Sub Form_Load()
    ' Open database
    Set mDB = DBEngine.Workspaces(0).OpenDatabase(M_DATABASE)
End Sub

' Simple call the insertobj dialog, to give us some data to work with
Private Sub cmdInsertOle_Click()
    OLE1.InsertObjDlg
End Sub

' Load the first record in our table, and display it in the OLE control
Private Sub cmdLoadFromDB_Click()
    Dim rsData As DAO.Recordset
   
    ' Open the recordset
    Set rsData = mDB.OpenRecordset("SELECT * FROM OLEData;", dbOpenSnapshot)
   
    If rsData.EOF Then
        MsgBox "No oleobjects in database"
    Else
        Dim sOleData As String
        ' Get the data chunk from the recordset
        sOleData = rsData.Fields("OLEData").GetChunk(0, rsData.Fields("OLEData").FieldSize)
       
        ' Get a handle to a free temporary file
        Dim lHandle As Long
        lHandle = FreeFile()
        ' If the oledata file exists, delete it
        If Dir(M_TEMPFILE) <> "" Then Kill M_TEMPFILE
        ' The ole string is saved to a temporary file
        Open M_TEMPFILE For Binary As #lHandle
        ' Put the data
        Put #lHandle, , sOleData
        Close #lHandle
       
        ' Open the temporary file, and give it to the ole object
        lHandle = FreeFile()
        Open M_TEMPFILE For Binary As #lHandle
        ' OLE Reads the file
        OLE1.ReadFromFile lHandle
   
        ' Close the temp file handle
        Close #lHandle

        ' Delete the temp file
        Kill M_TEMPFILE
    End If
   
    ' Clean up the recordset
    rsData.Close
    Set rsData = Nothing
End Sub

' Save our OLE control's data in the first record of our table
Private Sub cmdSaveToDB_Click()
    On Error GoTo ERR_Save
    Dim rsData As DAO.Recordset
   
    ' Kill the temporary file, if it exists
    If Dir(M_TEMPFILE) <> "" Then Kill M_TEMPFILE
   
    ' Get a handle to a temporary file
    Dim lHandle As Long
    lHandle = FreeFile()
    ' The ole string is saved to a temporary file
    Open M_TEMPFILE For Binary As #lHandle
    ' OLE Reads the file
    OLE1.SaveToFile lHandle
    ' Close file
    Close #lHandle
       
    ' Read the temp file into a string variable
    Dim sData As String
    sData = Space(FileLen(M_TEMPFILE))
   
    ' Open new file handle
    lHandle = FreeFile()
    Open M_TEMPFILE For Binary As #lHandle
   
    ' Get data into a temp variable
    Get #lHandle, , sData
    ' Close handle to the temp file
    Close #lHandle
   
    ' We delete everything from the table, to make room for
    ' the new ole blob
    mDB.Execute "DELETE * From OLEData;", dbFailOnError
   
    ' Open the table, no transactions or failsafing in this example
    Set rsData = mDB.OpenRecordset("OLEData", dbOpenTable)
    rsData.AddNew
    rsData("OLEData").AppendChunk sData
    rsData.Update

    ' Delete the temp file
    Kill M_TEMPFILE
   
    ' Clean up the recordset
    rsData.Close
    Set rsData = Nothing
    Exit Sub
ERR_Save:
    MsgBox Err.Description, vbCritical
    Exit Sub
End Sub

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question