Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Creating a dbfield in Access with VB

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
soft4u
Asked:
soft4u
  • 5
  • 3
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
using what? DAO? ADO?

have a look at
http://www.experts-exchange.com/questions/Q.20146826.html
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
soft4uAuthor Commented:
.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
Industry Leaders: 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!

 
Éric MoreauSenior .Net ConsultantCommented:
your field needs to be numeric.
0
 
soft4uAuthor Commented:
How do I make field numeric?
0
 
Éric MoreauSenior .Net ConsultantCommented:
something like      
.Fields.Append .CreateField("IntegerField", dbInteger)
0
 
soft4uAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now