Solved

AutoNumber for DAO CreateField

Posted on 1998-08-18
2
488 Views
Last Modified: 2008-03-04
From what I gather so far, it does not seem to be possible to create a field object (i.e. CreateField) with an AutoNumber datatype using Access VBA/DAO. Is this correct? If so, there's got to be a trick to get around it somehow, doesn't there?
My hunch is that the AutoNumber thing is part of one of those special Document objects that Access tags onto certain DAOs (i.e., custom property), and is specific only to Access, and not really supported solely at the DAO level. Am I on the right track? Completely off-base?
0
Comment
Question by:mdoleman
[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 Comments
 
LVL 4

Accepted Solution

by:
tomook earned 150 total points
ID: 1959575
Try this:

Sub CreateTableWithAutonumber(TableName As String)
    Dim db As Database
    Dim td As TableDef
    Dim fd As Field
   
    Set db = CurrentDB
    Set td = CurrentDB.CreateTableDef("tblTestAutonum")
   
    Set fd = td.CreateField("fldTestAutonum")
    fd.Type = dbLong
    ' Here is the real trick. Have to set the attributes.
    fd.Attributes = dbAutoIncrField
    td.Fields.Append fd
    db.TableDefs.Append td
   
    Set fd = Nothing
    Set td = Nothing
    Set db = Nothing
End Sub

1
 

Author Comment

by:mdoleman
ID: 1959576
Thanks Tomook, that's the answer; ashamed I didn't find that one myself.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

749 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