我在VBA中有一個過程,該過程根據用戶在窗體中的下拉列表(報表名稱和分組標準)中選擇的內容來運行報表。另外,我有一個可以將用戶選擇轉移到Excel的按鈕。 我想要測試是否存在查詢過程的一部分低於:使用VBA檢查查詢是否存在
If Not IsNull(Me.cmbGroup.Value) Or Me.cmbGroup.Value = "" Then
strSQL = "SELECT * FROM qryCrossTotGroup WHERE [Group]='" & Me.cmbGroup.Value & "'"
'MsgBox strSQL
With MyDatabase
.QueryDefs.Delete ("tmpOutQry")
Set MyQueryDef = .CreateQueryDef("tmpOutQry", strSQL)
End With
'Step 3: Open the query
Set MyRecordset = MyDatabase.OpenRecordset(strSQL)
'Step 4: Clear previous contents
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.Workbooks.Add
.Sheets("Sheet1").Select
'Step 5: Copy the recordset to Excel
.ActiveSheet.Range("A2").CopyFromRecordset MyRecordset
'Step 6: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
xlApp.ActiveSheet.Cells(1, i).Value = MyRecordset.Fields(i - 1).Name
Next i
xlApp.Cells.EntireColumn.AutoFit
End With
End If
我想測試,如果查詢「tmpOutQry」以刪除它存在。有人有這樣的代碼行嗎?
你做一個簡單的谷歌搜索[「如果查詢存在,訪問vba測試」](https://www.google.com/search?q=access+vba+test+if+query+exists&oq=access+vba+to+test+if+query+e&aqs = chrome.1.69i57j0l4.8457j0j7&的SourceID =鉻&即= UTF-8)?如果是這樣,你會得到[這個答案](https://bytes.com/topic/access/answers/765384-determine-if-query-x-exists)作爲第一個結果。 –