Solved

DAO and VB - autoincrement solution?

Posted on 2004-03-25
9
925 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
ID: 10684219
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
ID: 10686175
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
ID: 10687610
@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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:Hendrik2
ID: 10687615
@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
ID: 10687619
@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
ID: 10687622
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
ID: 10687647
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
ID: 10687802
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
ID: 10688825
thanks a lot - it works :-)

Greets
Hendrik
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

831 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