03.04 Handling Fields - AndrewMB2/Logic-Architect-for-Excel-VBA GitHub Wiki

Handling Fields

All data in Xdata objects is stored in fields and records. It is always possible to reference a field either by name of by number (1-based). There are a number of procedures available to manage the field set. All Xdata objects support:

Name Function
GetType Returns the datatype of the field as a VbVarType constant (TableData always returns -1 as data is not typed in Excel sheets).
Columns Returns the number of fields/columns.
Column Returns the number of a field/column name.
GetHeader Returns the name of a field/column number. In the case of TableData, it is also possible to set this, which has the effect of changing the field name as used in subsequent procedures without changing what appears on the worksheet. This is useful for field matching.
Exists Checks if a field/column exists.

In addition, ArrData supports:

Name Function
Rename Renames a field.
AddField Adds a new field (at the end) and optionally sets its initial value.
AddFields Adds all the fields in a specified Xdata object not already there. If the Xdata object is TableData (where there is no data type), a data type can be supplied as a parameter which is applied to all the fields. ChangeType can be applied afterwards if needed.
DropField Drops one or more fields.
ChangeType Changes the data type of a field.
GetOffset Transfers data from one field to another, optionally applying an offset to pick up data from an earlier or later record.

And TableData supports:

Name Function
HdrRange Returns the header as a range object.
CheckHeaders Checks if a set of header field names exists.
GetHeaders Returns non-blank headers as a comma-separated string. Can be run in the immediate pane to produce a field list which can be pasted into ArrData Create when setting up a matching ArrData object. Can also set all the field names from an array of names (does not alter the names shown on the worksheet).
GetTypes Returns data types as a comma-separated string based on first row. Can be run in the immediate pane to produce a data types list which can be pasted into ArrData Create when setting up a matching ArrData object. However since it is only based on the first data row, some types may need to be altered.
GetColHeader Gets the column header of a field as a range. Column headers can be altered without affecting the field name.
AddColumn Adds a column at the end (assumes space is available, does not shift anything).
InsertColumn Inserts a column at a specified position and shifts anything on the right to the right.
DeleteColumn Deletes a column and shifts anything on the right to the left.
GetColumn Gets the column data of a column as a range.