gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:09:13 26 4
gpt4 key购买 nike

我正在尝试创建一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com