Ms access 正在Access中导入包含“0”的文件&引用;在它里面';使用VBA的s名称

Ms access 正在Access中导入包含“0”的文件&引用;在它里面';使用VBA的s名称,ms-access,import,vba,Ms Access,Import,Vba,我正在尝试将csv文件导入access数据库。它是这样写的: “2016年8月23日第卷csv” 当我尝试使用docmd导入此文件时,它会给我一个错误,即无法导入该文件。我发现了一个问题,即它无法将文件识别为csv,因为有多个“.” 但是,我将以需要导入的方式命名文件。有什么办法吗 请帮忙 这是我的密码: fileName = "Vol." & Year(Date) & "." & MonthName(Month(Date), True) & "." &

我正在尝试将csv文件导入access数据库。它是这样写的: “2016年8月23日第卷csv”

当我尝试使用docmd导入此文件时,它会给我一个错误,即无法导入该文件。我发现了一个问题,即它无法将文件识别为csv,因为有多个“.”

但是,我将以需要导入的方式命名文件。有什么办法吗

请帮忙

这是我的密码:

fileName = "Vol." & Year(Date) & "." & MonthName(Month(Date), True) & "." & Day(Date) &  ".csv"

filePath = "C:\Users\House\Desktop\"

DoCmd.TransferText Transfertype:=acImportDelim, TableName:="Table1", fileName:=filePath + fileName, HasFieldNames:=True

好的,这应该对你有用

我在我的系统上进行了测试,并确认您的DoCmd调用可能使用了旧的MS ACcess代码基要求,并且在“.”和文件名中的任何其他特殊字符上出现了故障-可能是为了找出扩展名而对文件名中的第一个点进行了硬编码

解决方案是使用短文件名

将其添加到模块顶部

' Borrowed code from https://support.microsoft.com/en-us/kb/175512

#If VBA7 Then
    Declare PtrSafe Function GetShortPathName Lib "kernel32" _
      Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
      ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
#Else
    Declare Function GetShortPathName Lib "kernel32" _
      Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
      ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
#End If

Public Function GetShortName(ByVal sLongFileName As String) As String
    Dim lRetVal As Long, sShortPathName As String, iLen As Integer
    'Set up buffer area for API function call return
    sShortPathName = Space(255)
    iLen = Len(sShortPathName)

    'Call the function
    lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
    'Strip away unwanted characters.
    GetShortName = Left(sShortPathName, lRetVal)
End Function
然后修改代码以将文件名转换为短版本

Dim filename As String
Dim filePath As String

Dim csvFile As String

filename = "Vol." & Year(Date) & "." & MonthName(Month(Date), True) & "." & Day(Date) & ".csv"

filePath = "C:\Users\House\Desktop\"

' Convert to Short Filename to work with old MS-Access code base
csvFile = GetShortName(filePath & filename)
Debug.Print csvFile

DoCmd.TransferText Transfertype:=acImportDelim, TableName:="Table1", filename:=csvFile, HasFieldNames:=True

尝试一下下面的解决方案

我没有尝试处理文件名中存在的句点,而是编写了一个脚本来临时重命名文件名。基本上,我将
替换为
,以避开进口问题

Option Explicit

Public Sub ImportTableWithPeriod()
    Dim fso             As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim f               As Object
    Dim FolderName      As String
    Dim Filename        As String
    Dim TempFilepath    As String
    Dim OrigFilePath    As String
    Dim TempFileName    As String
    Dim tempFolderPath  As String

    Filename = "Vol." & Format(Now(), "yyyy") & "." & _
                Format(Now(), "MMM") & "." & _
                Format(Now(), "d") & ".csv"

    FolderName = "C:\Users\House\Desktop\"

    'Build string for replacement names
    'Basically I've replaced the periods with underscores
    TempFileName = Replace(Left(Filename, Len(Filename) - 4), ".", "-") & ".csv"
    TempFilepath = FolderName & TempFileName

    OrigFilePath = FolderName & Filename
    Set f = fso.getFile(OrigFilePath)

    'rename the file temporarily
    f.Name = TempFileName

    'Import the file with the renamed instance
    DoCmd.TransferText Transfertype:=acImportDelim, TableName:="Table1", Filename:=TempFilepath, HasFieldNames:=True

    'Put the name back as it was
    f.Name = Filename

    'Clean up
    set fso = nothing
    set f = nothing
End Sub

你能分享你正在使用的代码吗?\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,并使用代码格式。刚刚更新了问题中的代码!很抱歉,如果您取消注释
&.csv“
部分,它会工作吗?这很好。我对你的答案投了赞成票,因为这是最干净的答案,不涉及文件重命名或临时文件。谢谢!