Solved

Read Write an OLE object to Access 97

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month6 days, 9 hours left to enroll

636 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