简体   繁体   English

将图像从VBA Excel中的二进制类型上传到图像actvex控件

[英]upload image into image actvex control from binary type in vba excel

Currently now I'm working in VBA Excel and I'm trying to import the image (.png) from the sql server. 目前,我正在使用VBA Excel,并且正在尝试从sql服务器导入图像(.png)。 I have completely done to insert the image into sql server in binary type. 我已经完全完成了将图像以二进制类型插入sql server的操作。 This is the sql query that i have done. 这是我已经完成的SQL查询。

INSERT INTO [DemoDatabase].[dbo].[well_image] ([well_img]) SELECT BulkColumn FROM openrowset ( Bulk 'C:\\Users\\Pictures\\003.png', Single_Blob) as img 插入[DemoDatabase]。[dbo]。[well_image]([well_img])从openrowset中选择BulkColumn(Bulk'C:\\ Users \\ Pictures \\ 003.png',Single_Blob)作为img

Then I'm try to select the fetch the image from the sql server and display it in image activex control but it give me an error. 然后,我尝试选择从sql server提取图像并将其显示在图像Activex控件中,但这给我一个错误。

run time error, object required. 运行时错误,需要对象。 (the error) (错误)

Below I'm provide the code for your overview. 下面,我提供您概述的代码。

Set rs = New ADODB.Recordset
Set stm = New ADODB.Stream

Dim sqlquery As String

sqlquery = "SELECT [well_img] FROM well_image WHERE [img_id] = 2"

rs.Open sqlquery, objConnection, adOpenStatic, adLockOptimistic

stm.Type = adTypeBinary
stm.Open
stm.Write rs("well_img").Value   'write bytes to stream
stm.Position = 0

'Sheet1.OLEObjects("well_img").Object.Picture = stm.Read 'load bytes into image control on form
 Sheet1.well_img.Picture = stm.Read    ***here is the problem

stm.Close
rs.Close
objConnection.Close

when I'm debug, this line is the problem "Sheet1.well_img.Picture = stm.Read". 当我调试时,此行是问题“ Sheet1.well_img.Picture = stm.Read”。 I need your help. 我需要你的帮助。 Please help me and thank u for your consideration. 请帮助我,谢谢您的考虑。

I don't really have a solution for you, but I don't believe you can load a picture this way. 我真的没有适合您的解决方案,但我不相信您可以用这种方式加载图片。 From msdn : 来自msdn

Note At run time, the Picture property can be set to any other object's DragIcon, Icon, Image, or Picture property, or you can assign it the graphic returned by the LoadPicture function. 注意在运行时,可以将Picture属性设置为任何其他对象的DragIcon,Icon,Image或Picture属性,或者可以为它分配LoadPicture函数返回的图形。 The exception to this is the Picture property of the ListImages object, which is a read-only property. ListImages对象的Picture属性是一个例外,它是一个只读属性。

The LoadPicture function takes a filepath as an argument as well. LoadPicture函数也将文件路径作为参数。 This article explains how to store and load images from an Access database . 本文介绍了如何存储和加载Access数据库中的图像 Unfortunately, none of this solves your problem, but I hope it may tell you why it can't be done the way you're trying to do it. 不幸的是,这些都不能解决您的问题,但是我希望它可以告诉您为什么无法以您尝试的方式完成它。

Sorry for the late reply. 这么晚才回复很抱歉。 I have try the solution that all of u given, but still does not work. 我已经尝试了所有u给出的解决方案,但是仍然无法正常工作。 Then I try to use another way to do it. 然后,我尝试使用另一种方式来做到这一点。 Even though this is not answer the question but I just want to share what I'm already done. 即使这不能回答问题,但我只想分享我已经完成的工作。


Now I set the image data type as string/varchar. 现在,我将图像数据类型设置为string / varchar。 This is because I plan to save the image name and the image itself is save to a specific folder that I set. 这是因为我计划保存图像名称,并且图像本身会保存到我设置的特定文件夹中。 This is the way how I'm do it. 这就是我的方式。

The code 编码


Dim imgDir as String, imgName as String
imgDir = "C:\Users\pc\Pictures\img\"
imgName = "test.jpg"

'copy img from source directory to destination directory
Dim destinationImg As String
destinationImg = "C:\Users\pc\Pictures\img\" & imgName

'FileCopy is a method that copy file to specific folder/directory
FileCopy imgDir, destinationImg   

'-----------------
'insert imgName to database
'set up connection
Call serverConn

Dim sqlquery As String
sqlquery = "INSERT INTO well_img ([img_name]) " & _
           "VALUES ('" & imgName & "') "

objConnection.Execute sqlquery

' close connection
objConnection.Close
Set objConnection = Nothing

And this is the way I display the image. 这就是我显示图像的方式。 I use image activeX control. 我使用图像ActiveX控件。

Dim destinationImg As String
destinationImg = "C:\Users\pc\Pictures\img\" & imgName

Sheet2.dwnload_img.Picture = LoadPicture(destinationImg)

That all from me. 这一切都是我的。 Any improvement are welcome and thank u everyone. 欢迎任何改进,谢谢大家。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM