Field type

2 posts / 0 new
Last post
Robert van den ...
Offline
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) = field.name
Next i
j = 1
 
Do
For i = 1 To field_count
If fieldname = fieldNames(i) Then
fieldname = origFieldName & j
j = j + 1
fieldNameOk = FALSE
Exit For
Else
fieldNameOk = TRUe
End If
'
Next i
Loop While fieldNameOk = FALSE
 
checkIfFieldNameExists = fieldname
 
If fieldname = (Field0) Then
FieldExists = True
Else 
FieldExists = False
End If
Set field = Nothing
Set table = Nothing
Set db = Nothing
 
End Function
 

ravisdxb
Offline
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).
Regards,
Ravi
 
 
 
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)
 retval="Undefined"
 Select Case fld.Type
  Case 3 'character
   retval="String"
  Case 4 'number
   retval="Numeric"
  Case 5  'date
   retval="Date"
  Case 9  'multistate
   retval="Multistate"
  Case 11 'time
   retval="Time"
 End Select    
 Set fld=Nothing
 Set tab=Nothing
 Set db=Nothing
 retval="Type of " & fieldname & " is " & retval
Exit Sub
errHandler:
 If err.Number=-2097151022 Then
  retval="No such field " & fieldname & " in " & dbname
 Else
  MsgBox err.Number & err.Description
 End If 
End Sub