Solved

Create Column in a remote table programmatically

Posted on 2002-04-13
9
400 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can you open the FORM2 2 32
Access 2016 7 35
Create tables in access db (2016)  using vba 13 42
VBA to copy the database back end to a new database file. 3 35
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

810 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