【问题标题】:MSACCESS 2013 - How to execute and display a SELECT SQL from vbaMSACCESS 2013 - 如何从 vba 执行和显示 SELECT SQL
【发布时间】:2017-08-14 21:49:16
【问题描述】:

我一直找不到一个很好的例子来说明如何从 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

【问题讨论】:

标签: ms-access vba


【解决方案1】:

您的数据库连接“CurrentDb”未定义和设置?

别忘了关闭你的连接;)

查看Accessing SQL Database in Excel-VBA了解更多信息。

【讨论】:

  • 呃...不是 Excel VBA,是 Access VBA,所以 currentDb 始终设置,您不需要创建与正在访问的数据库的连接,也不需要关闭它。
猜你喜欢
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多