[RESOLVED] Unloading a form from popupmenu that is on another form.
There are two forms. The first one, let's call it form1 is a simple form. The second one, form2, is a hidden one with a menu on it.
When you right-click on the form1, it calls a popupmenu from form2
In the popupmenu that is located in form2, there is a menu "Unload this form".
There also is a module with code:
Code:
Public formFrom as string
The code for showing up the popupmenu when you right click in the form1 is like this:
Code:
Private Sub Form1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 2 Then
Me.PopupMenu form2.menu
formFrom = me.name
Else
End If
End Sub
The code in the "Unload" menu on the form2 is like this:
Code:
Private Sub unload_Click()
Unload formFrom
End Sub
This doesn't work. How can I solve it? Thanks in advance.
Re: Unloading a form from popupmenu that is on another form.
Try this, you could add this to a module to make it public.
Code:
Public formFrom As Form
But I suggest you do it like this (or similar)
Form1 Code
Code:
Private Sub Form1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 2 Then
Me.PopupMenu form2.menu
Form2.SetFormName = Me
Else
End If
End Sub
Note: What i have highlighted in red doesn't look right, I have not played with this but I would not be surprise if it does not work.
Form2 Code
Code:
Private FormName As Form
Private Sub Unload_Click()
Unload FormName
End Sub
Public Property Let SetFormName(Form_Name As Form)
Set FormName = Form_Name
End Property
Re: Unloading a form from popupmenu that is on another form.
In addition to dimming as form you would need to use a set statement to assign the form
Re: Unloading a form from popupmenu that is on another form.
Why not exploit the ActiveForm property of the Screen object instead of dimensioning a public variable? Sort of like this:
Code:
'In Form1
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then PopupMenu Form2.menu
End Sub
Note that there's no 1 in Form_MouseUp.
Code:
'In Form2
Private Sub unload_Click()
Unload Screen.ActiveForm
End Sub
BTW, unloading Forms this way isn't the usual way it's done. Perhaps your program needs to be redesigned?
Re: Unloading a form from popupmenu that is on another form.
Quote:
Try this, you could add this to a module to make it public.
Code:
Public formFrom As Form
But I suggest you do it like this (or similar)
Form1 Code
Code:
Private Sub Form1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 2 Then
Me.PopupMenu form2.menu
Form2.SetFormName = Me
Else
End If
End Sub
Note: What i have highlighted in red doesn't look right, I have not played with this but I would not be surprise if it does not work.
Form2 Code
Code:
Private FormName As Form
Private Sub Unload_Click()
Unload FormName
End Sub
Public Property Let SetFormName(Form_Name As Form)
Set FormName = Form_Name
End Property
This method didn't work. It kept showing the same error that showed with my code "runtime error 91 object variable or with block not set"
Quote:
Why not exploit the ActiveForm property of the Screen object instead of dimensioning a public variable? Sort of like this:
Code:
'In Form1
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then PopupMenu Form2.menu
End Sub
Note that there's no 1 in Form_MouseUp.
Code:
'In Form2
Private Sub unload_Click()
Unload Screen.ActiveForm
End Sub
BTW, unloading Forms this way isn't the usual way it's done. Perhaps your program needs to be redesigned?
Thank you so much. It works. And it didn't create any other problem, because I am using multiple copies of the same form. And no, I can't redesign it because I have already redesigned it a lot of times, but every time a problem would pop up. And this time I decided to just face the problem and solve it.
Re: [RESOLVED] Unloading a form from popupmenu that is on another form.
I'm glad its working now. I just want to add, it won't work if you only copy and past sometimes! My code did work I normally always try my code before posting and it was working 100% no errors. Bonnie's way is more simple to copy and paste I agree :)
But still if you can't get a form to close as you need, please study people's code before copy paste and thinking that it will always fit perfect into your code. You will never go foward in life if you copy and paste. Just my opinion.