Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / April 2005

Tip: Looking for answers? Try searching our database.

Modifying field properties in table using VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Terri - 16 Apr 2005 01:27 GMT
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..
Signature

Terri

Allen Browne - 16 Apr 2005 02:37 GMT
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

Signature

Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

> 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..
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.