如何从一个文件中的OLE对象 - MS-访问 [英] how to create an ole object from a file - Ms-access

查看:630
本文介绍了如何从一个文件中的OLE对象 - MS-访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌入的图片(OLE)coulmn表。

I have a table with an embedded picture(OLE) coulmn.

我希望能够通过与浏览选项的形式插入一个新的记录。

I Want to be able to insert a new record via a form with a browse option.

不过,我有一个文件名,我需要把它变成一个OLE对象,并将其插入到表单中。我怎么做,在VBA?

Anyway, I have a file name, And I need to turn it into an ole object and insert it into the form. how do I do that in VBA?

为了澄清 - 我需要打开一个文件名,与该文件中的OLE对象,而不是将其插入到表

谢谢, Fingerman。

Thanks, Fingerman.

编辑:

好吧,正如刚才@HansUp我需要解释一下。 在我的表,我有一个不绑定到一个领域,而是一个查阅函数绑定的OLE对象。我得到正确的ID为通过查询和组合框控制研究 - 这样的控制器绑定到:

Ok, as pointed out by @HansUp I need to explain. In my form, I have a bound OLE object that is not bound to a field, but to a dlookup function. I get the correct id into a controll via a query and a combo box - so the controller is bound to:

=DLookUp("picture","articles","id=" & [articles])

请注意这文章是不是一个领域,但一个控制,我不知道,做任何diffrence。

note that articles is not a field but a controller, I don't know if that does any diffrence.

。每一个控制器改变我用时间 me.recalc ,以便结合OLE可以更新它的价值。

Every time that controller is changed I use me.recalc so that the bound OLE may update it's value.

不管怎样,我以为只是通过VBA和输入文件联系地址,用户要做到这一点,而无需使用控制器,但某种INSERT或财产以后的,但其他的选择会受到欢迎。

Anyway, I thought to do this just by VBA and a user entering a file adress,without using the controller but some kind of INSERT or somthing, but other options would be welcome.

如果我不明白,请教!我会澄清和修正自己。

编辑2:

因此​​,如何在文件名收购或   来源?你希望使用的   条款ArticleID?是画面总是在一个   与预期预期位置   文件名?究竟你想   做,如果你不使用浏览按钮?   您是否正在寻找的东西全   自动化的基于文件夹和文件上   名称或者是你寻找的东西   像拖放?

So how is the filename acquired or derived? Are you expecting to use the ArticleID? Is the picture always at an expected location with an expected file name? What exactly do you want to do if you don't use the Browse button? Are you looking for something fully automated based on the folder and file name or are you looking for something like drag and drop?

文件名是通过浏览选项获得性,我把它implented。为了simplicty的 - 让我们说,用户必须输入文件名自己的文本框。 现在 - 我想这对一个按钮的点击,我可以插入文件名作为嵌入OLE对象 - 到我的数据库。我不是在寻找任何自动化也不拖放(但是,如果拖动和放大器;下降的作品,这将是巨大的)。第一次编辑是有关OLE控制器,因为有人问。他认为我的问题可以用该控制器来解决 - 所以我就如何我desplaying的画面细节。我不认为这有什么关联,但如果有人可以使用,这将是罚款由我。我期待使用条款ArticleID进行更新 - 但同样,我看不出这涉及到这个问题。

the filename is aquired via a browse option, I have it implented. for the sake of simplicty - let's say the user has to enter the file name themselves to a text box. now - I want that on a click of a button, I could insert that file name as an embedded ole object - to my database. I'm not looking for any automation nor drag and drop (But, If drag & drop works, It would be great). The first edit is about the ole controller since someone asked. he thought my question could be resolved using that controller - so I gave details on how I am desplaying the picture. I don't think it has any relevance but If someone could use that, it would be fine by me. I am expecting to use an articleID for the update - but again, I don't see how this relates to the question.

我开始认为这可能是不可能的...... :( 这是unfortunte因为porblem相当stright转发。 您有一个文件名,你需要它embeeded在数据库中的OLE对象。

I am beginning to think this might be not possible..... :( This is unfortunte since the porblem is fairly stright-forward. you have a file-name, you need it embeeded as an OLE object in your database.

推荐答案

在提供我的答案,我将采取快速甘德在改写你的问题,它的要求。这听起来像你对我希望能够加载二进制文件对象,在这种情况下,使用VBA,在表中的OLE对象字段,并绑定对象框。

Before providing my answers, I'm going to take a quick gander at rephrasing your question and it's requirements. It sounds to me like you want to be able to load binary file objects, in this case pictures, using VBA, an OLE Object field in your table, and a Bound Object Frame.

您最好的选择就是停止尝试使用绑定的对象框,因为它有太多的限制。

Your best option is to stop trying to use a bound object frame because it has far too many limitations.

有两种基本方法,建议你正在努力做的事情。

There are basically two recommended methods for what you're trying to do.

1)店铺只有一个链接到图像文件,然后使用图像控制(可绑定到你的图片域)来显示图像。

1) Store only a link to the image file and then use an image control (it can be bound to your picture field) to display the image.

