Q(uick)BASIC Statement: BEGINTRANS

Quick View

BEGINTRANS Statement

BEGINTRANS indicates the beginning of an ISAM transaction

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • BEGINTRANS
Description/Parameter(s)

BEGINTRANS indicates the beginning of a transaction (a series of ISAM database operations).

Usage Notes

  • Transactions are a way to group a series of ISAM operations so that you can commit them as a whole, rescind them all, or rescind operations since a designated savepoint.
  • Use the COMMITTRANS statement to commit a transaction.
  • Use the SAVEPOINT function to designate a savepoint.
  • Use the ROLLBACK and ROLLBACK ALL statements to rescind all or part of a transaction's operations.
  • If you attempt to use BEGINTRANS when there is already a transaction pending, BASIC generates the error message, "Illegal function call."

Programming Tips

  • Any ISAM operation that closes a table causes transactions to be committed. For example, if a type mismatch occurs while you are opening an ISAM table, the table is closed and a pending transaction is committed.
  • You may wish to code your programs so they first open all tables, then perform all transactions, then close tables. Make sure any operation that can close a table occurs outside a transaction.
Example

This example uses the DELETE statement to remove records from an ISAM file. It creates a transaction with the BEGINTRANS and COMMITTRANS statements, and uses the SAVEPOINT function and ROLLBACK statement within the transaction to provide rollback of any or all of the deletions.

Note: To run this program, you must load the ISAM TSR program PROISAM.EXE. Also, this program assumes a file called BOOKS.MDB exists in the current directory. BOOKS.MDB is a sample ISAM file that SETUP copies to your disk.

DEFINT A-Z TYPE Borrower Cardnum AS LONG 'Card number. TheName AS STRING * 36 'Name. Street AS STRING * 50 'Street. City AS STRING * 26 'City. State AS STRING * 2 'State. Zip AS LONG 'Zip code. END TYPE DIM People AS Borrower 'Record structure variable. CONST Database = "BOOKS.MDB" 'Name of the disk file. CONST Tablename = "Cardholders" 'Name of the table. CONST viewbottom = 17, viewtop = 3, msgtxt = " *** Deleted: Savepoint " DIM SavePts(viewbottom - viewtop + 1) TableNum = FREEFILE OPEN Database FOR ISAM Borrower Tablename AS TableNum 'Loop until user chooses to quit. DO VIEW PRINT CLS : COLOR 15, 0 PRINT SPC(34); "Card Holders" PRINT "Card#"; SPC(2); "Name"; SPC(13); "Street"; PRINT SPC(20); "City"; SPC(6); "State"; SPC(2); "Zip" LOCATE 24, 1: PRINT "Choose a key: "; PRINT "D - Delete this record N - Next record Q - Quit"; MOVEFIRST TableNum VIEW PRINT viewtop TO viewbottom: COLOR 7, 0: CLS vPos = viewtop 'Loop through a screenful of records. DO 'For each record, display and ask user what to do with it. RETRIEVE TableNum, People LOCATE vPos, 1 PRINT USING ("#####"); People.Cardnum; PRINT " "; LEFT$(People.TheName, 15); " "; PRINT LEFT$(People.Street, 25); " "; PRINT LEFT$(People.City, 10); " "; People.State; " "; PRINT USING ("#####"); People.Zip 'Get keystroke from user. validkeys$ = "DNQ" DO keychoice$ = UCASE$(INKEY$) LOOP WHILE INSTR(validkeys$, keychoice$) = 0 OR keychoice$ = "" 'Process keystroke. SELECT CASE keychoice$ CASE "D" IF NOT inTransaction THEN inTransaction = -1 BEGINTRANS END IF NumSavePts = NumSavePts + 1 SavePts(NumSavePts) = SAVEPOINT DELETE TableNum LOCATE vPos, 7: PRINT msgtxt; NumSavePts; SPC(79 - POS(0)); CASE "Q" EXIT DO CASE "N" MOVENEXT TableNum END SELECT vPos = vPos + 1 LOOP UNTIL EOF(TableNum) OR vPos = viewbottom 'If user didn't delete any records, simply quit. IF NumSavePts = 0 THEN EXIT DO 'Allow user to commit deletions, or roll back some or all of them. VIEW PRINT: LOCATE 24, 1: PRINT "Choose a key: "; PRINT "R - Rollback to a savepoint A - Rollback all deletions" PRINT SPC(17); "Q - commit deletions and Quit"; validkeys$ = "RAQ" DO keychoice$ = UCASE$(INKEY$) LOOP WHILE INSTR(validkeys$, keychoice$) = 0 OR keychoice$ = "" SELECT CASE keychoice$ CASE "R" VIEW PRINT 24 TO 25: PRINT : PRINT DO PRINT "Roll back to which savepoint ( 1 -"; NumSavePts; ")"; INPUT RollbackPt LOOP UNTIL RollbackPt > 0 AND RollbackPt <= NumSavePts ROLLBACK SavePts(RollbackPt) NumSavePts = RollbackPt - 1 CASE "A" NumSavePts = 0 ROLLBACK ALL CASE "Q" EXIT DO END SELECT LOOP IF inTransaction THEN COMMITTRANS CLOSE END