Solved

Setting properties to Access field using VBA

Posted on 2012-04-03
3
781 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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