How to get all opened forms call Sub with same name under each form in VB.NET - vb.net

I have an application with several forms(frm1, frm2...). Under each form there is a sub method: sub1. Is that possible to create a method in module to get all opened forms and made then run frm1.sub1, frm2.sub1...?
I already know that My.Application.OpenForms can find all opened form. But how to use this for next step? Since there are bunch of forms I can't name the form like this one by one.
'$'
Dim frm As Form
For Each frm In My.Application.OpenForms
If frm Is My.Forms.TheFormINeed Then
'do something
end if
This may sounds newbie. Please help me.

If all the subs you want to run are present in all the forms, then
For Each frm In My.Application.OpenForms
CallByName(frm, "sub1", CallType.Method, Nothing)
Next
should do the trick.
Bear in mind of course that your main form may not have the sub, so you can use If..Then etc to include or exclude particular forms of course.
You can name forms by the way by setting the.Name property

Related

Access an Collection within a form via VBA out of a Class-module

I just like to reorder the VBA of a bunch of Forms in Access, doing all nearly the same. So I created a class clsPopup and I just want to pass some collections out of all this Forms into the class, so I can access their entries over there or in there or how ever.
I could pass the Form-element of the Popup to the class as I created a sub named Load in clsPopup like
Dim m_frm As Form
sub Load(frm As Form)
Set m_frm = frm
debug.print m_frm.colSp("Name")
end sub
In the Form I tried
Dim m_clsPopup As clsPopup
Dim colSp As Collection
sub Form_Load()
Set m_clsPopup = New clsPopup
Set colSp = New Collection
colSp.Add "SomeString", "Name"
m_clsPopup.Load Me.Form
end sub
At this point I got the 2465 Runtime error in the line of m_clsPopup.Load Me.Form.
My main idea is, to just collect all the needed data within the Forms as collections and than I easily could work with them in the class.
Of course I thought of arrays, but collections seems so much more handy and I could avoid some terrible indexing.
Ahh, and it needs to work in Access 2010. Might that be the problem?
I feel like just a tiny pice of code is missing. Could anyone help to create nice code out of a masterpiece of redundancy?

Declaring WebBrowser in VB.NET Other Form

I am working on a project that has WebBrowsers in Other Forms;
I wrote the code below to control these WebBrowsers; but I need the code to recognize (Declare) the WebBrowsers of these forms.
Dim openForm As Form = Nothing
For Index As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
openForm = My.Application.OpenForms.Item(Index)
If openForm IsNot Me Then
MyWebBrowser.navigate("http://www.google.com/") ' PROBLEM IN THIS LINE
End If
Next
My Module created them as below:
Module MMMBrowser
Dim frmNew As New Form
Dim MekdamBrowser As New WebBrowser
Other info gleaned from comments:
there is form factory of some sort which creates new frmNew
there are many of these open at a time, which is the reason for the backwards loop thru OpenForms to find the last one.
The MekdamBrowser reference is an attempt to refer to the browser on the form.
The easy things is to provide a way for outsiders to tell the form to navigate somewhere using a new Sub, and let the form drive the browser control. This probably eliminates the need for a global MekdamBrowser reference. In the browser form add something like this:
Public Sub GotoNewURL(url As String)
myWebBrowserName.navigate(url)
End Sub
This procedure only exists for Form1 not the generic Form type, so we need to change how you find the form to use. Your existing loop is wonky. It will only ever find the last instance of a form which is not the current form. If you add a third form type, it wont work well:
Dim lastBrowserFrm As Form1 ' use the class name!
' this will try to get the last Instance of Form1
lastBrowserFrm = Application.OpenForms.OfType(Of Form1)().LastOrDefault
' LastOrDefaultcan return nothing if there are none,
' so test
If lastBrowserFrm IsNot Nothing Then
lastBrowserFrm .GotoNewUrl("www.stackoverflow.com")
Else
' create a new one, I guess
End If
Your loop was not considering that there could be other form types in the collection which are not Form1 or even if a new browser form was the last one created! This is more important now because GotoNewURL is only available on Form1 instances.
I changed the name to lastBrowserFrm to reflect what is really going one - it will just find the last one created. If you are trying to work with a specific instance, you need to provide a way to track the ones you create such as with a List(of Form1) or use the Name property so you can tell one from the other. As is, you do not a way to get back a specific form instance.

