Solved

Setting properties to Access field using VBA

Posted on 2012-04-03
3
721 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

13 Experts available now in Live!

Get 1:1 Help Now