【问题标题】:run time error 438 when trying to use Openrecordset results尝试使用 Openrecordset 结果时出现运行时错误 438
【发布时间】:2018-12-30 15:00:38
【问题描述】:

我有一个 Access 项目(在 Office 2016 中),其中包含多个表格和表单。我还设计了一种用户登录方法,使用它,一些用户必须访问我尝试通过以下代码在表单加载事件中设置它们的特定记录。我的表tbPrimary 的一个字段是initial File,它是附件类型,其他一些用户用图像\Word 文档\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

(任何解决方案? 提前致谢)

【问题讨论】:

  • 小贴士 1) 使用后始终关闭记录集 2) 无需再次设置相同的对象Set db = CurrentDb 3) 我更喜欢使用If Not (rs.EOF and rs.BOF) 而不是If rs.RecordCount > 0
  • 非常感谢。我从上面的代码中删除了几行以使其最小化。例如,我曾经将 db 设置为空,所以我再次设置它,依此类推。

标签: sql sql-server ms-access vba


【解决方案1】:

这是向 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

【讨论】:

  • 所以它添加了一个附件到数据库。但我的问题是当尝试搜索某个记录并在导致错误的附件控件中显示它的初始文件字段时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2013-06-16
  • 2021-08-24
  • 1970-01-01
  • 2012-02-05
相关资源
最近更新 更多