使用 ColumnOrder 屬性可以指定“數(shù)據(jù)表”視圖中列的順序。Integer 型,可讀寫。
expression.ColumnOrder
expression 必需。返回“應(yīng)用于”列表中的一個(gè)對(duì)象的表達(dá)式。
通過在“數(shù)據(jù)表”視圖中選擇一列并將其拖到新的位置即可設(shè)置該屬性。
在 Microsoft Access 數(shù)據(jù)庫 (.mdb) 中,通過在 Visual Basic 中使用 Long Interger 值,也可以設(shè)置該屬性。
若要通過使用 Visual Basic 來設(shè)置或更改表或查詢的這一屬性,必須使用列的 Properties 集合。有關(guān)使用 Properties 集合的詳細(xì)信息,請(qǐng)參閱 Properties。
注釋 ColumnOrder 屬性在“設(shè)計(jì)”視圖中不可用。
注釋 ColumnOrder 屬性適用于“數(shù)據(jù)表”視圖中所有的字段,并且當(dāng)窗體處于“數(shù)據(jù)表”視圖時(shí),還適用于窗體控件。
在“數(shù)據(jù)表”視圖中,字段的 ColumnOrder 屬性設(shè)置是由這個(gè)字段的位置確定的。例如,在“數(shù)據(jù)表”視圖中,最左側(cè)列中字段的 ColumnOrder 屬性設(shè)置為 1,第二個(gè)字段的屬性設(shè)置為 2,依此類推。更改某個(gè)字段的 ColumnOrder 屬性,將重新設(shè)置那個(gè)字段及“數(shù)據(jù)表”視圖中其原始位置左側(cè)每個(gè)字段的這項(xiàng)屬性。
在其他視圖中,除非在“數(shù)據(jù)表”視圖中明確地更改字段的次序(通過將字段拖到新位置,或通過更改其 ColumnOrder 屬性設(shè)置),否則該屬性將設(shè)為 0。位于移動(dòng)字段新位置右側(cè)的字段,在除“數(shù)據(jù)表”視圖以外的視圖中,該屬性都設(shè)為 0。
“數(shù)據(jù)表”視圖中字段的次序并不影響表“設(shè)計(jì)”視圖或“窗體”視圖中字段的次序。
以下示例在“產(chǎn)品”窗體的“數(shù)據(jù)表”視圖中,將“產(chǎn)品名稱”和“單位數(shù)量”字段顯示在前兩列中。
Forms!Products!ProductName.ColumnOrder = 1
Forms!Products!QuantityPerUnit.ColumnOrder = 2
下一個(gè)示例在“數(shù)據(jù)表”視圖中,將“產(chǎn)品”表的“產(chǎn)品名稱”和“單位數(shù)量”字段顯示在前兩列中。為了設(shè)置 ColumnOrder 屬性,本示例使用了 SetFieldProperty 過程。如果在表打開的狀態(tài)下執(zhí)行該過程,則更改只能在表關(guān)閉并重新打開后才能顯示。
Public Sub SetColumnOrder()
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Set dbs = CurrentDb
Set tdf = dbs!Products
' Call the procedure to set the ColumnOrder property.
SetFieldProperty tdf!ProductName, "ColumnOrder", dbLong, 2
SetFieldProperty tdf!QuantityPerUnit, "ColumnOrder", dbLong, 3
Set tdf = Nothing
Set dbs = Nothing
End Sub
Private Sub SetFieldProperty(ByRef fld As DAO.Field, _
ByVal strPropertyName As String, _
ByVal intPropertyType As Integer, _
ByVal varPropertyValue As Variant)
' Set field property without producing nonrecoverable run-time error.
Const conErrPropertyNotFound = 3270
Dim prp As Property
' Turn off error handling.
On Error Resume Next
fld.Properties(strPropertyName) = varPropertyValue
' Check for errors in setting the property.
If Err <> 0 Then
If Err <> conErrPropertyNotFound Then
On Error GoTo 0
MsgBox "Couldn't set property '" & strPropertyName & _
"' on field '" & fld.Name & "'", vbCritical
Else
On Error GoTo 0
Set prp = fld.CreateProperty(strPropertyName, intPropertyType, _
varPropertyValue)
fld.Properties.Append prp
End If
End If
Set prp = Nothing
End Sub