Can't find way to read Fields format programmatically

M

Marko Utriainen

Hi,

How can I read (and change) programmatically fields type of format? I can
change it manually in tables design view in ' Field Properties' section on
'General' tab but I just can't find the way how I can check it when running
code.

Thanks in advance, Marko Utriainen
 
A

Allen Browne

You can set the Format property with DAO only.

If a field has a Format defined, you can read it like this:
CurrentDb().TableDefs("MyTable").Fields("MyField").Properties("Format")

If not, that will produce error 3270, and you have to CreateProperty().

Paste the 2 functions below into a module. You can then create and set the
property with just this line of code:
? SetPropertyDAO(CurrentDb().TableDefs("MyTable").Fields("MyField"),
"Format", dbText, "Currency")


Function SetPropertyDAO(obj As Object, strPropertyName As String, intType As
Integer, varValue As Variant, Optional strErrMsg As String) As Boolean
On Error GoTo ErrHandler
'Purpose: Set a property for an object, creating if necessary.
'Arguments: obj = the object whose property should be set.
' strPropertyName = the name of the property to set.
' intType = the type of property (needed for creating)
' varValue = the value to set this property to.
' strErrMsg = string to append any error message to.

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

ExitHandler:
Exit Function

ErrHandler:
strErrMsg = strErrMsg & obj.Name & "." & strPropertyName & " not set to
" & varValue & ". Error " & Err.Number & " - " & Err.Description & vbCrLf
Resume ExitHandler
End Function

Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant

On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function


--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

message
news:[email protected]...
 
T

Tim Ferguson

=?Utf-8?B?TWFya28gVXRyaWFpbmVu?=
How can I read (and change) programmatically fields type of format? I
can change it manually in tables design view in ' Field Properties'
section on 'General' tab but I just can't find the way how I can
check it when running code.

But but but but....

There is no conceivable reason to! The format property of the field object
affects two things only:

1) the presentation of the data in a table datasheet, and nobody who knows
enough to get into heavy DAO programming is going to make his users look at
them! <ugh!>

2) the default format setting for any new controls based on the field when
added at form design time. If your users are able to create forms and
controls, then they have access to the table design too, and can change the
format property themselves.

Ergo, there just is no call to mess about with field format properties at
runtime (or input mask, or description, etc).

All the best


Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top