以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  怎么导入多个mdb中的多个表  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=147564)

--  作者:ymli
--  发布时间:2020/3/19 14:50:00
--  怎么导入多个mdb中的多个表
有1000多个mdb,每个mdb中有按年月排列的多个表,现在想重新整理数据,将所有的mdb中的表都导入到指定的表格中,由于有的月份数据不存在,所以导入时总出现运行错误,Microsft Jet 数据库引擎找不到输入表或查询xxx,每次都需要关闭提示后方能运行,请各位大神帮忙看一看怎么解决。此外希望能将整理后的数据按月份生成不同的表。(现在出现运行错误:不存在表名为xxx的DataTable)
代码如下:

Dim dlg As New OpenFileDialog

dlg.Filter = "Access文件|*.mdb"

dlg.MultiSelect = True \'允许选择多个文件

If dlg.ShowDialog =DialogResult.OK Then

    For Each fl As String In dlg.FileNames

        Dim mg As New Merger

        mg.SourcePath = fl

        Dim name As String

        Dim year As Integer

        For year = 2019 To 2019 Step 1

            Dim month As Integer

            For month = 1 To 12 Step 1

                If month < 10 Then

                    name = year & "0" & month & "_Memory"

                    mg.SourceTableName = name

                    mg.DataTableName = name

                    mg.Merge()

                Else If month < 13

                    name = year & month & "_Memory"

                    mg.SourceTableName = name

                    mg.DataTableName = name

                    mg.Merge()

                    Continue For

                End If

            Next

            year = year + 1

        Next

    Next

End If


[此贴子已经被作者于2020/3/19 14:53:42编辑过]

--  作者:有点蓝
--  发布时间:2020/3/19 15:02:00
--  
Dim dlg As New OpenFileDialog
dlg.Filter = "Access文件|*.mdb"
dlg.MultiSelect = True \'允许选择多个文件
If dlg.ShowDialog =DialogResult.OK Then
    For Each fl As String In dlg.FileNames
        Dim name As String
        Dim year As Integer
        For year = 2019 To 2019
            Dim month As Integer
            For month = 1 To 12
                name = year & format(month,"00") & "_Memory"
                If DataTables.Contains(name) Then
                    Dim mg As New Merger
                    mg.SourcePath = fl
                    mg.SourceTableName = name
                    mg.DataTableName = name
                    mg.Merge()
                Else
                    Dim im As New Importer
                    im.SourcePath = fl
                    im.SourceTableName = name
                    im.NewTableName  = name
                    im.Import
                End If
            Next
        Next
        
    Next
    
End If
[此贴子已经被作者于2020/3/19 15:35:00编辑过]

--  作者:ymli
--  发布时间:2020/3/19 15:24:00
--  
编译错误,DataTablename不是Importer的成员。
错误代码:im.DataTableName = name

此外能在整理后的表中增加一列linename =1的语句吗
[此贴子已经被作者于2020/3/19 15:35:53编辑过]

--  作者:有点蓝
--  发布时间:2020/3/19 15:35:00
--  
Dim im As New Importer
im.SourcePath = fl
im.SourceTableName = name
im.NewTableName  = name
im.Import

--  作者:ymli
--  发布时间:2020/3/19 15:42:00
--  
运行错误:SELECT 子句中包含一个保留字、拼写错误或丢失的参数,或标点符号不正确。

能在增加一列linename 吗,所有的值都是1

--  作者:有点蓝
--  发布时间:2020/3/19 15:50:00
--  
1、要导入的数据和已有的表结构不一致
--  作者:ymli
--  发布时间:2020/3/19 15:58:00
--  
一直报运行错误:SELECT 子句中包含一个保留字、拼写错误或丢失的参数,或标点符号不正确。

--  作者:有点蓝
--  发布时间:2020/3/19 16:09:00
--  
要导入的mdb数据库是不是没有这些表?msgbox(name = year & format(month,"00") & "_Memory")
或者是要导入的mdb表的结构和foxtable里的表结构不一致

--  作者:ymli
--  发布时间:2020/3/19 16:46:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:数据.rar

而且mdb表的结构没有什么特别的,还是不知道问题出在哪里,mdb数据参照附件
--  作者:有点蓝
--  发布时间:2020/3/19 17:15:00
--  
代码是遍历12个月的,但是数据库里很多都只有5~12月份的表格,导入不存在的表格当然有问题了。

Dim dlg As New OpenFileDialog
dlg.Filter = "Access文件|*.mdb"
dlg.MultiSelect = True \'允许选择多个文件
Dim lst As List(Of String)
If dlg.ShowDialog =DialogResult.OK Then
    For Each fl As String In dlg.FileNames
        If Connections.Contains("test") Then Connections.Delete("test")
        Connections.Add("test","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fl & ";Persist Security Info=False") \'动态连接数据库
        lst = Connections("test").GetTableNames \'获取数据源里所有的表名
        Dim name As String
        Dim year As Integer
        For year = 2019 To 2019
            Dim month As Integer
            For month = 1 To 12
                name = year & format(month,"00") & "_Memory"
                If lst.Contains(name ) Then \'如果数据库包含这个表
                    If DataTables.Contains(name) Then
                        Dim mg As New Merger
                        mg.SourcePath = fl
                        mg.SourceTableName = name
                        mg.DataTableName = name
                        mg.Merge()
                    Else
                        Dim im As New Importer
                        im.SourcePath = fl
                        im.SourceTableName = name
                        im.NewTableName  = name
                        im.Import
                    End If
                End If
            Next
        Next
    Next
End If