Call Form from another Form

drag1c

Board Regular
Joined
Aug 7, 2019
Messages
96
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi all,

I've created menu in tree view and I'm using it to load another forms.
When I load another form via tree view dblclick event, form loads but if macroes are written in Module and called in form via Application.Run "Module1.NameOfProcedure", that procedure won't run.

Does anyone know how to fix this?

Here are names of form and module so it's easier for you to understand:
frmMainMenu (Main Menu Form)

frmProjectTaskList (Form which is opened by Main Menu Form)
ProjectTaskList (Module which is used to store lot of coding for frmProjectTaskList)


For example, In frmProjectTaskList_Initialization sub, it loads all parts of code where I do .additem in comboboxes, but it doesn't want to run Application.Run "ProjectTaskList.ListAllProjects"

I've tried to switch ListAllProjects to be Public sub and to call it without Application.Run but it also didn't work for run.

Of course, when I run directly frmProjectTaskList, it works just fine.


Please, if you have any ideas, help... I have, like, 20+ different forms which work via ADODB connection to MS Access which I have to load with this Main Menu. I've even coded "Roles" for menu display. It would be such a waste if this can't work...
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Does anyone have idea how to fix this nesting?
 
Upvote 0
I've done tons of debugging and rs! data is visible to debugger, but it's not listed in listbox if I call form with listbox from another form.
I've tried to cancel Module and to write code directly into form where listbox is and still not working if I call form with listbox from another form.

If I call form directly, it works.

Excel will destroy me :D
 
Upvote 0
Here is update:
Code:
Private Sub TreeView1_DblClick()
    Dim selectedNode As MSComctlLib.Node
    Dim formName As String
    
    ' Get the selected node
    Set selectedNode = Me.TreeView1.selectedItem
    
    ' Check if the selected node is a child node and has a form association
    If ChildNodeFormAssociation.exists(selectedNode.key) Then
        formName = ChildNodeFormAssociation(selectedNode.key)
        ' Open the corresponding form (assuming form names are UserForms in Excel)
        OpenUserFormByName formName
    End If
End Sub

Private Sub OpenUserFormByName(formName As String)
    On Error Resume Next
    
    Dim frm As Object
    Set frm = VBA.UserForms.Add(formName)
    
    If Err.Number <> 0 Then
        MsgBox "Failed to open form: " & formName, vbExclamation
    Else
        ' Calculate the position to open formName adjacent to frmMainMenu
        Dim frmMainMenuLeft As Long
        Dim frmMainMenuTop As Long
        
        ' Adjust these values according to your form's size
        frmMainMenuLeft = Me.Left
        frmMainMenuTop = Me.Top
        
        ' Position formName adjacent to frmMainMenu
        frm.StartUpPosition = 0 ' Manual
        
        ' Ensure no space between frmMainMenu and formName
        frm.Left = frmMainMenuLeft + Me.Width
        frm.Top = frmMainMenuTop
        
        ' Show the form
        frm.Show
        
    End If
    
    On Error GoTo 0
End Sub

This is code I'm using to load forms, but it's malfunctioning because of this:
Code:
Set frm = VBA.UserForms.Add(formName)

How to set this properly, but not to add dynamically forms, instead simply to call them?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top