我一直无法找到如何从vba执行SELECT SQL并将结果显示为数据表的好例子。 我有31个字段的查询。我试图构建一个表单,允许用户选择他们想要在结果中看到的字段,而不是构建一堆可能永远不会使用的存储查询。 我从小开始...我有一个带有3个选项按钮的表单,我用它来构建我想要显示的字段。 SQL看起来是正确的,我已经能够让SQL在过去运行,但从未将它显示为数据表。 我非常感谢您提供的任何帮助。
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
答案 0 :(得分:-1)