Solved

Create Column in a remote table programmatically

Posted on 2002-04-13
9
410 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

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)

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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