MS Access 在数据表中显示 vba select 查询
MS Access displaying vba select query in datasheet
正在学习使用 SQL inside ms access vba。我了解了 select 查询的工作原理,但它们不会像通过访问表单构建的那样立即输出到数据表。我需要复杂的 if else 链来获得我想要的东西,所以我不得不使用 vba 路由。
我已经尝试了其他问题中建议的几种方法,但它们并没有像我想要的那样工作。
我正在为一个按钮创建代码,该按钮显示包含基于复选框的特定元素的数据表。下面的代码将只是显示 Select 查询的元素。
我尝试了 .QueryDef 方法
Dim qd As QueryDef
Set qd = CurrentDb.CreateQueryDef("")
With qd
.ReturnsRecords = True
.sql = "SELECT * FROM EXPORT_CERTIFICATION WHERE EXPORT_CERTIFICATION.CertificationStatus = 'Certified'"
End With
这有效,但只有当我输入查询名称时才有效,而不是在空的时候。它创建了一个新查询来执行我想要的操作,但此后按钮没有执行任何操作。
我是否应该创建一个 table 来接受输出并尝试将 table 设置为由 sql 语句创建的记录集?我想避免使用另一个 table,因为它仅用于查看。
这是我目前尝试的更简单解决方案的完整代码
Private Sub NDC_CERT_VIEW_Click()
Dim StrSQLclause As String
Dim db1 As DAO.Database, qry1 As DAO.QueryDef
Set db1 = CurrentDb()
Set qry1 = db1.QueryDefs("NDC_EXPORT_VIEW")
MsgBox ("Here")
If (Certified_Check And Not Revised_Check And Not Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' "
ElseIf (Not Certified_Check And Revised_Check And Not Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Not Certified_Check And Not Revised_Check And Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null "
ElseIf (Not Certified_Check And Not Revised_Check And Not Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
ElseIf (Certified_Check And Revised_Check And Not Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Not Certified_Check And Revised_Check And Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Not Certified_Check And Not Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
ElseIf (Certified_Check And Not Revised_Check And Not Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
ElseIf (Not Certified_Check And Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null "
ElseIf (Certified_Check And Not Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null "
ElseIf (Certified_Check And Revised_Check And Not Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Certified_Check And Revised_Check And Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Certified_Check And Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
Else
MsgBox ("No Status Selected")
Exit Sub
End If
MsgBox (StrSQLclause)
MsgBox ("Here3")
qry1.sql = StrSQLclause
MsgBox ("Here4")
DoCmd.OpenQuery "NDC_EXPORT_VIEW"
MsgBox ("Here6")
我建议您使用预建的表单和查询来达到您要在此处实现的目的。
但是,话虽如此,您正在学习在 VBA 中使用 SQL,所以这里是使用代码的方法(其他方法更有可能):
- 创建一个基本表单并为每个表单添加正确数量的控件
记录。
- 为表格设置
Has Module
属性(在其他
选项卡)到 是 。
- 将
Default View
设置为 DataSheet
。
- 将表格另存为
MyForm
。
添加一个VBA代码模块并添加此代码:
Sub Test()
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim frm As Form_MyForm
Set qd = CurrentDb.CreateQueryDef("", "PARAMETERS Stats Text(255); " & _
"SELECT * FROM EXPORT_CERTIFICATION " & _
"WHERE CertificationStatus = Stats")
qd.Parameters("Stats") = "Certified"
Set rs = qd.OpenRecordset
Set frm = New Form_MyForm
'The record source for the form
Set frm.Recordset = rs
'The record source fields attached to each control.
frm.Text0 = "FieldA"
frm.Text2 = "Field"
frm.Text3 = "CertificationStatus"
frm.Visible = True
Debug.Assert False 'Form will disappear when code ends, so pause here.
End Sub
将出现一个包含您的记录集的表单。
就我个人而言,我使用以下代码来显示记录集。
像 Darren 的回答一样,我创建了一个表单,我将其命名为 frmDynDS,默认视图设置为数据表视图,并且我使用以下代码向其添加了 255 个控件 (运行当表单处于设计视图时):
Dim i As Long
Dim myCtl As Control
For i = 0 To 254
Set myCtl = Application.CreateControl("frmDynDS", acTextBox, acDetail)
myCtl.Name = "Text" & i
Next i
然后,我将以下代码添加到表单的模块中:
Public Myself As Object
Public Sub LoadRS(myRS As Object)
'Supports both ADODB and DAO recordsets
Dim i As Long
Dim myTextbox As textbox
Dim fld As Object
i = 0
With myRS
For Each fld In myRS.Fields
Set myTextbox = Me.Controls("Text" & i)
myTextbox.Properties("DatasheetCaption").Value = fld.Name
myTextbox.ControlSource = fld.Name
myTextbox.ColumnHidden = False
myTextbox.columnWidth = -2
i = i + 1
Next fld
End With
For i = i To 254
Set myTextbox = Me.Controls("Text" & i)
myTextbox.ColumnHidden = True
Next i
Set Me.Recordset = myRS
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set Myself = Nothing 'Prevent memory leak
End Sub
然后,我在 public 模块中得到以下代码:"
Public Sub DisplayRS(rs As Object)
Dim f As New Form_frmDynDS
f.LoadRS rs
f.Visible = True
Set f.Myself = f
End Sub
完成所有这些设置后,显示记录集就非常简单了。只需执行以下操作:
DisplayRS CurrentDb.OpenRecordset("SELECT * FROM EXPORT_CERTIFICATION WHERE EXPORT_CERTIFICATION.CertificationStatus = 'Certified'")
这将打开表单,使适当数量的控件可见,设置标题,调整单元格宽度以容纳标题,然后将控件绑定到记录集。窗体将一直存在直到关闭,您可以使用此代码同时打开多个记录集。
请注意,当 运行 执行此代码时,您不能在记录集中使用参数,因为它会在 filtering/sorting.
时崩溃
正在学习使用 SQL inside ms access vba。我了解了 select 查询的工作原理,但它们不会像通过访问表单构建的那样立即输出到数据表。我需要复杂的 if else 链来获得我想要的东西,所以我不得不使用 vba 路由。 我已经尝试了其他问题中建议的几种方法,但它们并没有像我想要的那样工作。
我正在为一个按钮创建代码,该按钮显示包含基于复选框的特定元素的数据表。下面的代码将只是显示 Select 查询的元素。
我尝试了 .QueryDef 方法
Dim qd As QueryDef
Set qd = CurrentDb.CreateQueryDef("")
With qd
.ReturnsRecords = True
.sql = "SELECT * FROM EXPORT_CERTIFICATION WHERE EXPORT_CERTIFICATION.CertificationStatus = 'Certified'"
End With
这有效,但只有当我输入查询名称时才有效,而不是在空的时候。它创建了一个新查询来执行我想要的操作,但此后按钮没有执行任何操作。
我是否应该创建一个 table 来接受输出并尝试将 table 设置为由 sql 语句创建的记录集?我想避免使用另一个 table,因为它仅用于查看。
这是我目前尝试的更简单解决方案的完整代码
Private Sub NDC_CERT_VIEW_Click()
Dim StrSQLclause As String
Dim db1 As DAO.Database, qry1 As DAO.QueryDef
Set db1 = CurrentDb()
Set qry1 = db1.QueryDefs("NDC_EXPORT_VIEW")
MsgBox ("Here")
If (Certified_Check And Not Revised_Check And Not Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' "
ElseIf (Not Certified_Check And Revised_Check And Not Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Not Certified_Check And Not Revised_Check And Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null "
ElseIf (Not Certified_Check And Not Revised_Check And Not Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
ElseIf (Certified_Check And Revised_Check And Not Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Not Certified_Check And Revised_Check And Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Not Certified_Check And Not Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
ElseIf (Certified_Check And Not Revised_Check And Not Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
ElseIf (Not Certified_Check And Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null "
ElseIf (Certified_Check And Not Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null "
ElseIf (Certified_Check And Revised_Check And Not Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Certified_Check And Revised_Check And Doc_Exceptions_Check And Not Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' "
ElseIf (Certified_Check And Revised_Check And Doc_Exceptions_Check And Pending_Check) Then
StrSQLclause = "Select * From EXPORT_NDC_CERTIFICATION Where EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Certified' Or EXPORT_NDC_CERTIFICATION.CertificationStatus = 'Revised' Or EXPORT_NDC_CERTIFICATION.DocumentException Not Is Null Or EXPORT_NDC_CERTIFICATION.CertificationStatus = '' "
Else
MsgBox ("No Status Selected")
Exit Sub
End If
MsgBox (StrSQLclause)
MsgBox ("Here3")
qry1.sql = StrSQLclause
MsgBox ("Here4")
DoCmd.OpenQuery "NDC_EXPORT_VIEW"
MsgBox ("Here6")
我建议您使用预建的表单和查询来达到您要在此处实现的目的。
但是,话虽如此,您正在学习在 VBA 中使用 SQL,所以这里是使用代码的方法(其他方法更有可能):
- 创建一个基本表单并为每个表单添加正确数量的控件 记录。
- 为表格设置
Has Module
属性(在其他 选项卡)到 是 。 - 将
Default View
设置为DataSheet
。 - 将表格另存为
MyForm
。
添加一个VBA代码模块并添加此代码:
Sub Test()
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim frm As Form_MyForm
Set qd = CurrentDb.CreateQueryDef("", "PARAMETERS Stats Text(255); " & _
"SELECT * FROM EXPORT_CERTIFICATION " & _
"WHERE CertificationStatus = Stats")
qd.Parameters("Stats") = "Certified"
Set rs = qd.OpenRecordset
Set frm = New Form_MyForm
'The record source for the form
Set frm.Recordset = rs
'The record source fields attached to each control.
frm.Text0 = "FieldA"
frm.Text2 = "Field"
frm.Text3 = "CertificationStatus"
frm.Visible = True
Debug.Assert False 'Form will disappear when code ends, so pause here.
End Sub
将出现一个包含您的记录集的表单。
就我个人而言,我使用以下代码来显示记录集。
像 Darren 的回答一样,我创建了一个表单,我将其命名为 frmDynDS,默认视图设置为数据表视图,并且我使用以下代码向其添加了 255 个控件 (运行当表单处于设计视图时):
Dim i As Long
Dim myCtl As Control
For i = 0 To 254
Set myCtl = Application.CreateControl("frmDynDS", acTextBox, acDetail)
myCtl.Name = "Text" & i
Next i
然后,我将以下代码添加到表单的模块中:
Public Myself As Object
Public Sub LoadRS(myRS As Object)
'Supports both ADODB and DAO recordsets
Dim i As Long
Dim myTextbox As textbox
Dim fld As Object
i = 0
With myRS
For Each fld In myRS.Fields
Set myTextbox = Me.Controls("Text" & i)
myTextbox.Properties("DatasheetCaption").Value = fld.Name
myTextbox.ControlSource = fld.Name
myTextbox.ColumnHidden = False
myTextbox.columnWidth = -2
i = i + 1
Next fld
End With
For i = i To 254
Set myTextbox = Me.Controls("Text" & i)
myTextbox.ColumnHidden = True
Next i
Set Me.Recordset = myRS
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set Myself = Nothing 'Prevent memory leak
End Sub
然后,我在 public 模块中得到以下代码:"
Public Sub DisplayRS(rs As Object)
Dim f As New Form_frmDynDS
f.LoadRS rs
f.Visible = True
Set f.Myself = f
End Sub
完成所有这些设置后,显示记录集就非常简单了。只需执行以下操作:
DisplayRS CurrentDb.OpenRecordset("SELECT * FROM EXPORT_CERTIFICATION WHERE EXPORT_CERTIFICATION.CertificationStatus = 'Certified'")
这将打开表单,使适当数量的控件可见,设置标题,调整单元格宽度以容纳标题,然后将控件绑定到记录集。窗体将一直存在直到关闭,您可以使用此代码同时打开多个记录集。
请注意,当 运行 执行此代码时,您不能在记录集中使用参数,因为它会在 filtering/sorting.
时崩溃