PDA

View Full Version : Popup UserForm, the custom message box, is not closed.



pmyk
09-05-2019, 10:35 PM
Instead of the VB MsgBox, I have an UserForm entitled frmTelr and on this I have a label with the name lblTelr in which my Alert Message will be displayed for a specific time.
While my main UserForm which is frmHomPg runs, the following code in it will show the frmTelr and the label lblTelr will display this message for 3 seconds.


PopUpNews "The Description column is sorted successfully. Programme continues. Pl. wait!", 3

I have the following code in frmTelr, which is my msgbox.

Private Sub UserForm_Activate()
Dim PauseTime, Start
PauseTime = varDelyr
Start = Timer
Do While Timer < Start + PauseTime
DoEvents
Loop
frmTelr.Hide
End Sub

I have the following code in Module1.


Public varDelyr As Integer
Public varTxtr As String

Function PopUpNews(varTelTxt As String, varTimDelay As Integer)
varTxtr = varTelTxt
varDelyr = varTimDelay
frmTelr.lblTelr.Caption = varTxtr
If frmTelr.Visible = False Then
frmTelr.Show
End If
End Function

This works fine.
But, sometimes after displaying the message the popup userform, which is frmTelr is not closed.

Kindly give me suitable suggestion to close the PopUp.

Kenneth Hobs
09-06-2019, 05:15 AM
frmTelr.Show vbModeless

pmyk
09-06-2019, 10:01 PM
Thanks. I will try.

pmyk
10-24-2019, 09:51 PM
frmTelr.Show vbModeless
@Kenneth Hobs (http://www.vbaexpress.com/forum/member.php?3661-Kenneth-Hobs)
http://www.vbaexpress.com/forum/images/statusicon/user-offline.png
Very sorry for the delay. I was in a project. Only now I could get some time to try your code in my file.
Thanks for the valuable suggestion.
In frmTelr ShowModal property is set toTrue. I used your suggestion. But the problem was not solved. I don't know why.
So I modified my code as given below by hiding it if it is visible and then showing it:


If frmTelr.Visible = False Then
frmTelr.Show 'Display Message for 3 seconds
ElseIf frmTelr.Visible = True Then
frmTelr.Hide
'frmTelr.Show vbModeless 'Display Message for time delay seconds
frmTelr.Show 'Display Message for time delay seconds
End If

Now it works fine.
But I would like to know, if you have time, why the vbModeless command failed.

Artik
10-26-2019, 12:38 AM
1. Replace frmTelr.Hide (Me.Hide should be more correct) with
Unload Me
2. Add in the UserForm module
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = vbFormControlMenu Then
Cancel = True
End If
End Sub
3. Content of PopUpNews function
Function PopUpNews(varTelTxt As String, varTimDelay As Integer)
varTxtr = varTelTxt
varDelyr = varTimDelay

frmTelr.lblTelr.Caption = varTxtr

frmTelr.Show vbModeless

End Function
Artik

pmyk
10-28-2019, 05:53 AM
@Artik (http://www.vbaexpress.com/forum/member.php?19997-Artik)
I thank you for your reply.
I tried it.
This is what I got.

Run-time error '401':
Can't show non-modal form when modal form is displayed

When I click the DEBUG, the programme has halted on the following code in the Function PopUpNews which is in Module1 in the MODULES section:
frmTelr.Show vbModeless

Instead of frmTelr.Hide I used Me.Hide.
But this is what I got:
Compile error:
Invalid use of Me keyword

This may be because the Function PopUpNews which is in Module1 in the MODULES section. I am not sure.

Any suggestion, appreciated.

SamT
10-28-2019, 11:51 PM
In Standard Modules, ME refers to ThisWorkbook.

Place this code in a UserForm Module, the ThisWorkbook Module, a Class Module, a standard Module, and a Worksheet Module and test all

Private Sub WhoIsME()
MsgBox Me.Name
End Sub

Paul_Hossler
10-29-2019, 05:21 AM
Should that be "WhoIsME" or "WhoAmI"???:devil2:

SamT
10-29-2019, 09:41 AM
"What-R-You"

Paul_Hossler
10-29-2019, 12:28 PM
"What-R-You"

Sorry - spent last 5 days proofreading daughter's college report - hard to turn it off

pmyk
10-29-2019, 10:46 PM
SamT (http://www.vbaexpress.com/forum/member.php?6494-SamT)
http://www.vbaexpress.com/forum/images/statusicon/user-offline.png
I tried your code.

When it is used in UserForm frmHomPg, it gives the name of the UserForm, which is frmHomPg.

When it is used in ThisWorkbook, it gives the name of the current Excel File with extension.

I pasted it in Module1, which is in Modules Folder. I think this is the Standard Module that you have mentioned.
I called it from UserForm frmHomPg with the following code:


Call WhoIsME

It displayed the following error message:
Compile error:
Sub or Function not defined

So, I removed the word PRIVATE from this code in the Module1 and called again from UserForm frmHomPg.
It displayed the following error message:
Compile error:
Invalid use of Me keyword

I am using only FUNCTIONS in Module1. So, I changed the code as below:


Function WhoIsME()
MsgBox Me.Name
End Function

It displayed the following error message:
Compile error:
Invalid use of Me keyword

I never knew about Class Module.
Only after seeing your suggestion, I searched on the net about Class Module and learnt something about it. I haven't tried Class Module yet. I will try it.

I dont know about Worksheet Module.
After searching on the net, I could not get a better explanation with examples.

Thanks for the suggestions.

Paul_Hossler
10-30-2019, 06:11 AM
I am using only FUNCTIONS in Module1. So, I changed the code as below:
Function WhoIsME()
MsgBox Me.Name
End Function
It displayed the following error message:
Compile error:
Invalid use of Me keyword


"Me" won't work in a Standard Module since the standard module it really isn't an object, like a Worksheet, Class, Userform, or Thisworkbook

Fortunately, I've found it's seldom needed

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/me-keyword


The Me keyword (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#keyword) behaves like an implicitly declared variable (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#variable). It is automatically available to every procedure (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#procedure) in a class module (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#class-module).

When a class (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#class) can have more than one instance, Me provides a way to refer to the specific instance of the class where the code is executing.

Using Me is particularly useful for passing information about the currently executing instance of a class to a procedure in another module (https://docs.microsoft.com/en-us/office/vba/language/glossary/vbe-glossary#module).

pmyk
10-30-2019, 10:58 PM
Paul_Hossler (http://www.vbaexpress.com/forum/member.php?9803-Paul_Hossler)
http://www.vbaexpress.com/forum/images/statusicon/user-offline.png
Thanks for your explanations.
I am still studying about Class.
I saw the example code in the link that you have given. Thanks.