首页 > 解决方案 > 预期的用户定义类型,而不是项目

问题描述

关于这个错误,我已经浏览了几个关于 SF 的页面。这是我第一次尝试通过 VBA 向 Access 添加记录。这是我的代码:

Option Compare Database

Public Sub Retrieve_SOPS()
' Retrieve SOP files
    'Record starting timer - BEGIN
    Dim StartTime As Double
    StartTime = Timer

    'Set network folder path
    Const FolderPath As String = "\\JACKSONVILLE-DC\Common\SOP's for JV\SOPs Final"

    'Instantiate FSO
    Dim oFSO As Object
    Dim oFolder As Object
    Dim oFiles As Object
    Dim oFile As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(FolderPath)
    Set oFiles = oFolder.Files

    'Instantiate DAO
    Dim db As DAO
    Dim rs As DAO.Recordset
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblSOP", dbOpenDynaset)

    Dim v As Variant

    'Loop through each file in FSO
    For Each oFile In oFiles
        'Remove temporary/hidden files
        If (oFile.Attributes And 2) <> 2 Then
            'Split filename
            v = Split(oFile.Name, "-")

            ' Instantiate Necessary Variables
            Dim file_path As String
            Dim file_id As Integer
            Dim file_title As String
            Dim lang_code As String
            Dim creation_date As String

            file_path = oFile.Path
            file_id = v(2)
            file_title = v(4)
            lang_code = v(5)
            'If dimension in array exists; Remove file extension
            If UBound(v) >= 6 Then
                creation_date = v(6)
            End If

            With rs
                .AddNew

                .Fields("file_path").Value = file_path
                .Fields("file_id").Value = file_id
                .Fields("file_title").Value = file_title
                .Fields("lang_code").Value = lang_code
                If UBound(v) >= 6 Then
                    .Fields("creation_date").Value = creation_date
                End If
            End With
        End If

        'Stop For Loop (TEMP)
        Exit Sub
    Next oFile
End Sub

然后我得到这个错误:“编译错误:预期的用户定义类型,而不是项目”

我正在阅读 Wiley 的“Microsoft Access 2019 Bible”。

我已阅读以下链接,但仍然无法理解我做错了什么:

  1. 编译器错误:未定义用户定义的类型

  2. ms访问编译错误:未定义用户定义类型

标签: vbams-access

解决方案


你错过了更新:

       With rs
            .AddNew
            .Fields("file_path").Value = file_path
            .Fields("file_id").Value = file_id
            .Fields("file_title").Value = file_title
            .Fields("lang_code").Value = lang_code
            If UBound(v) >= 6 Then
                .Fields("creation_date").Value = creation_date
            End If
            .Update
       End With

推荐阅读