Foxpro database Auto ID field

Posted on 2005-03-23
Medium Priority
Last Modified: 2013-12-25

I use vb6 and foxpro database, Pls advice how to do an auto id field.
Question by:aarontham
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
  • 3
LVL 33

Expert Comment

ID: 13614143
Later releases of Visual FoxPro, starting with VFP8 and now VFP9, have added the ability to use AuoIncrementing fields built right into the tables as a newer type of integer field.

Integer (Autoinc) field type
Integer Field Type

How to: Set Autoincrementing Field Values

In previous versions, Fox developers had to roll their own autoincrementing capabilities for their FoxPro tables/.dbfs, if they wanted to go that way.  Here is one example article on how to do that in earlier versions:

An Autoincrementing Class
LVL 33

Accepted Solution

CarlWarner earned 1500 total points
ID: 13618234
Many Fox developers never use an auto-ID capability.  It could be that, pretty unique to the xBASE world of which FoxPro is a part, is the fact that each record/row in a FoxPro table/.dbf has an internal record number that is ordered by when a record was added/inserted to the table.  In a shared data session, it will stay constant and is used heavily by many Fox developers for quick navigation within the table.

One way to get that info out is via a quick SQL query such as the following with VFP:

SELECT *, RECNO() as recnum FROM customers

That will return not only all of the data, but each record number reference in the new field called "recnum" that results by virtue of using the VFP RECNO() function.

I mentioned that it is constant within a shared session because, once that table is PACKed by a maintenance routine in exclusive mode, the records marked for deletion are left out and the record number sequence is changed because records end up missing.  A data maintenance routine could also have the capability of re-ordering the data position by an index order if that is what the Fox developer chose to do.  So, even without deleted records in the mix that get removed by a PACK, most, if not all, record numbers could be different after a maintenance routine.  But, again, that will not affect an active data session by one or more users where the database and its tables are in shared mode.  A PACK command or a maintenance routine that copies the data back in a new order would require EXCLUSIVE use of those tables.  That can't happen if you are still working in the tables.

So, it may be you could find a use for the Fox internal record number now that you know it exists.
LVL 33

Expert Comment

ID: 14012886
Glad I could help.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

765 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