Receipt of Goods Error checking issue

I am working on a script to do a receipt of goods against an existing purchase order.  I have code to verify some data.  Here is a code snippet:

		' Process record
		If GoodRecord = 1 Then
			If LastPONum <> PurchaseOrderNumber Then
				LastPONum = PurchaseOrderNumber
				retVal = oRcpt.nSetKeyValue("ReceiptType$","G")
				retVal = oRcpt.nGetNextReceiptNo(ReceiptNo)
				If retVal <> 1 Then
					GoodRecord = 0
					ErrorMsg = ErrorMsg & ErrHeader & "Cannot get next receipt number, Error: " & oRcpt.sLastErrorMsg & Chr(13) & Chr(10)
				Else
					NewReceipt = 1
				End If
			End If
			
			If GoodRecord = 1 Then
				retVal = oRcpt.nSetKeyValue("ReceiptNo$",ReceiptNo)
				retVal = oRcpt.nSetKey()
				If retVal = 0 Then
					GoodRecord = 0
					ErrorMsg = ErrorMsg & ErrHeader & "Cannot create receipt, Error: " & oRcpt.sLastErrorMsg & Chr(13) & Chr(10)
				Else	
					If NewReceipt = 1 Then
						retVal = oRcpt.nSetValue("ReceiptDate$",ReceiptDate)
						retVal = oRcpt.nSetValue("PurchaseOrderNo$",PurchaseOrderNumber)
						If retVal <> 1 Then
							GoodRecord = 0
							ErrorMsg = ErrorMsg & ErrHeader & "Cannot set PO No on Receipt, Error: " & oRcpt.sLastErrorMsg & Chr(13) & Chr(10)
						Else
							retVal = oRcpt.oLines.nCopyPurchaseOrderLines(PurchaseOrderNumber,0)	
							If retVal <> 1 Then
								GoodRecord = 0
								ErrorMsg = ErrorMsg & ErrHeader & "Cannot copy PO lines, Error: " & oRcpt.oLines.sLastErrorMsg & Chr(13) & Chr(10)
							Else						
								NewReceipt = 0
							End If
						End If
					End If
				End If
			End If

			If GoodRecord = 1 Then
				retVal = oRcpt.oLines.nMoveFirst()
				LineFound = 0
				Do While (Not oRcpt.oLines.nEOF And LineFound = 0)
					retVal = oRcpt.oLines.nGetValue("OrderLineKey$",PO_OrderLineKey)
					retVal = oRcpt.oLines.nGetValue("ItemCode$",PO_ItemCode)
					retVal = oRcpt.oLines.nGetValue("WarehouseCode$",PO_WhseCode)
					If PO_OrderLineKey = LineKey And PO_ItemCode = ItemCode Then
						LineFound = 1
						If PO_WhseCode <> ToWarehouseCode Then  ' I AM TESTING THIS PART, SO THIS IS TRUE
							GoodRecord = 0
							ErrorMsg = ErrorMsg & ErrHeader & "Warehouse Code mismatch, SVMX: " & ToWarehouseCode & ", Sage: " & PO_WhseCode & Chr(13) & Chr(10)
						Else
							retVal = oRcpt.oLines.nSetValue("QuantityReceived",QuantityReceived)
							retVal = oRcpt.oLines.nWrite()
							If retVal = 0 Then
								GoodRecord = 0
								ErrorMsg = ErrorMsg & ErrHeader & "Error writing Receipt Line, Error: " & oRcpt.oLines.sLastErrorMSg & Chr(13) & Chr(10)
							Else
								retVal = oRcpt.nWrite()
								If retVal = 0 Then
									GoodRecord = 0
									ErrorMsg = ErrorMsg & ErrHeader & "Error writing Receipt, Error: " & oRcpt.sLastErrorMSg & Chr(13) & Chr(10)
								Else
									GoodRecordCount = GoodRecordCount + 1
									LastPONum = PurchaseOrderNumber
								End If
							End If
						End If
					End If
					retVal = oRcpt.oLines.nMoveNext()
				Loop
				If LineFound = 0 Then
					GoodRecord = 0
					ErrorMsg = ErrorMsg & ErrHeader & "PO Line not found" & Chr(13) & Chr(10)
				End If
			End If
		End If

What I am finding is that a record gets written to the PO_ReceiptReturnMatRqByPO file. I need this record to go away if I do not complete the Writes on the Receipt. What can I do to accomplish this?