我一直無法找到一個很好的例子,說明如何從vba執行SELECT SQL並將結果顯示爲數據表。 我有31個字段的查詢。我試圖構建一個允許用戶選擇他們希望在結果中看到的字段的表單,而不是構建一堆可能永遠不會使用的存儲查詢。 我開始小...我有一個窗體建立了3個選項按鈕,我用它來構建我想要顯示的字段。 SQL看起來是正確的,我已經能夠讓SQL在過去運行,但從來沒有將結果顯示爲數據表。 我非常感謝您提供的幫助。MSACCESS 2013 - 如何執行和顯示SELECT SQL從vba
Private Sub btn_RunQuery_Click()
Dim int_build_fields As Integer
Dim str_fields As String
Dim int_length As Integer
Dim rs_query As Recordset
Dim str_SQL As String
Dim str_List As String
str_fields = ""
If opt_Jobname Then
str_fields = str_fields & ", Jobname"
End If
If opt_CycleDate Then
str_fields = str_fields & ", CycleDate"
End If
If opt_EndTime Then
str_fields = str_fields & ", EndTime"
End If
'Remove comma and space from first field
int_length = Len(str_fields)
str_fields = Right(str_fields, (int_length - 2))
str_SQL = " SELECT " & str_fields & " FROM UnionWithJobnamesAndGeneralStats"
MsgBox "str_SQL = " & str_SQL
Set rs_query = CurrentDb.OpenRecordset(str_SQL)
'Not sure how to execute & display the SQL here...
rs_query.Close
Set rs_query = Nothing
MsgBox "Done"
End Sub
[如何通過vba查看訪問表中的記錄集?](https://stackoverflow.com/questions/5182087/how-to-view-a-recordset-in-an-access -VB) – Andre