Field type

2 posts / 0 new
Last post
Robert van den ...
Joined: 08/08/2018 - 07:37
Field type

Is there a way to check if a field type is numeric or a string? I found the code to check is a field exists but now i need some code to check is the field type is numeric or not.
Function checkIfFieldNameExists(fieldname As String, filename As String) As String
fieldname = UCase(fieldname)
origFieldName = fieldname
Set db = Client.OpenDatabase(Databasename)
Set table = db.TableDef
field_count = table.count
ReDim fieldNames(field_count)
For i = 1 To field_count
Set field = table.GetFieldAt(i)
fieldNames(i) =
Next i
j = 1
For i = 1 To field_count
If fieldname = fieldNames(i) Then
fieldname = origFieldName & j
j = j + 1
fieldNameOk = FALSE
Exit For
fieldNameOk = TRUe
End If
Next i
Loop While fieldNameOk = FALSE
checkIfFieldNameExists = fieldname
If fieldname = (Field0) Then
FieldExists = True
FieldExists = False
End If
Set field = Nothing
Set table = Nothing
Set db = Nothing
End Function

Joined: 01/13/2019 - 06:12

Hello Robert,
Please check whether the following code works for you. I have hard-coded sales.IMD as filename and prod_code as field name. Please also try a fieldname which is not in the database, say prod_code_1 which should give you an error message that the field is not in the database. Also, this code returns all data types (string, numeric, date, time and multistate).
Dim retval As String
Sub Main
 Call getFieldType( "sales.IMD","prod_code")
 MsgBox retval
End Sub
Sub getFieldType(dbname,fieldname)
 On Error GoTo errHandler
 Set db=Client.OpenDatabase(dbname)
 Set tab=db.TableDef
 Set fld=tab.GetField(fieldname)
 Select Case fld.Type
  Case 3 'character
  Case 4 'number
  Case 5  'date
  Case 9  'multistate
  Case 11 'time
 End Select    
 Set fld=Nothing
 Set tab=Nothing
 Set db=Nothing
 retval="Type of " & fieldname & " is " & retval
Exit Sub
 If err.Number=-2097151022 Then
  retval="No such field " & fieldname & " in " & dbname
  MsgBox err.Number & err.Description
 End If 
End Sub