使用 MS Access 读取文件

Posted

技术标签:

【中文标题】使用 MS Access 读取文件【英文标题】:Read file using MS Access 【发布时间】:2014-08-05 08:41:05 【问题描述】:

我正在开发 ms access 2013,但发现了一些错误。我正在尝试从文本文件中读取数据,但它显示错误。我到处搜索,但没有复制问题。请帮我解决这个问题。

代码

    Set fs = Application.FileSearch   'Get Error on this line
    With fs
    Debug.Print CABPath
        .LookIn = CABPath
        .SearchSubFolders = True
        .FileName = ConFileNm
        If .Execute() > 0 Then
            For FileNum = 1 To .FoundFiles.Count
             Next
        End If
    End With

错误说明

Run-time error 2455:
You entered an expression that has an invalid reference to the property FileSearch

【问题讨论】:

能否也向我们提供错误描述?还有抛出此错误的 LOC。 是的,请立即检查已编辑的问题 你最后把这个排序了吗? 那么您能否将帮助您的人标记为答案,以便其他人可以从中受益? @PaulFrancis 准确答案不在列表中 【参考方案1】:

Application.FileSearch,自 2007 版本起已停用。所以它不能在 2013 年使用。你有一些替代品,比如 Scripting.FileSystem 对象。本站有一些解释和替代方案:http://www.mrexcel.com/forum/excel-questions/268046-application-filesearch-gone-excel-2007-alternatives.html

希望这会有所帮助!祝你好运。

【讨论】:

【参考方案2】:

还有多种解决方法可以通过 google 找到;

Function GetFiles(MatchString As String, StartDirectory As String, Optional DrillSubfolders As Boolean = False) As Variant

    Dim Results() As Variant

    ReDim Results(0 To 0) As Variant

    CheckFiles Results, MatchString, StartDirectory, DrillSubfolders

    If UBound(Results) > 0 Then
        GetFiles = Results
    Else
        GetFiles = ""
    End If

End Function

Sub CheckFiles(ByRef Results As Variant, MatchString As String, StartDir As String, Drill As Boolean)

    Dim fso As Object
    Dim fld As Object
    Dim sf As Object
    Dim fil As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(StartDir)

    For Each fil In fld.Files
        If LCase(fil.Name) Like LCase(MatchString) Then
            If LBound(Results) > 0 Then
                ReDim Preserve Results(1 To UBound(Results) + 1)
            Else
                ReDim Results(1 To 1)
            End If
            Results(UBound(Results)) = fil.Name
        End If
    Next

    If Drill Then
        For Each sf In fld.SubFolders
            CheckFiles Results, MatchString, sf.Path, Drill
        Next
    End If

    Set fil = Nothing
    Set sf = Nothing
    Set fld = Nothing
    Set fso = Nothing

End Sub

你可以通过这样的方式在你的表单中调用它;

Dim FileList As Variant
    Dim Counter As Long

    FileList = GetFiles("*.jpeg", "c:\folder\subfolder", True)
    ' to NOT look in subfoldres:
    'FileList = GetFiles("*.jpeg", "c:\folder\subfolder", True)

    If IsArray(FileList) Then
        With DoCmd
            .SetWarnings False
            For Counter = LBound(FileList) To UBound(FileList)
                .RunSQL "INSERT INTO [mytable] (FilePath) VALUES ('" & _
                    FileList(Counter) & "')"
            Next
            .SetWarnings True
        End With
    End If

注意:通过 google 找到的代码:http://www.experts-exchange.com/Database/MS_Access/Q_28027899.html

【讨论】:

以上是关于使用 MS Access 读取文件的主要内容,如果未能解决你的问题,请参考以下文章

如何让 MS Access 以特定时间间隔从 .txt 文件中读取?

CodeIgniter框架连接读取MS Access数据库文件

如何在 Spring 批处理中读取 MS Access db(.mdb 文件)并加载到 mysql db

如何通过 Java 从 MS Access 2007 数据库中读取 Unicode 字符?

将 .csv 导入 MS Access。 “AtEndOfStream”没有读取最后一行

如何从 SQL Server 读取 MS Access 数据库以更新一个或多个表列中的数据?