The following example displays the ProductName and QuantityPerUnit fields in the first two columns in Datasheet view of the Products form.
Forms!Products!ProductName.ColumnOrder = 1
Forms!Products!QuantityPerUnit.ColumnOrder = 2
The next example displays the ProductName and QuantityPerUnit fields in the first two columns of the Products table in Datasheet view. To set the ColumnOrder property, the example uses the SetFieldProperty procedure. If this procedure is run while the table is open, changes will not be displayed until is closed and reopened.
Dim dbs As Database, tdfProducts As TableDef
Set dbs = CurrentDb
Set tdfProducts = dbs!Products
SetFieldProperty tdfProducts!ProductName, "ColumnOrder", dbLong, 2
SetFieldProperty tdfProducts!QuantityPerUnit, "ColumnOrder", dbLong, 3
Sub SetFieldProperty(fldField As Field, strPropertyName As String, _
intPropertyType As Integer, varPropertyValue As Variant)
' Set field property without producing nonrecoverable run-time error.
Const conErrPropertyNotFound = 3270
Dim prpProperty As Property
On Error Resume Next ' Don't trap errors.
fldField.Properties(strPropertyName) = varPropertyValue
If Err <> 0 Then ' Error occurred when value set.
If Err <> conErrPropertyNotFound Then
On Error GoTo 0
MsgBox "Couldn't set property '" & strPropertyName _
& "' on field '" & fldField.name & "'", 48, "SetFieldProperty"
Else
On Error GoTo 0
Set prpProperty = fldField.CreateProperty(strPropertyName, _
intPropertyType, varPropertyValue)
fldField.Properties.Append prpProperty
End If
End If
End Sub