DOCUMENT:Q316910 11-MAR-2002 [foxpro] TITLE :HOWTO: Create an Auto-ID Field in a FoxPro DBF via ADO and ASP PRODUCT :Microsoft FoxPro PROD/VER::7.0 OPER/SYS: KEYWORDS:kbInternet kbGrpDSFox kbDSupport kbCodeSnippet kbvfp700 ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual FoxPro for Windows, version 7.0 ------------------------------------------------------------------------------- SUMMARY ======= This article describes how to create an auto-incrementing field in a Visual FoxPro (VFP) table by using Active Server Pages (ASP) and ActiveX Data Objects (ADO). This article assumes that you have a good working knowledge of Visual FoxPro and are comfortable creating and modifying databases. It also assumes you can use Internet Information Server (IIS) Administrator to create virtual directories and that you can assign user rights on files and folders under Microsoft Windows NT, Microsoft Windows 2000, and Microsoft Windows XP. In order to use this sample, you need Visual FoxPro 7.0 and a computer that is running both IIS and ADO. The IIS computer must also have the VFP 7.0 OLEDB provider installed and properly registered. MORE INFORMATION ================ This demonstration has three parts: - Create the VFP data files - Create the ASP page - Test the sample Create the VFP Data Files: 1. Using Windows Explorer, create a new directory on the local hard disk of a computer that is running Visual FoxPro 7.0. 2. Open VFP 7.0 and change directory (CD command) to the new directory that you just created. 3. Run the following code in VFP 7.0 to create a .dbc file and two tables: CLOSE DATABASES ALL CREATE DATABASE 'SAMPLEDBC.DBC' CREATE TABLE 'TABLE1.DBF' NAME 'TABLE1' (UID I NOT NULL DEFAULT newid("TABLE1"), ; FNAME C(10) NOT NULL, ; LNAME C(10) NOT NULL) CREATE TABLE 'IDS_TABLE.DBF' NAME 'IDS_TABLE' (TABLENAME C(25) NOT NULL, ; CURRENTKEY I NOT NULL) SET COLLATE TO 'MACHINE' INDEX ON UPPER(TABLENAME) TAG TNAME INSERT INTO IDS_TABLE VALUES("TABLE1",0) CLOSE DATABASES ALL 4. Modify the database and open the Stored Procedures window. Paste the following code in the Stored Procedures window: FUNCTION NewID(tcAlias) LOCAL lcAlias, ; lnID lcAlias = UPPER(ALLTRIM(tcAlias)) lnID = 0 SET REPROCESS TO AUTOMATIC IF !USED("IDS_TABLE") USE SAMPLEDBC!IDS_TABLE IN 0 ENDIF SELECT IDS_TABLE IF SEEK(lcAlias, "IDS_TABLE", "Tname") IF RLOCK() lnID = IDS_TABLE.CurrentKey REPLACE IDS_TABLE.CurrentKey WITH IDS_TABLE.CurrentKey + 1 UNLOCK ENDIF ENDIF RETURN lnID ENDFUNC You now have a database named SAMPLEDBC that contains two tables named TABLE1 and IDS_TABLE. TABLE1 is the table that you will insert records into by using ASP and ADO. The IDS_TABLE table is used to keep track of the last unique ID in the TABLE1 table. TABLE1 has three fields: UID, FNAME and LNAME. UID is the unique ID field and will be automatically incremented through the stored procedure and a default value. When a new record is inserted into this table by way of ASP and ADO, the UID field is not included in the fields list. This value is generated by a default value for the field. The default value is actually a call to the stored procedure entered earlier. This stored procedure, newid("TABLE1"), returns a unique number that is subsequently added to the UID field when the new record is saved. Create the ASP Page: Now we'll create an ASP page to add records to the TABLE1 table. 1. Open the text editor of your choice (NotePad is fine) and paste the following code into a new file: <% DataPath = "YOUR DIRECTORY PATH\SAMPLEDBC.DBC" SET oConn = CREATEOBJECT("ADODB.Connection") SET oRS = CREATEOBJECT("ADODB.RECORDSET") oConn.OPEN("PROVIDER=VFPOLEDB.1;Data Source=" & DataPath) oConn.Execute("Insert into Table1 (FName, Lname) VALUES ('Test','Guy')") oRS.OPEN "SELECT * FROM TABLE1", oConn %>
<% = UCASE(oRS(i).Name) %> | <% Next %>
<% = oRS(i) %> | <% Next %>