2)存储图像使用code读取二进制数据图像的OLE对象字段。当你需要显示图像,你需要写出来到一个临时文件,然后你可以在图像控件为临时映像文件的完整路径和文件名设置图片属性。这将是由你来管理图像文件作为临时文件。你可以使用Windows的临时目录下,或者你可以在每次需要显示图像时简单地写出来,以相同的文件名。

2) Store the image in an OLE Object field using code to read the image in as binary data. When you need to display the image you'll need to write it out to a temp file and then you can set the Picture property on an Image Control to the full path and filename for the temporary image file. It will be up to you to manage the image file as a temp file. You could use Windows' temp directory or you can simply write out to the same file name every time you need to display an image.

无论这些技术是太困难。有一个真正的好文章来帮助你进一步了解我所说的: HTTP:// WWW .jamiessoftware.tk /用品/ handlingimages.html

Neither of these techniques are overly difficult. There's a really good article here to help you further understand what I'm talking about: http://www.jamiessoftware.tk/articles/handlingimages.html

下面是一个函数读取二进制数据(在这种情况下,您的图片文件),另一个函数写出二进制数据:的 http://www.ammara.com/access_image_faq/read_write_blob.html 这非常适用于书写你的照片了一个临时文件。然后,所有你必须​​做你的图像控制设置Picture属性是你的临时文件的文件路径和名称。

Here's a function to read in binary data (in this case your picture file) and another function to write out binary data: http://www.ammara.com/access_image_faq/read_write_blob.html This works well for writing your picture out to a "temp" file. Then all you have to do is set the Picture property on your image control to be the file path and name of your temp file.

您也可以读取和写入使用ADO Stream对象,再加上一个ADO Recordset对象和ADO连接对象的二进制数据。你必须设置在Access中一个对Microsoft ActiveX数据对象2.8库。

You can also read and write binary data using an ADO Stream object, together with an ADO RecordSet Object and ADO Connection Object. You'll have to set a reference in Access to Microsoft ActiveX Data Objects 2.8 Library.

下面是一些code,以使用ADO图片到数据库:

Here's some code to add pictures to the database using ADO:

Private Function LoadPicIntoDatabase(sFilePathAndName As String) As Boolean
On Error GoTo ErrHandler

    'Test to see if the file exists. Exit if it does not.
    If Dir(sFilePathAndName) = "" Then Exit Function

    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 tblArticles", 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("olepicturefield") = 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 Function

ErrHandler:
    MsgBox "Error: " & Err.Number & " " & Err.Description
    LoadPicIntoDatabase = False
    Resume Cleanup

End Function


Private Sub Command0_Click()
    If IsNull(Me.txtFilePathAndName) = False Then
        If Dir(Me.txtFilePathAndName) <> "" Then
            If LoadPicIntoDatabase(Me.txtFilePathAndName) = True Then
                MsgBox Me.txtFilePathAndName & " was successfully loaded into the database."
            End If
        End If
    End If
End Sub

EDIT1:

根据您的要求,这里的code查找/加载一个给定的物品的照片。为了统一起见,我也改变了我的表和字段名以上,以更好地反映您的项目,以匹配code以下。我测试了这code和它工作正常的我。

As per your request, here's code to lookup/load a picture for a given article. For the sake of consistency, I've also changed my table and field names above to better reflect your project and to match the code below. I tested this code and it worked properly for me.

Private Sub Command1_Click()
    If IsNull(Me.txtArticleID) = False Then
        If DCount("articleid", "tblArticles", "articleid = " & Me.txtArticleID) = 1 Then
            Dim rs As DAO.Recordset, sSQL As String, sTempPicture As String
            sSQL = "SELECT * FROM tblArticles WHERE ArticleID = " & Me.txtArticleID
            Set rs = CurrentDb.OpenRecordset(sSQL)
            If Not (rs.EOF And rs.BOF) Then
                sTempPicture = "C:\MyTempPicture.jpg"
                Call BlobToFile(sTempPicture, rs("olepicturefield"))
                If Dir(sTempPicture) <> "" Then
                    Me.imagecontrol1.Picture = sTempPicture
                End If
            End If
            rs.Close
            Set rs = Nothing
        Else
            MsgBox "Article Not Found"
        End If
    Else
        MsgBox "Please enter an article id"
    End If
End Sub

Private Function BlobToFile(strFile As String, ByRef Field As Object) As Long
    On Error GoTo BlobToFileError

    Dim nFileNum As Integer
    Dim abytData() As Byte
    BlobToFile = 0
    nFileNum = FreeFile
    Open strFile For Binary Access Write As nFileNum
    abytData = Field
    Put #nFileNum, , abytData
    BlobToFile = LOF(nFileNum)

BlobToFileExit:
    If nFileNum > 0 Then Close nFileNum
    Exit Function

BlobToFileError:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
           "Error writing file in BlobToFile"
    BlobToFile = 0
    Resume BlobToFileExit

End Function        

这篇关于如何从一个文件中的OLE对象 - MS-访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