提问人:Susan Gottfried 提问时间:10/24/2023 最后编辑:Susan Gottfried 更新时间:10/24/2023 访问量:72
在集合错误中找不到项目 - 现在在第一个查询中仍然发生
Item not found in collection error - still occurring now on the first query
问:
我一直在研究这个问题,从这个小组获得帮助,甚至向人工智能寻求帮助。我发现查询名称不能超过 31 个字符,所以我缩短了它们。我仍然收到“在此集合中找不到项目”,但现在是第一个。querynames 数组中的所有查询拼写正确,并且在手动运行时运行良好。我简直不敢相信这很难调试!
顺便说一句,在我反转数组中的名称之前,我删除了所有太长的名称,它起作用了!但是我注意到我希望它们以不同的顺序出现,所以我只是修改了数组,以便第一个是工作表中的最后一个,等等。
Private Sub ExportToXlsx_Click()
Dim cdb As DAO.Database
Dim qdfs As DAO.QueryDefs
Dim qdf As DAO.QueryDef
Dim excelApp As Object
Dim excelWorkbook As Object
Dim excelWorksheet As Object
Dim xlsxPath As String
Dim querynames() As String
Dim i As Integer
'Set up a string array with the query names
querynames = Split("Samples_BIOSAMPLEINFO_UNION,Samples_WATERSAMPLEINFO,Samples_SEDIMENTSAMPLEINFO,Samples_Descriptions,LANDERINFO-Type3,Extra_PHOTOINFO,Extra_AtSeaTransects,DIVEINFO-Type1,CTDINFO-Type2,Base_LAUNCHSITEINFO,Base_CruiseInfo", ",")
'set the name of the excel file you want to create
xlsxPath = Filepath & Filename & ".xlsx"
'create an instance of the excel application
Set excelApp = CreateObject("Excel.Application")
'create a new Excel workbook
Set excelWorkbook = excelApp.Workbooks.Add
'Open the Access database and the Query Definitions
Set cdb = CurrentDb
Set qdfs = cdb.QueryDefs
'Loop through the queries in the QueryDefs but only those identified in querynames()
'Create each new Excel Worksheet for each
For i = 0 To 10
' reset the QueryDef for each iteration of the loop
Set qdf = qdfs(querynames(i))
Set excelWorksheet = excelWorkbook.Sheets.Add
excelWorksheet.Name = querynames(i)
excelWorksheet.Range("A1").CopyFromRecordset qdf.OpenRecordset
Next i
' save the Excel Workbook
excelWorkbook.SaveAs xlsxPath
'Clean up
excelApp.Quit
Set excelWorksheet = Nothing
Set excelWorkbook = Nothing
Set excelApp = Nothing
Set qdf = Nothing
Set qdfs = Nothing
Set cdb = Nothing
End Sub
你能看出我可能做错了什么吗?
提前致谢!
答: 暂无答案
评论
xlsxPath = Filepath & Filename
Filepath
Filename