Modifying field properties in table using VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using Access XP, and would like to be able to modify a field's Format and
InputMask properties using VBA. I see there is a CreateField method, but is
there a way to modify an already existing field? Thanks..
 
You set the property of a field in a table:

If the property already exists, use:
CurrentDb.TableDefs("MyTable").Fields("MyField").Properties("Format") = ...

The function below sets the property if it exists, and creates and sets it
if it does not. Call it like this:
Call 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
 
Back
Top