Solved

Create Column in a remote table programmatically

Posted on 2002-04-13
9
425 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
[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
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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
 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…

717 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