excel vba - Acces VBA: Discard "can't append" message (Primary Key Violation) -
i'm trying create macro in access 2010 opens excel file, runs macro in excel , imports given results. have 2 problems process.
- application.displayalerts = false in excel nevertheless displayalerts keep popping up. need special in macro access?
- alert "can't append due primary key violations" keeps popping up. know problem is, want ignore it. can use
on error resume
? want @ end messagebox the table hasn't append to. possible , can point me in right direction. tried errorhandeling don't know how make message popup @ end without interrupting process.
code:
private sub main_btn_click() dim fileimport(0 3, 0 2) string fileimport(0, 0) = "stock_cc" fileimport(0, 1) = "f:\370\hyperviseur\situatie\macro\stock_getdata.xlsm" fileimport(0, 2) = "getstock" fileimport(1, 0) = "wips_cc" fileimport(1, 1) = "f:\370\hyperviseur\situatie\macro\wips_getdata.xlsm" fileimport(1, 2) = "update" fileimport(2, 0) = "cca_cc" fileimport(2, 1) = "f:\370\hyperviseur\situatie\macro\slacc.xls" fileimport(2, 2) = "read_cca" fileimport(3, 0) = "eps_cc" fileimport(3, 1) = "f:\370\hyperviseur\situatie\macro\eps.xlsm" fileimport(3, 2) = "update" dim integer = 0 ubound(fileimport, 1) runmacroinxcel fileimport(i, 1), fileimport(i, 2) transferspreadsheetfunction fileimport(i, 0), fileimport(i, 1) next end sub private sub runmacroinexcel(fname string, macroname string) dim xl object 'step 1: start excel, open target workbook. set xl = createobject("excel.application") xl.workbooks.open (fname) xl.visible = true xl.run (macroname) xl.activeworkbook.close (true) xl.quit set xl = nothing end sub private sub transferspreadsheetfunction(byval tablename string, byval filename string) if fileexist(filename) docmd.transferspreadsheet acimport, , tablename, filename, true else dim msg string msg = "bestand niet gevonden" & str(err.number) & err.source & err.description msgbox (msg) end if end sub function fileexist(stestfile string) boolean dim lsize long on error resume next lsize = -1 lsize = filelen(stestfile) if lsize > -1 fileexist = true else fileexist = false end if end function
add error handling within loop, concatenate string variable, message box string:
dim integer, failedfiles string failedfiles = "list of failed tables: " & vbnewline & vbnewline = 0 ubound(fileimport, 1) on error goto nextfile call runmacroinxcel(fileimport(i, 1), fileimport(i, 2)) call transferspreadsheetfunction(fileimport(i, 0), fileimport(i, 1)) nextfile: failedfiles = failedfiles & " " & fileimport(i,0) & vbnewline resume nextfile2 nextfile2: next msgbox failedfiles, vbinformation, "failed tables list"
Comments
Post a Comment