VBA Excel 和 Access - 将附件添加到 Access 中的特定记录
VBA Excel and Access - Add attachment to certain record in Access
我有一个 Excel 文件,可以将数据读写到 Access。我希望能够向某些记录添加附件。到目前为止,我已经设法编写代码,将附件添加到所需 table 中的新记录。但是,我无法设法将附件添加到某条记录。这是 Excel VBA:
中的代码
Public adodbConnectionString As String
Public adodbConnection As ADODB.Connection
Public adodbRecordset As ADODB.Recordset
Public daoDB As DAO.database
Public daoWS As DAO.Workspace
Public daoRecordset As DAO.Recordset
Public daoRecordset2 As DAO.Recordset
Public daoFields2 As DAO.Field2
Public Function refreshPath() 'this function updates the path to the .accdb-file globally
pathDb = "[...]\Data.accdb"
End Function
Sub exportAttachmentToAccess() ' this function adds the attachment to the table
refreshPath
Dim filePath As String
filePath = SelectFile()
If Len(filePath) = 0 Then
Debug.Assert "No file selected!"
Exit Sub
End If
Set daoWS = DBEngine.Workspaces(0)
Set daoDB = OpenDatabase(pathDb)
Set daoRecordset = daoDB.OpenRecordset("SELECT * FROM N_C_A;", dbOpenDynaset)
daoRecordset.AddNew
Set daoRecordset2 = daoRecordset.Fields("Test1").value 'Test1 is the field name where the attachments are stored
daoRecordset2.AddNew
daoRecordset2.Fields("FileData").LoadFromFile filePath
daoRecordset2.Update
daoRecordset.Update
daoRecordset.Close
Set daoRecordset = Nothing
Set daoDB = Nothing
End Sub
This is the Access table:
如您所见,每次我 运行 宏时,附件都会出现在新记录中。然而,当我改变
daoRecordset.AddNew
至
daoRecordset.Edit
它将附件添加到第一条记录。
如何将附件添加到ID 12,即第四条记录?
在 table 中保存对象会消耗 Access 2GB 大小限制。通常最好将附件留在外部并在文本字段中保存路径。
选项:
- 在 SQL 中应用过滤器以仅打开包含应更新记录的 daoRecordset
daoDB.OpenRecordset("SELECT * FROM N_C_A WHERE ID =" & Me!ID, dbOpenDynaset)
- 使用记录集 FindFirst 方法转到所需的记录
daoRecordset.FindFirst "ID = " & Me!ID
If Not daoRecordset.NoMatch Then
'code to add attachment
End If
我有一个 Excel 文件,可以将数据读写到 Access。我希望能够向某些记录添加附件。到目前为止,我已经设法编写代码,将附件添加到所需 table 中的新记录。但是,我无法设法将附件添加到某条记录。这是 Excel VBA:
中的代码Public adodbConnectionString As String
Public adodbConnection As ADODB.Connection
Public adodbRecordset As ADODB.Recordset
Public daoDB As DAO.database
Public daoWS As DAO.Workspace
Public daoRecordset As DAO.Recordset
Public daoRecordset2 As DAO.Recordset
Public daoFields2 As DAO.Field2
Public Function refreshPath() 'this function updates the path to the .accdb-file globally
pathDb = "[...]\Data.accdb"
End Function
Sub exportAttachmentToAccess() ' this function adds the attachment to the table
refreshPath
Dim filePath As String
filePath = SelectFile()
If Len(filePath) = 0 Then
Debug.Assert "No file selected!"
Exit Sub
End If
Set daoWS = DBEngine.Workspaces(0)
Set daoDB = OpenDatabase(pathDb)
Set daoRecordset = daoDB.OpenRecordset("SELECT * FROM N_C_A;", dbOpenDynaset)
daoRecordset.AddNew
Set daoRecordset2 = daoRecordset.Fields("Test1").value 'Test1 is the field name where the attachments are stored
daoRecordset2.AddNew
daoRecordset2.Fields("FileData").LoadFromFile filePath
daoRecordset2.Update
daoRecordset.Update
daoRecordset.Close
Set daoRecordset = Nothing
Set daoDB = Nothing
End Sub
This is the Access table:
如您所见,每次我 运行 宏时,附件都会出现在新记录中。然而,当我改变 daoRecordset.AddNew 至 daoRecordset.Edit 它将附件添加到第一条记录。
如何将附件添加到ID 12,即第四条记录?
在 table 中保存对象会消耗 Access 2GB 大小限制。通常最好将附件留在外部并在文本字段中保存路径。
选项:
- 在 SQL 中应用过滤器以仅打开包含应更新记录的 daoRecordset
daoDB.OpenRecordset("SELECT * FROM N_C_A WHERE ID =" & Me!ID, dbOpenDynaset)
- 使用记录集 FindFirst 方法转到所需的记录
daoRecordset.FindFirst "ID = " & Me!ID
If Not daoRecordset.NoMatch Then
'code to add attachment
End If