预期的用户定义类型,而不是项目

时间:2019-10-30 16:19:21

标签: vba ms-access

关于此错误,我已经浏览了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圣经”。

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

  1. Compiler Error: User-defined types not defined

  2. ms access compile error: user-defined type not defined

1 个答案:

答案 0 :(得分:1)

您错过了更新:

       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