primary key?  
Author Message
Bobby110uk





PostPosted: Visual FoxPro General, primary key? Top

How do u auto increment a primary key in vfp

Visual FoxPro1  
 
 
David Frankenbach





PostPosted: Visual FoxPro General, primary key? Top

You just INSERT into the table and it happens automatically. For example here's a chunk of code that creates a database of my Windows Media Player music library.

close databases all

create database c:\temp\musiclib
create table c:\temp\band ( iBand i autoinc, cBand c(60) )
create table c:\temp\album ( iAlbum i autoinc, iBand i, cTitle c(80), dAdded d, cGuid c(38), iYear i )
create table c:\temp\songs ( iSong i autoinc, iAlbum i, cTitle c(128), iLength i )
set safety off

lcRootPath = "C:\Documents and Settings\David\My Documents\My Music\"
lnBands = adir( laBands, lcRootPath + "*.*", "D", 1 )

for i = 3 to lnBands
if ( ! "D" $ laBands[i,5] )
loop
endif
if ( "My Playlists" $ laBands[i,1] )
loop
endif
if ( "License Backup" $ laBands[i,1] )
loop
endif

lcBand = alltrim( laBands[i,1] )

insert into band ( cBand ) values ( laBands[i,1] )
lnAlbums = adir( laAlbums, lcRootPath + lcBand + "\*.*", "D", 1 )
for j = 3 to lnAlbums
lcAlbumPath = lcBand + "\" + alltrim( laAlbums[j,1] )

insert into album ( iBand, cTitle, dAdded, cGUID, iYear ) values ( band.iBand, ;
laAlbums[j,1], ;
{}, ;
"", 0 )
lnSongs = adir( laSongs, lcRootPath + lcAlbumPath + "\*.*", "HS", 1 )
ldAdded = date() + 1000
for k = 1 to lnSongs
l = at( "{", laSongs[k,1] )
if ( ( l > 0 ) and empty( album.cGUID ) and ( ".jpg" $ lower( laSongs[k,1] ) ) )
lcStr = substr( laSongs[k,1], l, 38 )
replace album.cGUID with lcStr, dAdded with laSongs[k,3] in album
ldAdded = min( ldAdded, laSongs[k,3] )
else
if ( ( "custom.jpg" $ lower( laSongs[k,1] ) ) and empty( album.cGUID ) )
replace cGUID with "custom.jpg" in album
endif
if ( ( ".wma" $ lower( laSongs[k,1] ) ) or ( ".mp3" $ lower( laSongs[k,1] ) ) )
insert into songs ( iAlbum, cTitle, iLength ) values ;
( album.iAlbum, laSongs[k,1], 0 )
ldAdded = min( ldAdded, laSongs[k,3] )
endif
endif
if ( empty( album.dAdded ) )
replace album.dAdded with ldAdded in album
endif
endfor
endfor
endfor



 
 
Bobby110uk





PostPosted: Visual FoxPro General, primary key? Top

thanx for da help
 
 
Alex Feldstein





PostPosted: Visual FoxPro General, primary key? Top

Autoinc fields are a feature in newer versions (8+ )

Before that you needed to do it in code. Here's a sample counter program to increment PKs.