Excel や Calc の表からデータベースにデータを移行するマクロを書いていて
表題のようなエラーに出くわした。
原因は分らない。
回避策として OnError 文でリトライを掛けることにした。
一見うまく行ったように見えたのだが、よく調べるとデータの欠落を起こしている
場合もある。仕方ないのでこのエラーがでたときは再度同じファイルを読み込ま
せる様に運用することにした。
トランザクションにしてロールバックすればよりよい対処ができるのだろうが。
めんどい。
[変更前]
function insertDetailTable(recset as object, sheet as object, file_id as integer, bill_id as integer , row as integer)
C = recset.Columns
row_index = row +1
rem_cell = sheet.getCellByPosition(1,row) ' B:摘要
qty_cell = sheet.getCellByPosition(6,row) ' G:数量
price_cell = sheet.getCellByPosition(7,row) ' H:単価
amount_cell = sheet.getCellByPosition(8,row) ' I:金額
item_name = convertCharCode(rem_cell.string)
quantity = qty_cell.value
unit_price = price_cell.value
amount = amount_cell.value
recset.moveToInsertRow()
recset.updateInt(C.findColumn("BILL_ID"), bill_id)
recset.updateInt(C.findColumn("FILE_ID"), file_id)
recset.updateInt(C.findColumn("ROW"), row_index)
recset.updateString(C.findColumn("ITEM_NAME"), item_name)
recset.updateDouble(C.findColumn("UNIT_PRICE"), unit_price)
recset.updateDouble(C.findColumn("QUANTITY"), quantity)
recset.updateDouble(C.findColumn("AMOUNT"), amount)
recset.insertRow() ' ここでエラーが出てくる
insertDetailTable = recset.getInt(C.findColumn("ID"))
end function
[変更後]
function insertDetailTable(recset as object, sheet as object, file_id as integer, bill_id as integer , row as integer)
errcnt = 0
ErrorRetry:
C = recset.Columns
row_index = row +1
rem_cell = sheet.getCellByPosition(1,row) ' B:摘要
qty_cell = sheet.getCellByPosition(6,row) ' G:数量
price_cell = sheet.getCellByPosition(7,row) ' H:単価
amount_cell = sheet.getCellByPosition(8,row) ' I:金額
item_name = convertCharCode(rem_cell.string)
quantity = qty_cell.value
unit_price = price_cell.value
amount = amount_cell.value
recset.moveToInsertRow()
recset.updateInt(C.findColumn("BILL_ID"), bill_id)
recset.updateInt(C.findColumn("FILE_ID"), file_id)
recset.updateInt(C.findColumn("ROW"), row_index)
recset.updateString(C.findColumn("ITEM_NAME"), item_name)
recset.updateDouble(C.findColumn("UNIT_PRICE"), unit_price)
recset.updateDouble(C.findColumn("QUANTITY"), quantity)
recset.updateDouble(C.findColumn("AMOUNT"), amount)
on error goto ErrorHandler
recset.insertRow()
insertDetailTable = recset.getInt(C.findColumn("ID"))
exit function
ErrorHandler:
on error goto 0
if (errcnt < 5) then
errcnt = errcnt+1
resume ErrorRetry
else
msgBox err &":"& error(err) &chr(13) _
& "TAB:"& sheet.Name &chr(13) _
& "ROW:"& row_index &chr(13) _
& item_name &" / "& quantity &"*"& unit_price &chr(13) _
& "この行はデータベースに追加できませんでした。"
insertDetailTable = -2
exit function
endif
end function