Name Property

Applies To   Connection object, Container object, Database object, Document object, Dynamic-Type Recordset object, Dynaset-Type Recordset object, Field object, Forward-Only–Type Recordset object, Group object, Index object, Parameter object, Property object, QueryDef object, Recordset object, Relation object, Snapshot-Type Recordset object, Table-Type Recordset object, TableDef object, User object, Workspace object.

Description

Sets or returns a user-defined name for a DAO object. For an object not appended to a collection, this property is read/write.

Settings and Return Values

The setting or return value is a String that specifies a name. The name must start with a letter. The maximum number of characters depends on the type of object Name applies to, as shown in Remarks. It can include numbers and underscore characters ( _ but can't include punctuation or spaces.

Remarks

TableDef, QueryDef, Field, Index, User, and Group objects can't share the same name with any object in the same collection.

The Name property of a Recordset object opened by using an SQL statement is the first 256 characters of the SQL statement.

You can use an object's Name property with the Visual Basic for Applications Dim statement in code to create other instances of the object.

Note For many of the DAO objects, the Name property reflects the name as known to the Database object, as in the name of a TableDef, Field, or QueryDef object. There is no direct link between the name of the DAO object and the object variable used to reference it.

The read/write usage of the Name property depends on the type of object it applies to, and whether or not the object has been appended to a collection. In an ODBCDirect workspace, the Name property of an appended object is always read-only. The following table indicates whether the Name property in a Microsoft Jet workspace is read/write or read-only for an object that is appended to a collection (unless otherwise noted), and also indicates its maximum length in cases where it is read/write.

Object

Usage

Maximum length

Container

Read-only

Connection

Read-only

Database

Read-only

Document

Read-only

Field

Unappended

Read/write

64

Appended to Index

Read-only

Appended to QueryDef

Read-only

Appended to Recordset

Read-only

Appended to TableDef (native)

Read/write

64

Appended to TableDef (linked)

Read-only

Appended to Relation

Read-only

Group

Unappended

Read/write

20

Appended

Read-only

Index

Unappended

Read/write

64

Appended

Read-only

Parameter

Read-only

Property

Unappended

Read/write

64

Appended

Read-only

Built-in

Read-only

QueryDef

Unappended

Read/write

64

Temporary

Read-only

Appended

Read/write

64

Recordset

Read-only

Relation

Unappended

Read/write

64

Appended

Read-only


(continued)

TableDef

Read/write

64

User

Unappended

Read/write

20

Appended

Read-only

Workspace

Unappended

Read/write

20

Appended

Read-only


See Also   CreateDatabase method, CreateField method, CreateIndex method, CreateQueryDef method.

Example

This example uses the Name property to give a name to a newly created object, to show what objects are in a given collection, and to delete an object from a collection.

Sub NameX()

    Dim dbsNorthwind As Database
    Dim qdfNew As QueryDef
    Dim qdfLoop As QueryDef

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")

    With dbsNorthwind
        ' Create a new permanent QueryDef object and append it
        ' to the QueryDefs collection.
        Set qdfNew = .CreateQueryDef()
        qdfNew.Name = "NewQueryDef"
        qdfNew.SQL = "SELECT * FROM Employees"
        .QueryDefs.Append qdfNew

        ' Enumerate the QueryDefs collection to display the
        ' names of the QueryDef objects.
        Debug.Print "Names of queries in " & .Name

        For Each qdfLoop In .QueryDefs
            Debug.Print "    " & qdfLoop.Name
        Next qdfLoop

        ' Delete new QueryDef object because this is a
        ' demonstration.
        .QueryDefs.Delete qdfNew.Name

        .Close
    End With

End Sub
Example (Microsoft Access)

The following example creates two new TableDef objects and names them. The name of the first TableDef object is included as an argument to the CreateTableDef method. The name of the second TableDef object is set by using the Name property, after the TableDef object has been created.

Note that you must define fields in the table before the TableDef object can be appended to the TableDefs collection.

Sub NameNewTables()
    Dim dbs As Database
    Dim tdfDefinitions As TableDef, tdfSynonyms As TableDef

    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Create new TableDef object.
    Set tdfDefinitions = dbs.CreateTableDef("Definitions")
    ' Create second TableDef object.
    Set tdfSynonyms = dbs.CreateTableDef("")
    ' Set Name property for second TableDef object.
    tdfSynonyms.Name = "Synonyms"
    .                                        ' Create fields.
    .
    .
    dbs.TableDefs.Append tdfDefinitions
    dbs.TableDefs.Append tdfSynonyms
    Set dbs = Nothing
End Sub
Example (Microsoft Excel)

This example enters in the active cell on Sheet1 the name of the first recordset in the Nwindex.mdb database.

To create the Nwindex.mdb database, run the Microsoft Excel example for the CreateDatabase method.

Dim db As Database, td As TableDef
Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB")
Set td = db.TableDefs(0)
Sheets("Sheet1").Activate
ActiveCell.Value = td.Name
db.Close