Solved

Creating a dbfield in Access with VB

Posted on 2002-05-16
8
153 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
[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
  • 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
Technology Partners: 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!

 
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

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month4 days, 9 hours left to enroll

636 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