Skip to main content

Get the field name

This function will get a field name of a field given its location within the database.  If the location is -1 it will default to the last field name.

I have updated the script, it now uses 0 to find the last entry and negative numbers to find the field name starting from the right.

Snippet
'******************************************************************************************************
'* Function:	getFieldName
'* Purpose:	It will find a field name based on its position in the database, use 0
'*		for the last field, a positive number to find a field counting from the left
'*		and a negative number to find a field from the right
'* Accepts:	Filename of the file and an integer representing the location of the field
'* Returns:	The field name
'******************************************************************************************************

Function getFieldName(filename As String, position As Integer) As String

	Dim db As database
	Dim ThisTable As table
	Dim fieldInfo As Object
	Dim noOfFields As Integer
	
	Set db = Client.OpenDatabase(filename)
		Set ThisTable = db.TableDef
		
			If position = 0 Then
				noOfFields = ThisTable.Count
			ElseIf position < 0 Then
				noOfFields = ThisTable.Count
				noOfFields = noOfFields + position
			Else
				noOfFields = position
			End If
			Set fieldInfo = ThisTable.GetFieldAt(noOfFields)
			
				getFieldName = fieldInfo.Name
			Set fieldInfo = Nothing
		Set ThisTable = Nothing
	Set db = Nothing

End Function