Solved

Read Write an OLE object to Access 97

Posted on 1998-08-04
5
187 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
Comment Utility
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
Comment Utility
set rs.fields("MyFieldName") = OLEWord gives an  "invalid use of property" error
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
Sorry, it was just a thought.
0
 

Author Comment

by:LewisB
Comment Utility
Adjusted points to 200
0
 
LVL 2

Accepted Solution

by:
mtoft earned 200 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now