将多个具有类似结构的XML文件导入具有相同表的Access DB
我研究并获得了从本地路径导入多个XML文件以访问数据库表的代码。我打算添加滞后计时器,以避免在桌面太慢而无法导入XML文件时,每个XML文件之间发生共谋将多个具有类似结构的XML文件导入具有相同表的Access DB,xml,vba,ms-access,Xml,Vba,Ms Access,我研究并获得了从本地路径导入多个XML文件以访问数据库表的代码。我打算添加滞后计时器,以避免在桌面太慢而无法导入XML文件时,每个XML文件之间发生共谋 Private Sub Command2_Click() Dim fs Dim fsFolder Dim fsFile Dim i As Double Set fs = CreateObject("scripting.filesystemobject") Set fsFolder = fs.getfolder("
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
Private子命令2_Click()
暗fs
模糊文件夹
Dim fsFile
我是双人的
设置fs=CreateObject(“scripting.filesystemobject”)
设置fsFolder=fs.getfolder(“C:\MyDesiredPath”)
对于fsFolder.files中的每个fsFile
Debug.Print fsFile.Name
Application.ImportXML fsFile.Path、acStructureAndData
i=定时器+0.5
当定时器
根据这段代码,假设共享路径中有100个XML文件,那么在导入XML文件后,将在数据库中生成100个表。但是,我的目标是将所有这100个XML文件放在一个表中。由于它们具有相同的结构,我认为这是可能的。有人能帮我找到正确的密码吗
非常感谢你 Private子导入_XML()
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
作为字符串“文件名”的Dim strFile
Dim strFileList()作为字符串文件数组
Dim intFile作为“整数”文件号
将strPath设置为字符串的文件文件夹路径
“strPath=我![路径]
strPath=“C:\XML\”
strFile=Dir(strPath&“*.XML”)
strFile=Dir(strPath&A)
而strFile“”
'将文件添加到列表中
intFile=intFile+1
ReDim保留strFileList(1到intFile)
strFileList(intFile)=strFile
strFile=Dir()
温德
'查看是否找到任何文件
如果intFile=0,则
MsgBox“未找到任何文件”
出口接头
如果结束
'循环浏览文件列表
对于intFile=1到UBound(strFileList)
Application.ImportXML strPath和strFileList(intFile),2
下一个intFile
MsgBox“导入已完成”
端接头
使用Application.ImportXML
似乎无法导入到现有表中。如何:使用ImportXML
导入到新表中,然后将新表中的所有内容复制到现有表中,并再次删除新表,对每个文件重复,完成。感谢您的及时回复。我会给它一个try@Tomalak我得到了删除表的代码。由于我是MS Access VBA的新手,如果您能提供代码将新表中的所有内容复制到现有表中,我将不胜感激。这可以通过一条SQL语句完成。请参阅Try acAppendData选项。如何写出好的答案: