vba access 将多张表合二为一
vba access append multiple tables into one
首先我是编程新手。
问题 我从互联网上的许多示例中构建了以下代码。
数据库名为 "Code Holder" 此时我有一个 table "test" 并进入 table 我想追加与数据库中一样多的 table .
- 所有 tables
的所有列都相同
- "Test" 以外的 table 名称将会改变
目前我所拥有的如下,
代码运行良好,但我似乎无法将每个 table 附加到 "Test" table 中,每个 table 在 SQL 中都是空白字符串
Sub append4()
Dim db As Database
Dim tdf As TableDef
Dim rs As Recordset
Set db = currentdb()
Set rs = db.OpenRecordset("test")
For Each tdf In db.TableDefs
StrSQL = "INSERT INTO " & "test" & " " & _
"SELECT * " & _
"FROM " & "rs!tablename" & " ;"
DoCmd.RunSQL StrSQL
Next tdf
Set db = Nothing
End Sub
我想说我没有设置rs。正确但我不确定。
任何帮助将不胜感激。
谢谢
下午,发帖后我发现了一些真正有用的东西。下面是更新后的 VBA 代码,经过测试它对我有用。
谢谢 Barett,是的,我引用了 table 不正确,但当你盯着某物看得太久时就会发生这种情况。
如有需要,欢迎复制使用
'please note there are a few things that one assumes while using this code
'1 all tables column headers are the same
'2 this was used with Access 2010
Sub testeroony2()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
'you can set this database to other databases if you wanted too
Set db = currentdb
For Each tdf In db.TableDefs
' ignore system and temporary tables
'if you want to use it for your own use then you will need to change "test" that is the main table that gets uploaded too
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*" Or tdf.Name Like "test") Then
'you will need to also change "test" below to the main table you want appended too
StrSQL = "INSERT INTO " & "test" & " " & _
"SELECT * " & _
"FROM " & tdf.Name & " ;"
DoCmd.RunSQL StrSQL
End If
Next
Set tdf = Nothing
Set db = Nothing
End Sub
首先我是编程新手。
问题 我从互联网上的许多示例中构建了以下代码。 数据库名为 "Code Holder" 此时我有一个 table "test" 并进入 table 我想追加与数据库中一样多的 table .
- 所有 tables 的所有列都相同
- "Test" 以外的 table 名称将会改变
目前我所拥有的如下, 代码运行良好,但我似乎无法将每个 table 附加到 "Test" table 中,每个 table 在 SQL 中都是空白字符串
Sub append4()
Dim db As Database
Dim tdf As TableDef
Dim rs As Recordset
Set db = currentdb()
Set rs = db.OpenRecordset("test")
For Each tdf In db.TableDefs
StrSQL = "INSERT INTO " & "test" & " " & _
"SELECT * " & _
"FROM " & "rs!tablename" & " ;"
DoCmd.RunSQL StrSQL
Next tdf
Set db = Nothing
End Sub
我想说我没有设置rs。正确但我不确定。 任何帮助将不胜感激。
谢谢
下午,发帖后我发现了一些真正有用的东西。下面是更新后的 VBA 代码,经过测试它对我有用。
谢谢 Barett,是的,我引用了 table 不正确,但当你盯着某物看得太久时就会发生这种情况。
如有需要,欢迎复制使用
'please note there are a few things that one assumes while using this code
'1 all tables column headers are the same
'2 this was used with Access 2010
Sub testeroony2()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
'you can set this database to other databases if you wanted too
Set db = currentdb
For Each tdf In db.TableDefs
' ignore system and temporary tables
'if you want to use it for your own use then you will need to change "test" that is the main table that gets uploaded too
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*" Or tdf.Name Like "test") Then
'you will need to also change "test" below to the main table you want appended too
StrSQL = "INSERT INTO " & "test" & " " & _
"SELECT * " & _
"FROM " & tdf.Name & " ;"
DoCmd.RunSQL StrSQL
End If
Next
Set tdf = Nothing
Set db = Nothing
End Sub