如何排序 Access DB Table,通过 VBA,在使用导航窗格打开时按顺序显示
How to sort an Access DB Table, via VBA, displayed in order when opened by using the Navigation Pane
作为一些插入的结束,通过VBA脚本,我一直在ACCESS中做table。我有按字段订购 table 的要求。所以第三个人会通过访问导航窗格打开它,它会按指定的顺序显示。
编辑:我还需要 table 由第三人称table。
我可以考虑创建一个新的table,使用SQL句来命令它。但这似乎是一个非常丑陋的选择。
是否有任何方法可以使用 DAO 对象或其他 VBA 方法对其进行存档?
对 table 进行排序的查询是最干净的解决方案。
如果您不想为此创建额外的对象,您可以使用 DAO 属性设置 table 排序,如下所示:
Sub DoIt()
Call TableSetSort("myTable", "myField")
End Sub
' Set a table to be sorted by <sFieldname>
Public Sub TableSetSort(sTable As String, sFieldname As String)
Dim DB As Database
Dim tdf As TableDef
Dim prop As DAO.Property
Set DB = CurrentDb
Set tdf = DB.TableDefs(sTable)
' Set field to order by
Call TableAddProperty(tdf, "OrderBy", dbText, sFieldname)
' These two may be true by default, but better safe than sorry
Call TableAddProperty(tdf, "OrderByOn", dbBoolean, True)
Call TableAddProperty(tdf, "OrderByOnLoad", dbBoolean, True)
' if you want to debug
For Each prop In tdf.Properties
Debug.Print prop.Name, prop.Value
Next prop
End Sub
' Set or add a property in a TableDef
Public Sub TableAddProperty(tdf As DAO.TableDef, sName As String, iType As DAO.DataTypeEnum, vValue As Variant)
Dim prop As DAO.Property
' Try to set the property value, this will fail with Runtime Error 3270 if the property doesn't exist
On Error Resume Next
tdf.Properties(sName) = vValue
If Err.Number = 3270 Then
' Property doesn't exist yet - create and append it
On Error GoTo 0
Set prop = tdf.CreateProperty(sName, iType, vValue)
tdf.Properties.Append prop
End If
' Note: error handling here is quite minimal!
End Sub
作为一些插入的结束,通过VBA脚本,我一直在ACCESS中做table。我有按字段订购 table 的要求。所以第三个人会通过访问导航窗格打开它,它会按指定的顺序显示。 编辑:我还需要 table 由第三人称table。
我可以考虑创建一个新的table,使用SQL句来命令它。但这似乎是一个非常丑陋的选择。
是否有任何方法可以使用 DAO 对象或其他 VBA 方法对其进行存档?
对 table 进行排序的查询是最干净的解决方案。
如果您不想为此创建额外的对象,您可以使用 DAO 属性设置 table 排序,如下所示:
Sub DoIt()
Call TableSetSort("myTable", "myField")
End Sub
' Set a table to be sorted by <sFieldname>
Public Sub TableSetSort(sTable As String, sFieldname As String)
Dim DB As Database
Dim tdf As TableDef
Dim prop As DAO.Property
Set DB = CurrentDb
Set tdf = DB.TableDefs(sTable)
' Set field to order by
Call TableAddProperty(tdf, "OrderBy", dbText, sFieldname)
' These two may be true by default, but better safe than sorry
Call TableAddProperty(tdf, "OrderByOn", dbBoolean, True)
Call TableAddProperty(tdf, "OrderByOnLoad", dbBoolean, True)
' if you want to debug
For Each prop In tdf.Properties
Debug.Print prop.Name, prop.Value
Next prop
End Sub
' Set or add a property in a TableDef
Public Sub TableAddProperty(tdf As DAO.TableDef, sName As String, iType As DAO.DataTypeEnum, vValue As Variant)
Dim prop As DAO.Property
' Try to set the property value, this will fail with Runtime Error 3270 if the property doesn't exist
On Error Resume Next
tdf.Properties(sName) = vValue
If Err.Number = 3270 Then
' Property doesn't exist yet - create and append it
On Error GoTo 0
Set prop = tdf.CreateProperty(sName, iType, vValue)
tdf.Properties.Append prop
End If
' Note: error handling here is quite minimal!
End Sub