ms-access - 创建查询时 MS Access 堆栈空间不足

标签 ms-access vba

我正在尝试创建一个 Access 数据库,它将多个结构相同的数据库链接在一起。这些其他数据库是从 3D 模型导出的,它们具有相同的表,但每个数据库中的数据不同。我需要做的是从所有数据库中报告,就像它们是一个大数据库一样。我认为我将如何处理这一问题,即创建查询,将每个数据库中的各个相同表合并为一个查询,然后我可以在所有其他报告中使用该查询。我编写的用于将表连接在一起的代码如下。我遇到的麻烦是,这段代码在“Set QueryDef...”行上给了我一个堆栈空间不足的错误。有人可以告诉我我做错了什么吗?

Public Sub CreateUnionQueries()
'On Error GoTo Err_CreateUnionQueries
Dim QueryRs As DAO.Recordset
Dim TableRs As DAO.Recordset
Dim QueryDef As DAO.QueryDef
Dim SQLText As String
Dim qry As QueryDef

'Get list of all Foreign Table Names
Set QueryRs = CurrentDb.OpenRecordset("select distinct ForeignName from msysobjects where ForeignName is not null")

'Loop over list to create union queries
If QueryRs.RecordCount <> 0 Then
    Do While Not QueryRs.EOF
        Set TableRs = CurrentDb.OpenRecordset("select Name from msysobjects where ForeignName = """ & QueryRs![ForeignName] & """")
        Do While Not TableRs.EOF
            SQLText = SQLText & "select * from " & TableRs![Name]
            TableRs.MoveNext
            If Not TableRs.EOF Then
                SQLText = SQLText & " UNION ALL "
            End If
        Loop
        'Create union query
        For Each qry In CurrentDb.QueryDefs
            If qry.Name = "Q-" & QueryRs![ForeignName] Then
               DoCmd.DeleteObject acQuery, "Q-" & QueryRs![ForeignName]
            End If
        Next qry
        Set QueryDef = CurrentDb.CreateQueryDef("Q-" & QueryRs![ForeignName], SQLText)
        QueryDef.Close
        Set QueryDef = Nothing
        QueryRs.MoveNext
        TableRs.Close
        Set TableRs = Nothing
    Loop
Else
    MsgBox "No files are linked currently"
End If

QueryRs.Close

Err_CreateUnionQueries:
    MsgBox "We have an error"
    Set QueryRs = Nothing
    Set TableRs = Nothing
    Exit Sub
End Sub

最佳答案

天哪,我是个白痴。发现问题了。当我循环时,我没有将 SQLText 设置回空,因此它将对表组的查询附加到最后一个。删除了它,现在它按预期工作了。谢谢大家的帮助。

关于ms-access - 创建查询时 MS Access 堆栈空间不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589508/

相关文章:

excel - 按列名查找列并保留该列的特定值并删除所有其他数据,包括空白

c# - 如何在 C# 中比较 2 个不同 MS Access 数据库中的 2 个相似表?

forms - 在 Access 中创建 'calendar matrix'

excel - 有没有办法在vba中编辑公式

excel - 需要将行剪切并粘贴到上一行的末尾

excel - 在 Excel 单元格中插入图像

PHP导入数据完整性

c# - From 子句 ms Access 中的语法错误

database - Microsoft Access 2003 数据库可以存储多少条记录?

excel - 如果值相等则合并某一特定列的单元格