I’m trying to figure out the best way to reference a control on a form from within a module. Unfortunately the form is just an instance, so it’s not as easy as calling form.control…
Dim ChildForm As New frmSearch
' Make it a child of this MDI form before showing it. '
ChildForm.MdiParent = Me
ChildForm.Show()
That form has an option for printing, which calls another form where certain options are chosen. The print form in turn calls a function in a module, which tries to reference back to the origional form.
childform as new frmSearch -> frmPrintForm -> sub okToPrint (in module Print)
okToPrint tries to reference a listview on frmSearch, but can’t find it.
For Each itmX In frmSearch.lstResults.Items
So the solutions I can think of off the top of my head are:
1. Somehow divine which form is the caller of frmPrintForm
2. Pass ChildForm to the frmPrintForm as a variable to be passed on to module Print..
3. Use frmSearch directly instead of using an instance of it.
Number 1 would be my preference, as I don’t want to have to pass forms around like that.
I’m hesitant to suggest this, because this is “asking for downvotes”, but I’ll mention it anyway for sake of completeness: If you ensure that there is always only one search form on display, you could globally store a reference to the Search form. (Yes, I said “global variable”, now go ahead and downvote. :-P)
This will allow you to access the last opened search form with
frmSearch.Current. Note that this solution has all the drawbacks usually associated with global variables, but I guess it most closely resembles what you are used to in VB6.The code above is to give you the general idea, there is lots of room for improvement (make the constructor private and use a
Sharedmethod instead which ensures that no second instance of the form is opened; setcurrentSearchFormtoNothingwhen the form is closed, etc.).Just to mention this once more, passing the relevant data to the print form is a much cleaner solution, but in the end, the decision is yours, and you should know about all available options.