简体   繁体   English

使用 VBA 在 Microsoft Access 2010 表单中筛选结果

[英]Filter results in Microsoft Access 2010 Form using VBA

I am working on an Access Database where I need to display records from a table in a form as a datasheet.我正在开发一个 Access 数据库,我需要将表格中的记录显示为数据表。 I believe I have correctly written the code to perform the filtering, but am not sure how to display the records.我相信我已经正确编写了执行过滤的代码,但不确定如何显示记录。

I know that I can perform this easier using a query, and then a form based on those results, but wish to limit this process if at all possible, to reduce the overall size of the database.我知道我可以使用查询更轻松地执行此操作,然后使用基于这些结果的表单,但希望尽可能限制此过程,以减少数据库的整体大小。 The filter will be sorting a company, and the fiscal dates.过滤器将对公司和财务日期进行排序。

Any help is appreciated.任何帮助表示赞赏。

Here is the code I have thus far...这是我迄今为止的代码......

Option Compare Database

Sub Form_Current()
    Dim oTable As DAO.Recordset
    Dim oDataNeedsGas
    Dim dNextFiscal, dThisFiscal
    Dim iGas


'Fiscal Year turnover date, use DateValue(dNextFiscal) comparison.
    dNextFiscal = "10/1/" & Year(Date)
    dThisFiscal = "10/1/" & Year(Date) - 1

    'For Annual training by year comparison.
    'Year(DateValue(oTable!randomdate)) >= Year(Date)

Set oTable = Application.CurrentDb.OpenRecordset("tbl_main", dbOpenDynaset)    
    iGas = 0

Do Until oTable.EOF = True
    If (Year(DateValue(oTable![GasDate])) >= Year(Date) And oTable![Platoon] = "Data") Then
        `What do I do here?!!?
        iGas = iGas + 1
    End If
msgbox iGas

oTable.MoveNext

Loop  

End Sub

I know the filtering works, because I have it count the matched records, then display in a message box, but I want to be able to display the matched records.我知道过滤有效,因为我让它计算匹配的记录,然后显示在消息框中,但我希望能够显示匹配的记录。 How do I go about doing this?我该怎么做?

Make the RecordSource on your Datasheet from blank and then have this code run when the form loads:将数据表上的 RecordSource 设为空白,然后在加载表单时运行此代码:

Option Compare Database

Private Sub Form_Load()
    Dim sSQL as String
    sSQL = "SELECT * FROM tbl_Main "
    sSQL = sSQL & "WHERE Year(DateValue(GasDate)) >= Year(Date) "
    sSQL = sSQL & " AND Platoon = 'Data'"
    Me.RecordSource = sSQL
    MsgBox "RecordCount: " & Me.RecordCount
End Sub

I generally use the Form's RecordSource and the Forms Filter and FilterOn properties.我通常使用 Form 的 RecordSource 和 Forms Filter 和 FilterOn 属性。 You can always load the form showing all records and then filter down to what you want to see.您始终可以加载显示所有记录的表单,然后过滤到您想要查看的内容。

I didn't understand this line in your question: "...but wish to limit this process if at all possible, to reduce the overall size of the database."我不明白您的问题中的这一行: “...但希望尽可能限制此过程,以减少数据库的整体大小。”

Are you trying to increase performance?您是否正在尝试提高性能? Are you worried about storing too much data and the tables getting too large?您是否担心存储过多数据和表格变得过大? That part of your question just isn't clear.你问题的那部分不清楚。

You can set your Subform's Recordset property to oTable.您可以将子窗体的Recordset属性设置为 oTable。 Make the recordset a property of the main form though, as shown in the following code, so that you can release this reference when the form closes.使记录集成为主窗体的属性,如下面的代码所示,以便在窗体关闭时释放此引用。

Option Compare Database
Private oTable As Object

Private Sub Command2_Click()

    Set oTable = Application.CurrentDb.OpenRecordset("tbl_main", dbOpenDynaset)
    Set Me.sbfName.Form.Recordset = oTable
End Sub

Private Sub Form_Close()
    If Not oTable Is Nothing Then
        Set oTable = Nothing
    End If
End Sub

For your specific example you would OpenRecordset based on a SQL statement that includes your date-criteria.对于您的特定示例,您将基于包含日期标准的 SQL 语句OpenRecordset I haven't tested whether this will be updateable, as it is for a Table.我还没有测试这是否可以更新,就像表格一样。 (I am getting the impression that it will not be updateable.) (我的印象是它不会更新。)

It is possible to do this but I'm not suggesting it is a recommended approach.可以这样做,但我不建议这是一种推荐的方法。 It is far easier to use the RecordSource property, filtering its records.使用RecordSource属性过滤其记录要容易得多。

I want to emphasise that I would not use the Recordset of the subform.我想强调的是,我不会使用子窗体的Recordset Use the RecordSource .使用RecordSource You can set it to a SQL statement and/or filter records.您可以将其设置为 SQL 语句和/或过滤记录。 Using the Recordset property is problematic (and unnecessary).使用Recordset属性是有问题的(而且是不必要的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM