Solved

Creating a dbfield in Access with VB

Posted on 2002-05-16
8
146 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
ID: 7013486
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
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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7013559
your field needs to be numeric.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 46
Run code from text file in vb 1 56
Copy a row 12 57
How to measure sizes and angles in scanned images ? 3 53
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

910 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

27 Experts available now in Live!

Get 1:1 Help Now