Solved

Create Column in a remote table programmatically

Posted on 2002-04-13
9
378 Views
Last Modified: 2006-11-17
I am accessing an Access database from FoxPro.  I need to add a column to one of the Access tables programmatically and make it an autonumber type field.  I've worked with Word object model and am familiar with concepts; but I have very little experience with Access.
0
Comment
Question by:vfper
  • 6
  • 3
9 Comments
 
LVL 3

Expert Comment

by:Bob Scriver
ID: 6938799
The following is the code I would use from within ACCESS to do what you want.  

Dim MyWS As Workspace
Dim MyDB As Database
Dim MyTbl As TableDef
Dim MyFld As Field

Set MyWS = DBEngine.Workspaces(0)

Set MyDB = MyWS.OpenDatabase("\\" & vServerID & "\pathfolders. . .\a\b\c\targetdb.mdb", True, False)

Set MyTbl = MyDB!tblTargetTable
Set MyFld = MyTbl.CreateField("RecCounter", DB_Auto)
MyFld.OrdinalPosition = 0
MyTbl.Fields.Append MyFld
MyWS.close
MyDB.close

Hope this helps

Bob Scriver
0
 

Author Comment

by:vfper
ID: 6938811
I need to do outside of ACCESS.
0
 
LVL 3

Expert Comment

by:Bob Scriver
ID: 6938846
You create a seperate ACCESS database file and have already described Module created in that database.  Then create an AutoExec Module that opens the database with the code, executes the table update as described and closes the database.  

As I am not versed in FoxPro you will have to make the OpenDatabase command from within FoxPro.  

Bob scriver

0
 

Author Comment

by:vfper
ID: 6938926
I created a VB app and used the code in your first comment.  I'm having a problem with the following line:

Set MyFld = MyTbl.CreateField("RecCounter", DB_Auto)

DB_Auto is unknown.  I can't find this in the ACCESS or VB documentation
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:Bob Scriver
ID: 6938964
Sorry about that.  You must set the field to DB_Long and then add an addition line for the autoincrement attribute:

Set MyFld = MyTbl.CreateField("RecCounter", DB_Auto)
MyFld.Attributes = DB_AUTOINCRFIELD

If you need to set this as the primary index we can do that also  Just get back to me.

This should do the trick for you.

Bob Scriver
0
 
LVL 3

Accepted Solution

by:
Bob Scriver earned 100 total points
ID: 6938967
Boy, having a bad day today.  Now just paste these two lines over the Set MyFld line and you should have your new field.

Set MyFld = MyTbl.CreateField("RecCounter", DB_Long)
MyFld.Attributes = DB_AUTOINCRFIELD

Bob Scriver
0
 

Author Comment

by:vfper
ID: 6938975
Great! Wonderful!  This learning curve is costing me major hours.  Really appreciate your help.
0
 
LVL 3

Expert Comment

by:Bob Scriver
ID: 6938981
Glad I could help.

Bob
0
 
LVL 3

Expert Comment

by:Bob Scriver
ID: 6939002
Below is the statement that you would add at the end to create the Unique index on the new field:

MyDB.Execute "CREATE UNIQUE INDEX RecCounter ON tblTargetTable(RecCounter);"

Otherwise, the index property of the RecCounter field will say Duplicates OK which isn't what you want but because they are being updated only by the system may not be a problem.

If you want the new field to be the primary index also then use the following:

MyDB.Execute "CREATE UNIQUE INDEX RecCounter ON tblTargetTable(RecCounter) WITH Primary;"

This last one would have to be used with care as there probably already is another Primary Index that would have to be dealt with.  You may or may not want to change it.

Good luck.

Bob Scriver
0

Featured Post

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.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

759 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

20 Experts available now in Live!

Get 1:1 Help Now