Formating table field programatically

  • Thread starter Thread starter JackN
  • Start date Start date
J

JackN

I'm creating a table using DAO,
how do I set the format for the field to display as "Percent" when I open
the table?
With CreateField I can assign type, but not format.

Thanks
 
You can CreateProperty() on the field.
The property name is Format
The property type is dbText
The value to assign is "Percent"

Because the property has to be created if it does not exist, I use the
function below, and call it like this:
Call SetPropertyDAO(Currentdb.TableDefs("MyTable").Fields("MyField"), _
"Format", dbText, "Percent")

Function SetPropertyDAO(obj As Object, strPropertyName As String, _
intType As Integer, varValue As Variant, Optional strErrMsg As String) As
Boolean

If HasProperty(obj, strPropertyName) Then
obj.Properties(strPropertyName) = varValue
Else
obj.Properties.Append obj.CreateProperty(strPropertyName, intType,
varValue)
End If
SetPropertyDAO = True
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
Dim varDummy As Variant
On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function
 
Thanks a lot!

This works perfectly, I never thought it's that complicated, was thinking I
can do it with one line of code :)
 
Back
Top