VBA Listbox becomes unresponsive after first use

I have a VBA (Excel 2010) system which involves selecting an item from a listbox and then displaying it in another form. Here is a very simplified version of what happens.
' Part of frmForm1 code module
sub lstListbox_Click
dim MyEvent as string
dim i as integer
i=me.lstListbox.listindex
MyEvent=me.lstlistbox.list(i)
' Now show the item in the second form
Load frmForm2
me.hide
ThisWorkbook.LoadDataIntoForm2 (frmForm2, MyEvent)
frmForm2.show
unload frmForm2
me.show
end sub
The listbox accepts the click, and first the event (the event handler is giver above). Key parts of the event handler are:
Load the second form (to display the detail data)
Pass the second form as a UserForm parameter to a procedure (LoadDataIntoForm2)
Hide the host form (frmForm1) and show the second form (frmForm2)
When the second form processes an Exit click, the code looks like this:
' Part of frmForm2 code module
sub cmdExit_Click
me.hide
end sub
The first time round it works fine - but when I return to frmForm1 (in the tail end of the lstListBox_Click procedure), even though the rest of the form is operative, the listbox remains stubbornly unresponsive.
I've managed to abstract this down to a little demo system if that would help - the same behavior is seen there. (It's regular .xls file, but that seems not to be easily acceptable as an upload)
Has anyone seen this before? And does anyone have any ideas how I might get this to work the way I want it to?
Thanks,
Tony
The default for the .Show method is to make the form modal. Explicitly set it to modeless:
Sub lstListbox_Click
...
Me.Show vbModeless
End Sub

how to close a form with a string

Hey guys I have got to a head scratcher well at least for me any way. I need to find a way of opening a form with a string. I have got this ...
Dim asm = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes As Type() = asm.GetTypes()
Dim frm As Form
For Each t As Type In myTypes
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso Me.Label4.Text = t.Name Then
frm = CType(Activator.CreateInstance(t), Form)
frm.Close()
frm.Hide()
End If
Next
But it doesn't close the program or even hide it i have no clue?
Question: "I need to find a way of opening a form with a string"
Thanks in advance.
That looks to me like you created a new form instance of that type and tried to close/hide it, but I don't see it ever being shown.
If you are trying to close an existing form, then you don't want to create a new instance using Activator.CreateInstance. Rather you need to somehow locate the existing instance of the form that is already open, and close that specific instance.
The code you posted approximates code that would create a new instance of a form by type name, then close/hide the form.
(If you wanted to close an already-open form by name, I would do:
For Each f As Form In My.Application.OpenForms
If f Is My.Forms.NameOfFormThatIWantToClose Then f.Close()
Exit For
)
But I thought you wanted to open a new form by name. If so, you will need to use reflection. This page seems to do exactly what you want.

How to hide/Unhide a MS Access DB form using vb.net

If a form in Access DB is set as hidden. Then how to unhide it? so that we can manipulate the form programmentically using vb.net.
Thank you.
I cannot help with .net, in VBA:
Sub FormHidden()
Dim frm
For Each frm In CurrentProject.AllForms
SetHiddenAttribute acForm, frm.Name, False
Next
End Sub
The code Remou gave should work for unhiding forms whose properties have been changed to HIDDEN in the Access UI.
In VBA, to simplify Remou's example, that would be:
SetHiddenAttribute acForm, "MyHiddenForm", False
You may be required to automate Access from VB.NET in order to accomplish this, but SetHiddenAttribute is a method of the top-level Access application object, so ought to be fairly simple to use. The value of the VBA named constant acForm is 2, so you'd likely have to call that literally, something like this:
app.Application.SetHiddenAttribute 2, "MyHiddenForm", False
where the app object has been initialized as an Access application. Dunno how that's done in VB.NET, but in VBA it would be something like:
Set app = CreateObject("Access.Application")
I'm not sure if the correct syntax would be app.Application.SetHiddenAttribute or if it would be just app.SetHiddenAttribute, but you could easily try either one.
But keep in mind, it was Remou who gave the correct answer. I'm only speculating on how to make it work in a programming environment I don't even use!
Does the form have Visible property? You can set to true to make the form visible.
Do you mean the form is open but not visible, or that the form's meta properties are set to not visible? The later is something you shouldn't do, as items set with visible off will be deleted the next time the database is compacted.