Results 1 to 4 of 4
  1. #1
    Join Date
    May 2003
    Location
    Costa Rica
    Posts
    40

    Red face Unanswered: Add an Autonumeric Field into a Table

    I have a Table INFORME2. I would like to add into this table INFORME2 an autonumeric field named EMBARG using a module in Visual BAsic for Microsoft Access. Can someone help me.
    Last edited by Manuel Gimenez; 06-09-03 at 14:22.

  2. #2
    Join Date
    Jan 2003
    Location
    Rockville, MD
    Posts
    179
    Try this. You'll need to have the Microsoft DAO Object Library as a reference.
    Code:
    Sub AddFieldForAutoNumber()
       Dim dbForTable As DAO.Database
       Dim bFieldExists As Boolean
       Dim fldNew As New DAO.Field
       Dim intField As Integer
       Dim strTableName As String
       Dim strFieldName As String
       
       'use either of these two "set" commands, but not both
       'if your table is not in the current database, use
       'Set dbForTable = OpenDatabase("C:\MyFolder\MyDatabase.mdb")
       'otherwise use
       Set dbForTable = CurrentDb
       
       strTableName = "INFORME2"
       strFieldName = "EMBARG"
       bFieldExists = False
       
       'if the field already exists, or another autonumber field exists
       'don't create the field
       For intField = 0 To dbForTable.TableDefs(strTableName).Fields.Count - 1
          If _
                dbForTable.TableDefs(strTableName).Fields(intField).Name = strFieldName _
                Or _
                dbForTable.TableDefs(strTableName).Fields(intField).Attributes = 17 _
          Then
             bFieldExists = True
             Exit For
          End If
       Next
       If (Not bFieldExists) Then
          Set fldNew = dbForTable.TableDefs(strTableName).CreateField(strFieldName, dbLong)
              fldNew.Attributes = 17
          dbForTable.TableDefs(strTableName).Fields.Append fldNew
       End If
       dbForTable.Close
       Set dbForTable = Nothing
    End Sub

  3. #3
    Join Date
    May 2003
    Location
    Costa Rica
    Posts
    40

    Thumbs up Points to JTRockville

    Thank you very much JTRockville. Please moderator give the points to JTRockville. It was very helpfull. Thank you very much.

  4. #4
    Join Date
    Jan 2003
    Location
    Rockville, MD
    Posts
    179
    Points??? I get points for correct answers???

    And to think.... I was doin' it just for fun!

Posting Permissions

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