使用MS Access读取文件

时间:2014-08-05 08:41:06

标签: vba file-io access-vba ms-access-2013

我正在使用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

2 个答案:

答案 0 :(得分:3)

Application.FileSearch,自2007年版以来已停产。所以它在2013年无法使用。您有一些替代方案,如Scripting.FileSystem对象。此网站中有一些解释和替代方案:http://www.mrexcel.com/forum/excel-questions/268046-application-filesearch-gone-excel-2007-alternatives.html

希望这有帮助!祝好运。

答案 1 :(得分:1)

通过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