【问题标题】:Import multiple XML files with similar structure into Access DB with the same table将多个具有相似结构的 XML 文件导入到具有同一张表的 Access DB 中
【发布时间】:2020-12-06 07:24:47
【问题描述】:

我研究并获得了将多个 XML 文件从本地路径导入 Access 数据库表的代码。

如果桌面太慢而无法导入,我打算添加一个滞后计时器以避免每个 XML 文件之间发生冲突。

Private Sub Command2_Click()

Dim fs
Dim fsFolder
Dim fsFile
Dim i As Double

Set fs = CreateObject("scripting.filesystemobject")
Set fsFolder = fs.getfolder("C:\MyDesiredPath")

For Each fsFile In fsFolder.files
    Debug.Print fsFile.Name
    Application.ImportXML fsFile.Path, acStructureAndData

    i = Timer + 0.5
    While Timer < i
        DoEvents
    Wend
Next fsFile

End Sub

从这段代码中,假设我在共享路径中有 100 个 XML 文件,在导入 XML 文件后会在数据库中产生 100 个表。

我的目标是将所有这 100 个 XML 文件放入一个表中。由于它们具有相同的结构,我认为这是可能的。

【问题讨论】:

  • 使用Application.ImportXML 似乎无法导入现有表。怎么样:使用ImportXML 导入到新表中,然后将该新表中的所有内容复制到现有表中并再次删除新表,对每个文件重复,完成。
  • 感谢您的及时回复。我试试看
  • @Tomalak 我得到了删除表的代码。由于我是 MS Access VBA 的新手,如果您能提供将所有内容从新表复制到现有表的代码,我将不胜感激。
  • 这可以通过一条 SQL 语句来完成。见MSDN: INSERT INTO statement (Microsoft Access SQL)
  • 试试 acAppendData 选项。

标签: xml vba ms-access


【解决方案1】:
Private Sub Import_XML()
    Dim strFile As String       'Filename
    Dim strFileList() As String 'File Array
    Dim intFile As Integer      'File Number
    Dim strPath As String       ' Path to file folder
   
    'strPath = Me![Path]
    strPath = "C:\XML\"
    strFile = Dir(strPath & "*.XML")
    strFile = Dir(strPath & A)      
    
    While strFile <> ""
       'add files to the list
      intFile = intFile + 1
      ReDim Preserve strFileList(1 To intFile)
      strFileList(intFile) = strFile
      strFile = Dir()
    Wend
    
    'see if any files were found
    If intFile = 0 Then
      MsgBox "No files found"
      Exit Sub
    End If
    
    'cycle through the list of files
    For intFile = 1 To UBound(strFileList)
      Application.ImportXML strPath & strFileList(intFile), 2
    Next intFile
    
    MsgBox "Import Completed"
End Sub

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多