Solved

DAO and VB - autoincrement solution?

Posted on 2004-03-25
9
907 Views
Last Modified: 2013-12-25
Hi there

I want to create a database with this layout:

ID -- Feld 2
1      one      
2      two      

The ID must autoincemented like MySQL autoincement function. How can i do this?

This is the Code i use for creating my database..

[code]
      ' Name der Datenbank '
        DBName = App.Path & ".\" & DBName
     
        ' leere Datenbank anlegen '
        Set db = CreateDatabase(DBName, dbLangGeneral & Passwort, dbEncrypt)
       
        ' Tabellendef 1 anlegen '
        Set tbl = db.CreateTableDef("Tabelle1")
       
        ' 1tes Feld in Tabelle 1 anlegen und anhängen '
        Set fld = tbl.CreateField("ID", dbText, 50): tbl.Fields.Append fld
       
        ' 2tes Feld in 1. Tabelle anlegen und anhängen '
        Set fld = tbl.CreateField("Feld2", dbText, 50):  tbl.Fields.Append fld
       
        ' Tabelle in Datenbank anlegen '
        db.TableDefs.Append tbl
   
        ' Primary-Key in Tabelle1 auf Feld1 setzen '
        Set ind = tbl.CreateIndex("PrimaryKey")
        ind.Fields = "Feld1"
        ind.Primary = True
        ind.Unique = True
[/code]

This is code i found in the net, how can i autoincrement the ID?
I never used a database engine from Visual Basic.

thanks 4 help and sorry for my bad english
greets from Germany
Hendrik
0
Comment
Question by:Hendrik2
  • 6
  • 2
9 Comments
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
Hallo Hendrik2,
Is this a MySQL database. In this case I recommend not to use DAO
If it is an MSAccess .mdb file, then you have the autonumber field

Dabas
0
 
LVL 2

Expert Comment

by:dmenard
Comment Utility
Set the ID field attribute to dbAutoIncrField :

Set fld = tbl.CreateField("ID", dbText, 50)
fld.Attributes = fld.Attributes Or dbAutoIncrField
tbl.Fields.Append fld
0
 

Author Comment

by:Hendrik2
Comment Utility
@Dabas
Its a .mdb file that the script makes (DAO 3.5). How can i autonumber the field ?

@dmenard
if i change the code to yours i get the error "invalid argument - Ungültiges Argument. (Fehler 3001)" at these Line:

   db.TableDefs.Append tbl

I also checked this with a big integer

   Set fld = tbl.CreateField("ID", dbBigint, 16):  ' bigint is better for incrementing :-)

but i get the same error.

Here is the code again:


        '  Felder in Tabelle 1 anlegen und anhängen '
        Set fld = tbl.CreateField("ID",dbBigInt, 16):
        fld.Attributes = fld.Attributes Or dbAutoIncrField:
        tbl.Fields.Append fld
   
        Set fld = tbl.CreateField("Titel", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Beschreibung", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Kategorie", dbText, 50): tbl.Fields.Append fld
       
        ' Tabelle in Datenbank anlegen '
        db.TableDefs.Append tbl


0
 

Author Comment

by:Hendrik2
Comment Utility
@Dabas
Its a .mdb file that the script makes (DAO 3.5). How can i autonumber the field ?

@dmenard
if i change the code to yours i get the error "invalid argument - Ungültiges Argument. (Fehler 3001)" at these Line:

   db.TableDefs.Append tbl

I also checked this with a big integer

   Set fld = tbl.CreateField("ID", dbBigint, 16):  ' bigint is better for incrementing :-)

but i get the same error.

Here is the code again:


        '  Felder in Tabelle 1 anlegen und anhängen '
        Set fld = tbl.CreateField("ID",dbBigInt, 16):
        fld.Attributes = fld.Attributes Or dbAutoIncrField:
        tbl.Fields.Append fld
   
        Set fld = tbl.CreateField("Titel", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Beschreibung", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Kategorie", dbText, 50): tbl.Fields.Append fld
       
        ' Tabelle in Datenbank anlegen '
        db.TableDefs.Append tbl


0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Hendrik2
Comment Utility
@Dabas
Its a .mdb file that the script makes (DAO 3.5). How can i autonumber the field ?

@dmenard
if i change the code to yours i get the error "invalid argument - Ungültiges Argument. (Fehler 3001)" at these Line:

   db.TableDefs.Append tbl

I also checked this with a big integer

   Set fld = tbl.CreateField("ID", dbBigint, 16):  ' bigint is better for incrementing :-)

but i get the same error.

Here is the code again:


        '  Felder in Tabelle 1 anlegen und anhängen '
        Set fld = tbl.CreateField("ID",dbBigInt, 16):
        fld.Attributes = fld.Attributes Or dbAutoIncrField:
        tbl.Fields.Append fld
   
        Set fld = tbl.CreateField("Titel", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Beschreibung", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Kategorie", dbText, 50): tbl.Fields.Append fld
       
        ' Tabelle in Datenbank anlegen '
        db.TableDefs.Append tbl


0
 

Author Comment

by:Hendrik2
Comment Utility
Dabas
-------
Its a .mdb file that the script makes (DAO 3.5). How can i autonumber the field ?

dmenard
----------
if i change the code to yours i get the error "invalid argument - Ungültiges Argument. (Fehler 3001)" at these Line:

   db.TableDefs.Append tbl

I also checked this with a big integer

   Set fld = tbl.CreateField("ID", dbBigint, 16):  ' bigint is better for incrementing :-)

but i get the same error.

Here is the code again:


        '  Felder in Tabelle 1 anlegen und anhängen '
        Set fld = tbl.CreateField("ID",dbBigInt, 16):
        fld.Attributes = fld.Attributes Or dbAutoIncrField:
        tbl.Fields.Append fld
   
        Set fld = tbl.CreateField("Titel", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Beschreibung", dbText, 50): tbl.Fields.Append fld
        Set fld = tbl.CreateField("Kategorie", dbText, 50): tbl.Fields.Append fld
       
        ' Tabelle in Datenbank anlegen '
        db.TableDefs.Append tbl


0
 

Author Comment

by:Hendrik2
Comment Utility
sorry for this mass posting i got always a tomcat post error after submitting???

@admin
please delete the laste 3 answers from me
thx

Hendrik
0
 
LVL 2

Accepted Solution

by:
dmenard earned 125 total points
Comment Utility
Sorry, hendryk2 !

You're right, dbText is not very good for incrementing :-)

Well I think that you must use dbLong for an autoincrement field, not bigint. And we must not specify the size in this case, if my momory is good...

So try that :
       Set fld = tbl.CreateField("ID",dbLong):
       fld.Attributes = fld.Attributes Or dbAutoIncrField:
       tbl.Fields.Append fld
 
Does-it work ?

(sorry, I can test it right now)
0
 

Author Comment

by:Hendrik2
Comment Utility
thanks a lot - it works :-)

Greets
Hendrik
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

14 Experts available now in Live!

Get 1:1 Help Now