vba - Why do I get an error that the database connection is still open when I load the second userform? -


i creating couple userforms in order pull data small local microsoft access database , create dynamic invoices (and other forms) in excel. in first userform, pull information out of database in order populate comboboxes data validation (user can select available options). close of connections after using them, when click button triggers second userform, error userform_initialize() cannot run because connection still open. i'm not sure how correctly close previous connection before opening new 1 in initialization subroutine.

cnn , rst set in module global variables , getbrand , getcustomer global functions return value of variables in order pass values sql queries. else should in these blocks of code.

code first userform:

private sub userform_initialize() dim cnn new adodb.connection dim rst new adodb.recordset cnn.cursorlocation = aduseclient cnn.connectionstring = "provider=microsoft.jet.oledb.4.0; data source=c:\users\user\desktop\test.mdb" cnn.open rst.activeconnection = cnn  'populate customer combobox rst.open "select customer customer order customer" rst.movefirst me.customerbox     .clear             .additem rst![customer]         rst.movenext     loop until rst.eof end rst.close  'populate brand combobox rst.open "select distinct brand brand order brand" rst.movefirst me.brandbox     .clear             .additem rst![brand]         rst.movenext     loop until rst.eof end  rst.close cnn.close  end sub  private sub commandbutton1_click() customer = me.customerbox.value brand = me.brandbox.value numofsku = me.qtysku.value piwiz.show vbmodeless unload me end sub   private sub commandbutton2_click() unload me end sub 

and here's code second userform:

private sub userform_initialize()  'generate controls in window based on number of skus entered dim top integer dim ccntrl control dim ecntrl control dim fcntrl control dim gcntrl control dim hcntrl control dim icntrl control = 1 numofsku     d = 1 numofsku         set ccntrl = controls.add("forms.combobox.1", "", true)         set ecntrl = controls.add("forms.label.1", "", true)         set fcntrl = controls.add("forms.combobox.1", "", true)         set gcntrl = controls.add("forms.label.1", "", true)         set hcntrl = controls.add("forms.textbox.1", "", true)         set icntrl = controls.add("forms.label.1", "", true)         ccntrl             .name = "sku" & d             .width = 90             .height = 18             .top = 5 + (d * 25)             .left = 60             .zorder (0)         end         ecntrl             .caption = "sku " & d & ":"             .width = 35             .height = 25             .top = 5 + (d * 25)             .left = 25             .zorder (0)         end         fcntrl             .name = "asst" & d             .width = 45             .height = 18             .top = 5 + (d * 25)             .left = 190             .zorder (0)         end         gcntrl             .caption = "asst: "             .width = 30             .height = 25             .top = 5 + (d * 25)             .left = 160             .zorder (0)         end         hcntrl             .name = "qty" & d             .width = 45             .height = 18             .top = 5 + (d * 25)             .left = 280             .zorder (0)         end         icntrl             .caption = "qty: "             .width = 30             .height = 25             .top = 5 + (d * 25)             .left = 250             .zorder (0)         end     next d     top = ccntrl.top next  'open database connection dim cnn new adodb.connection dim rst new adodb.recordset cnn.connectionstring = "provider=microsoft.jet.oledb.4.0; data source=c:\users\user\desktop\test.mdb" cnn.open rst.activeconnection = cnn  'begin populating these comboboxes while using d counter d = 1 numofsku rst.open "select mastersku brand brand = " & "'" & getbrand & "'"     rst.movefirst     me.controls("sku" & d)         .clear                     .additem rst![mastersku]             rst.movenext         loop until rst.eof     end rst.close next d cnn.close  end sub 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -