excel vba - VBA How to properly globally initialize form and get/set values from its controls -
i have problems getting values textboxes form in vba app. in module have logics subs initialize form, presets values in , on:
dim usf new userform1 'in case assume form initialized 'globally, on top of subs, 'files , subs in vba project can acces 'controls in it. , works, every second 'run raises "automation error" sub start() 'when declare form here, program 'start, none of subs can 'acces form controls. of course in case 'must initialize form in 'every single sub. i've tried passing form 'argumend functions, 'this not work or in wrong way. subs 'can't read values form controls. sub 'is presets values in sub , show 'form on error goto errmyerrorhandler 'here part of code reading values sheet 'and inserting values textboxes, labels 'and comboboxes. no problem here. usf.show '//problem here when declare form globally. 'raises automation error exit sub errmyerrorhandler: call errorhandler(err) 'on error go error handling sub show 'messagebox err name end sub public sub startrun(optional x = 0) 'optional not show sub 'in macro's list. sub 'starts when form's button start 'being clicked on error goto errmyerrorhandler dim napzw, udn, str single dim h1, h2 integer usf 'reading text textboxes. it's not napzw = csng(.txtnapzw.text) 'work when form declared locally udn = csng(.txtudn.text) str = csng(.txtstr.text) h1 = cint(.cbx1h.text) h2 = cint(.cbx2h.text) end call obliczanieip(napzw, udn, str, h1, h2, usf) 'sub reading values 'sheet exit sub errmyerrorhandler: call errorhandler(err) end sub sub obliczanieip(napzw, udn, str, h1, h2, usf) on error goto errmyerrorhandler range("q31").value = napzw range("r31").value = udn range("s31").value = str range("s2").value = h2 range("s4").value = h1 'some code here, checking correctness of inserted values. if not correct, 'shows message box , exits sub. no problem here. usf.lblltr = range("s33").value 'reads sheet values , show usf.lbxtr = range("r33").value 'in labels. again, work, form must usf.lblimax = range("s18").value 'be declared globally usf.lblimediana = range("s22").value usf.lblisrednia = range("s20").value exit sub errmyerrorhandler: call errorhandler(err) end sub public sub errorhandler(err errobject) msgbox err.description & ", " & err.number & " " & err.source, _ vbexclamation + vbokcancel, _ "błąd: " & (err.description) err.clear end 'without end statement program runs once when 'excel initialized or when go vba editor , 'i think ressetting 'things' associated 'this program end sub
in userform1 have logic button event:
private sub btnstart_click() startrun end sub
the problem is, when initialize form in module outside sub, (which think should globally), program works correctly, every second run raises "automation error". when initialize form in sub start restricted access form , other sub cannot access controls in form. , when reinitialize form in every sub needed it, controls resetting. know may silly problem, i'm new in in vba. thank in advance reply.
Comments
Post a Comment