Solved

Creating a dbfield in Access with VB

Posted on 2002-05-16
8
148 Views
Last Modified: 2010-05-02
I have a mdb database/table that I would like to change a bit. I need to create a new field and define it as a counter. I can create all other types of fields but not the counter type. I use this code:
AppendField tdfArtiklar, "APPEND", "Http", dbText, 80
to create textfield named Http and length 80, please tell me how the code should look like for a counter field.
(Note: Appendfield is of cause a sub)
0
Comment
Question by:soft4u
  • 5
  • 3
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7013486
using what? DAO? ADO?

have a look at
http://www.experts-exchange.com/questions/Q.20146826.html
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7013504
0
 

Author Comment

by:soft4u
ID: 7013558
.Fields("ID").Attributes = dbAutoIncrField
It's OK to create the field but this line is creating en error no. RTE 3219, not allowed
(I'm using DAO)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7013559
your field needs to be numeric.
0
 

Author Comment

by:soft4u
ID: 7013889
How do I make field numeric?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7013985
something like      
.Fields.Append .CreateField("IntegerField", dbInteger)
0
 

Author Comment

by:soft4u
ID: 7015624
Still the same error, below is the sub that should work. It seems it's a problem with changing the attribute and that the method isn't allowed.

Sub AppendX()

    Dim dbsQSart As Database
    Dim tdfArtiklar As TableDef

    Set dbsQSart = OpenDatabase("c:\af99\data\lista0.mdb")
    Set tdfArtiklar = dbsQSart.TableDefs!artiklar
   
    With tdfArtiklar
    .Fields.Append .CreateField("ID", dbInteger, 10)
   
    MsgBox .Fields("ID").Attributes 'just to see attribute

    .Fields("ID").Attributes = dbAutoIncrField 'this    
    line generates the error
   
    End With

    dbsQSart.Close

End Sub
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 200 total points
ID: 7015890
I just discovered something: you cannot add an autonumber field using this syntax to an already existing table. This syntax is only working when you create a new table (see Command2_Click).

I found an workaround that will work with existing tables even if they are containing data (see Command1_Click).

Option Explicit

Private Sub Command1_Click()
Dim db As Database
Dim stSQLstr As String
   Set db = OpenDatabase("c:\temp\nwind.mdb")
    stSQLstr = "alter TABLE [Customers] add column emroeauid  COUNTER "
    db.Execute stSQLstr

End Sub

Private Sub Command2_Click()

   Dim db As Database
   Set db = OpenDatabase("c:\temp\nwind.mdb")


Dim tb As TableDef
Dim IDX As Index
Set tb = db.CreateTableDef("MyTableName")
'add fields like this
tb.Fields.Append tb.CreateField("MyField", dbText)
tb.Fields("MyField").AllowZeroLength = True 'if u want

'********
'Optional for add a primary key field to "myTableName" Table
tb.Fields.Append tb.CreateField("ForPrim", dbLong)
tb.Fields("ForPrim").Attributes = dbAutoIncrField
Set IDX = tb.CreateIndex("IDXforPrim")
IDX.Fields.Append tb.CreateField("ForPrim")
IDX.Primary = True
tb.Indexes.Append IDX
'************
'now u add the tb table to your .mdb file
db.TableDefs.Append tb

End Sub


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

829 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