Solved

Read Write an OLE object to Access 97

Posted on 1998-08-04
5
194 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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