Solved

Setting properties to Access field using VBA

Posted on 2012-04-03
3
769 Views
Last Modified: 2012-04-03
I want to set the format properties of an Access table field using vba. I try with the following code, but get an error at the line " myField.Properties(PropertyName) = PropertyValue"
What needs to be changed?



code:

Public Function fStartup

      Call SetDAOProperty("Tablename", "Fieldname", "Format", "000000-0000")

 End Function


Function SetDAOProperty(Optional mTable As String, Optional mfield As String, Optional PropertyName As String, Optional PropertyValue As String)
 
    Dim db As DAO.Database
    Dim myTable As DAO.TableDef
    Dim myField As DAO.Field
 
    Set db = CurrentDb()
    Set myTable = db.TableDefs(mTable)
    Set myField = myTable.Fields(mfield)
 
    myField.Properties(PropertyName) = PropertyValue

    myField.Properties.Refresh
    SetDAOProperty = True
    Exit Function

End Function
0
Comment
Question by:FagerbergDellby
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 37803362
Here you go:

    Dim db As DAO.Database, prp As DAO.Property
    Set db = CurrentDb
   
    With db.TableDefs("MyTable").Fields("MyField")
            'On Error Resume Next ' This is needed one prop is created
            Set prp = .CreateProperty("Format", dbText, Chr(34) & "000000-0000" & Chr(34))

            If Err.Number = 0 Or Err.Number = 3367 Then
                 'do nothing  3367 means Property already exists
                Err.Clear
            Else
                MsgBox "error " & Err.Number & "  " & Err.Description
                Exit Function
            End If
            .Properties.Append prp
    End With
0
 

Author Closing Comment

by:FagerbergDellby
ID: 37803410
Super, works like a charm. Many thanks!
0
 
LVL 75
ID: 37803422
You are welcome ...

mx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question