-1
我迷失了這個話題。我會試着解釋我想要做的事情: - 我的想法是能夠上傳或保存jpg文件到我的access 2010數據庫,這個過程是通過在excel中使用宏來啓動的。我在互聯網上閱讀了很多,但老實說我被卡住了,我無法找到一個例子。 我想使用ADO連接 我的想法是使用帖子標籤,這個標籤會有所不同,我的意思是,我想用這些圖片打印這些標籤。Excel宏保存jpg作爲附件
下面你可以看到我想要做什麼。我迷路了,我得到了錯誤,也許如果有人有一個例子我可以適應它,因爲我認爲我無法使用我擁有的那個。
的過程如下:
Sub SUBIRIMAGEN() 'To save a file in a table as binary
Dim adoStream As Object
Dim adoCmd As Object
Dim strFilePath As String
Dim adoCon As Object
Const strServerName As String = "" 'Server Name
Set adoCon = CreateObject("ADODB.Connection")
Set adoStream = CreateObject("ADODB.Stream")
Set adoCmd = CreateObject("ADODB.Command")
strDBName = "database1.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
'Connect to a data source:
'For pre - MS Access 2007, .mdb files (viz. MS Access 97 up to MS Access 2003), use the Jet provider: "Microsoft.Jet.OLEDB.4.0". For Access 2007 (.accdb database) use the ACE Provider: "Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the Access .mdb & .accdb files.
'--Open Connection to SQL server
adoCon.CursorLocation = adUseClient
adoCon.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB
Rem adoCon.Open "Provider=SQLOLEDB;Data Source=" & strServerName & ";Initial Catalog = " & strDB & ";Integrated Security=SSPI;"
'----
strFilePath = "C:\Users\pc2\Downloads\frutossecosgranel.JPG" ' File to upload
adoStream.Type = adTypeBinary
adoStream.Open
adoStream.LoadFromFile strFilePath 'It fails if file is open
With adoCmd
.CommandText = "INSERT INTO table1 (id,attach) VALUES (?,?) " ' Query
.CommandType = adCmdText
'---adding parameters
.Parameters.Append .CreateParameter("@Id", adInteger, adParamInput, 0, 1)
.Parameters.Append .CreateParameter("@attach", adVarBinary, adParamInput, adoStream.Size, adoStream.Read)
'---
End With
adoCmd.ActiveConnection = adoCon
adoCmd.Execute
adoCon.Close
End Sub