我有一个Access项目(在Office 2016中),它由几个表和表格组成。另外,我还设计了一种用户登录方法,使用该方法,某些用户必须访问特定的记录,这些记录是我尝试通过以下代码在表单加载事件中设置的。我的表tbPrimary
的一个字段是initial File
,它是附件类型,其他用户用images \ Word document \ Excel文件等填充它。当我尝试用记录集结果填充附件控件时,出现错误438,而其他控件正确填充。 (错误:Me.InitialFile = rs![Initial File]
。)这是代码:
Public Sub Form_Load()
Dim rs As DAO.Recordset ''Requires reference to Microsoft DAO x.x Library
Dim sSQL As String
Dim strSQL As String
Dim nn As Double
sSQL = "SELECT MIN(tbPrimary.[ID]) As mm FROM tbPrimary WHERE Translator IS NULL"
Set db = CurrentDb
Set rs = db.OpenRecordset(sSQL)
If rs.RecordCount > 0 Then
Me.tbSearch1 = rs!mm
Else
Me.tbSearch1 = "N/A"
End If
nn = CDbl(rs!mm)
strSQL = "SELECT * FROM tbPrimary WHERE ID= " & nn & ""
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
Me!ID = rs!ID
Me.tbInitial_Name = rs![Initial Name]
Me.length = rs!length
Me.Level1_Menu = rs![level1 Menu]
Me.Level2_Menu = rs![level2 Menu]
Me.Level3_Menu = rs![level3 Menu]
Me.Type = rs![Type]
Me.Description = rs!Description
Me.tbMiningDate = rs![Mining Date]
Me.Created = rs!Created
Me.InitialFile = rs![Initial File]
Else
Me.tbSearch1 = "N/A"
End If
Me.Translator.SetFocus
End Sub
(任何解决方案? 提前谢谢)
答案 0 :(得分:0)
这是将附件添加到Access DB的通用方法,希望对您有所帮助。
Option Explicit
Sub ExampleAddAccessAttachment()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsattach As DAO.Recordset
Dim fldattach As DAO.Field
Dim filepath As String
filepath = "SOME FILE PATH HERE"
Set db = CurrentDb
'Open a recordset to the table with the attachment
Set rs = db.OpenRecordset("Select * from SOMETABLENAME")
With rs
.AddNew
'The attachment field is a multipart field, so we can treat as a recordset
Set rsattach = .Fields("TheAttachmentFieldName").Value
'Get the fileData Field, this holds the data
Set fldattach = rsattach.Fields("FileData")
'Add a new record to this recordset, you can add multiple
rsattach.AddNew
'Use the load from file method to add a file to the attachment
fldattach.LoadFromFile (filepath)
'Update the recordset with the attachment
rsattach.Update
'Update the parent table recordset
.Update
End With
End Sub