?
Solved

Create Column in a remote table programmatically

Posted on 2002-04-13
9
Medium Priority
?
431 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
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.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

764 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