Solved

Setting properties to Access field using VBA

Posted on 2012-04-03
3
750 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 - Access MVP) 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now