Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function to create a field (Access 2000)

    My function to create a field is not working. I get no error but also no field is created. What is the reason ?
    Public Function CreateField()
    Dim wsp As DAO.Workspace
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim prp As DAO.Property
    Dim idx As DAO.Index
    Set wsp = DAO.DBEngine.Workspaces(0)
    Set dbs = CurrentDb
    Set tdf = dbs.TableDefs("TblClients")
    Set fld = tdf.CreateField("Tracks", dbText)
    tdf.Fields.Refresh
    dbs.Close
    Set fld = Nothing
    Set tdf = Nothing
    Set dbs = Nothing
    Set wsp = Nothing
    End Function

  2. #2
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Function to create a field (Access 2000)

    Hi Markus

    add the line in bold (ensure table is closed)

    Public Function CreateField()
    Dim wsp As DAO.Workspace
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim prp As DAO.Property
    Dim idx As DAO.Index
    Set wsp = DAO.DBEngine.Workspaces(0)
    Set dbs = CurrentDb
    Set tdf = dbs.TableDefs("TblClients")
    Set fld = tdf.CreateField("Tracks", dbText)
    tdf.Fields.Append fld
    tdf.Fields.Refresh
    dbs.Close
    Set fld = Nothing
    Set tdf = Nothing
    Set dbs = Nothing
    Set wsp = Nothing
    End Function
    Jerry

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Function to create a field (Access 2000)

    In addition to Jezza's reply, there are a lot of unnecessary lines in your code.
    There is no need to use a Workspace variable, so you can remove the three lines with wsp.
    You don't set any properties or create an index, so there is no need to define prp and idx.
    You cannot close the current database, so you don't need the line dbs.Close.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •