Kevin Cully wrote:
> Just to tack on some more Foxpro talk, I prefer to use the SCATTER NAME
> / INSERT FROM NAME methodology, as a way of getting away from APPEND
> BLANK. If the GATHER fails for whatever reason, many times you're left
> with a record that blocks the next APPEND BLANK.
>
> SELECT table2
> SCATTER NAME loMyRecord FIELDS EXCEPT Voucher
> INSERT INTO table1 FROM NAME loMyRecord
>
> If the INSERT fails, it won't leave a partial record in the table the
> way the APPEND BLANK would. And just a little bit better:
>
> SELECT table2
> SCATTER NAME loMyRecord FIELDS EXCEPT Voucher
> TRY
> INSERT INTO table1 FROM NAME loMyRecord
> CATCH
> && Something bad happened.
> THIS.lError = .T.
> THIS.cErrorMsg = [Failed trying to insert a record into table1.]
> && More error handling can go here too.
> ENDTRY
Ditto. It's also better (imo) for indexes too, as the APPEND BLANK
updates the index(es), then the GATHER updates the index(es) too.
Here's a copy/paste of my AddRecord method from my framework using GATHER
NAME:
HIDDEN FUNCTION AddRecord_VFP(toRecord as Object, tcTable as String,
toFields as Collection, tlSkipTransaction as Logical) as Integer
* Adds a lumber record to the VFP table and returns primary key.
LOCAL liKey as Integer, loException as Exception, llSuccess as
Logical, ;
lcCmd as String, lvValue as Variant
TRY
lcDeleted = SET("Deleted") && save current delete
setting
lcField = this.GetNonPKField(toFields)
lcWhere = this.CreateWhereClause(toRecord,toFields)
lcSQL = "select " + this.cPKField + " from " + tcTable
IF NOT EMPTY(lcWhere) THEN
lcSQL = lcSQL + " where " + lcWhere
ENDIF && NOT EMPTY(lcWhere)
this.OpenDBCs(.T.)
this.UseCursor(tcTable)
* see if record already exists
SET DELETED OFF && temporarily turn off to see if any
records
existing marked deleted (or even not deleted)
lcSQL = lcSQL + " into cursor curTemp"
&lcSQL
IF USED("curTemp") AND RECCOUNT("curTemp") > 0 THEN &&
already
exists...just return current key
liKey = EVALUATE("curTemp." + this.cPKField)
IF SEEK(liKey,tcTable,this.cPKField) AND
DELETED(tcTable) THEN &&
undelete it if it's marked for deletion
RECALL IN (tcTable)
ENDIF && SEEK(liKey,tcTable,this.cPKField) AND
DELETED(tcTable)
ELSE && doesn't exist...try and add it
IF NOT tlSkipTransaction THEN
this.StartTransaction()
ENDIF
TRY
* dev note: would like to use INSERT
INTO MyTable FROM NAME
toRecord in the future....
INSERT INTO (tcTable) (&lcField) VALUES
(EVALUATE("toRecord." +
lcField))
SELECT (tcTable)
*lcCmd = "GATHER NAME toRecord FIELDS
EXCEPT " + this.cPKField + ",
" + tcKey + " MEMO"
lcCmd = "GATHER NAME toRecord FIELDS "
+
this.oSQL.cUpdatableFieldList + " MEMO"
&lcCmd
liKey = EVALUATE(tcTable + "." +
this.cPKField) && will have new PK
llSuccess = .T.
CATCH TO loException
llSuccess = .F.
this.cErrMsg = loException.Message
FINALLY
IF NOT tlSkipTransaction THEN
IF llSuccess THEN
this.CommitTransaction()
ELSE
this.RollbackTransaction()
ENDIF && llSuccess
ENDIF && NOT tlSkipTransaction
ENDTRY
ENDIF && USED("curTemp") AND RECCOUNT("curTemp") > 0
CATCH TO loException
liKey = -1
this.cErrMsg = loException.Message
FINALLY
SET DELETED &lcDeleted && restore previous setting
ENDTRY
RETURN liKey
ENDFUNC && AddRecord_VFP(toRecord as Object, tcTable as String, tcKey
as String, tlSkipTransaction as Logical) as Integer
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/profox
OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech
Searchable Archive: http://leafe.com/archives/search/profox
This message:
http://leafe.com/archives/byMID/profox/[email protected]
** All postings, unless explicitly stated otherwise, are the opinions of the
author, and do not constitute legal or medical advice. This statement is added
to the messages for those lawyers who are too stupid to see the obvious.