Solved

Creating a dbfield in Access with VB

Posted on 2002-05-16
8
145 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 69

Expert Comment

by:Éric Moreau
Comment Utility
using what? DAO? ADO?

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

Expert Comment

by:Éric Moreau
Comment Utility
0
 

Author Comment

by:soft4u
Comment Utility
.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
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
your field needs to be numeric.
0
Free Trending Threat Insights Every Day

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:soft4u
Comment Utility
How do I make field numeric?
0
 
LVL 69

Expert Comment

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

Author Comment

by:soft4u
Comment Utility
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 69

Accepted Solution

by:
Éric Moreau earned 200 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

744 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

19 Experts available now in Live!

Get 1:1 Help Now