Results 1 to 18 of 18

Thread: Form show vbmodal and the rest of the sub

  1. #1

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Form show vbmodal and the rest of the sub

    Hi friends,

    Building a "import" feature and I show a form with a "Waiting" message duroing import process.

    Actually, I do like this :
    Code:
    Private Sub Import_click()
    [...]some code
    Form2.Show vbModeless, Me
    Call SetTopMostWindow(Form2.hWnd, OnTop)
    Call ImportProcess(parameters)
    End sub
    But Will import processing, the user can click everywhere else in the software.
    If a I change for :
    Code:
    Private Sub Import_click()
    [...]some code
    Form2.Show vbModal
    Call ImportProcess(parameters)
    End sub
    User can not click everywhere else anymore, but Call ImportProcess(parameters) is called only if I close Form2. So, not good.

    Is there a way to keep Form2 like vbModal, without pausing the sub ?

    Thanks
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Form show vbmodal and the rest of the sub

    That's how modal windows work.

  3. #3
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    176

    Re: Form show vbmodal and the rest of the sub

    If don't need to show progress this would be a simply solution:
    In the modal form put a timer control, and this code:
    Code:
    Option Explicit
    
    Public Parameters As Variant
    Public ParentObject As Object
    Public Process As String
    
    Private Sub Form_Load()
        Timer1.Enabled = True
        Timer1.Interval = 10
    End Sub
    
    Private Sub Timer1_Timer()
        'Start Proccess
        CallByName ParentObject, Process, VbMethod, Parameters
        'the process is terminated, unload modal form
        Unload Me
    
    End Sub
    And in the main form:
    Code:
    Private Sub Import_click()
        Dim Parameters As Variant
        '[...]some code
        Dim f As Form2
        Set f = New Form2
        'Pass the parameters to the other fom
        f.Parameters = Parameters
        Set f.ParentObject = Me
        f.Process = "ImportProcess"
        f.Show vbModal, Me
        'not needed the other form call the process
        'Call ImportProcess(Parameters)
    End Sub
    ImportProcess must be a public method in the main form and admits only a parameter, but you can adapt it at your needs

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Form show vbmodal and the rest of the sub

    Try these two sample forms to implement modal progress indication with cancellation

    Code:
    '--- Form1
    Option Explicit
    
    Private WithEvents m_oProgress As Form2
    
    Private Sub Form_Click()
        Set m_oProgress = New Form2
        m_oProgress.SetProgress "Starting..."
        m_oProgress.Show vbModal, Me
    End Sub
    
    Private Sub m_oProgress_Start()
        pvImport
        Unload m_oProgress
        Set m_oProgress = Nothing
    End Sub
    
    Private Sub pvImport()
        Dim cFolders    As Collection
        Dim lIdx        As Long
        Dim sFolder     As String
        Dim sFile       As String
        
        Set cFolders = New Collection
        cFolders.Add "C:"
        Do While lIdx < cFolders.Count
            sFolder = cFolders.Item(lIdx + 1)
            If Not m_oProgress.SetProgress("Processing " & sFolder & "...") Then
                Exit Sub
            End If
            sFile = vbNullString
            On Error Resume Next
            sFile = Dir(sFolder & "\*.*", vbDirectory)
            On Error GoTo 0
            Do While Len(sFile) <> 0
                If sFile <> "." And sFile <> ".." Then
                    sFile = sFolder & "\" & sFile
                    If (GetAttr(sFile) And vbDirectory) <> 0 Then
                        cFolders.Add sFile
                    End If
                End If
                sFile = Dir
            Loop
            lIdx = lIdx + 1
        Loop
    End Sub
    Code:
    '--- Form2
    Option Explicit
    
    Event Start()
    
    Private m_bIsActive As Boolean
    Private m_bIsCanceled As Boolean
    
    Public Function SetProgress(sText As String) As Boolean
        Label1.Caption = sText
        DoEvents
        If Not m_bIsCanceled Then
            SetProgress = True
        End If
    End Function
    
    Private Sub Form_Activate()
        If Not m_bIsActive Then
            m_bIsActive = True
            RaiseEvent Start
        End If
    End Sub
    
    Private Sub Command1_Click()
        m_bIsCanceled = True
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        If UnloadMode <> vbFormCode Then
            m_bIsCanceled = True
            Cancel = 1
        End If
    End Sub
    You'll need to put a Command1 and a Label1 on Form2 for indication and cancellation to happen.

    Click on Form1 to start "import" process.

    This is a sample of the only case when using DoEvents is justified i.e. spinning msg pump to redraw the progress label and accept clicks on cancellation button incl. click on [X] button to unload the form i.e. in order to dismiss the operation altogether it first has to be canceled.

    cheers,
    </wqw>

  5. #5

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Re: Form show vbmodal and the rest of the sub

    Thanks friends
    Have to debug first another point, and I'll try !
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  6. #6

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Re: Form show vbmodal and the rest of the sub

    Hi,

    Tried both codes, running in standalone
    As well as progress could be useful, I choose wqweto code

    But in a try it in the existing project, with adding the 2 forms, I get a Compil error: Object does not source automation events (I even not understanding what the error means lol) on this line:
    Code:
    Private WithEvents m_oProgress As Form2
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  7. #7
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by wqweto View Post
    Code:
        DoEvents
    ......

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by wqweto View Post
    This is a sample of the only case when using DoEvents is justified i.e. spinning msg pump to redraw the progress label and accept clicks on cancellation button incl. click on [X] button to unload the form i.e. in order to dismiss the operation altogether it first has to be canceled.
    @Eduardo: This is a case impl. all safe conditions to call DoEvents i.e. when there is a modal form in this case with a single Cancel button the end-user must interact with.

    In this particular case there is no possibility of reentrancy because the top modal form has disable every other (modal or non-modal) form so these cannot be interacted with.

    Quote Originally Posted by Couin View Post
    But in a try it in the existing project, with adding the 2 forms, I get a Compil error: Object does not source automation events (I even not understanding what the error means lol) on this line:
    Code:
    Private WithEvents m_oProgress As Form2
    There is Event Start() declaration in Form2 in the sample above.

    cheers,
    </wqw>

  9. #9
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by wqweto View Post
    @Eduardo: This is a case impl. all safe conditions to call DoEvents
    hahahahahahahahahahahaha

    (pssss, don't tell anybody else)

  10. #10
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by Eduardo- View Post
    hahahahahahahahahahahaha

    (pssss, don't tell anybody else)
    You can laugh all that you want but my stand on DoEvents usage remains unchanged -- it is very rarely needed, for newbies almost never.

    This is like most beginners try to fix their SQL queries by slapping a HAVING clause which is a case of being beyond clueless. DoEvents is in the same category of cluelessness.

    Do you have some other usage for DoEvents in your user-controls? I'll be most interested what you stance is on the subject which you so eagerly try to bring on here.

    cheers,
    </wqw>

  11. #11
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by wqweto View Post
    Do you have some other usage for DoEvents
    Do I need to?
    And of course I do.

    Quote Originally Posted by wqweto View Post
    I'll be most interested what you stance is on the subject which you so eagerly try to bring on here.

    cheers,
    </wqw>

    You already know my stance, or (re)read my old posts.

    The point? The point is that you criticized me for using DoEvents (without even taking into account -or knowing at all- how or where I used it).
    Joining and also provoking a horde of other ones also criticizing me, based on your reputation (general reputation, what I don't question of course).

    At least be coherent and do not use it yourself.

    Or anybody that uses DoEvents now is obligated to explain to others why it is well used is his particular case?


    Forget it, never mind.

  12. #12
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by Eduardo- View Post
    Do I need to?
    And of course I do.



    You already know my stance, or (re)read my old posts.

    The point? The point is that you criticized me for using DoEvents (without even taking into account -or knowing at all- how or where I used it).
    Joining and also provoking a horde of other ones also criticizing me, based on your reputation (general reputation, what I don't question of course).

    At least be coherent and do not use it yourself.

    Or anybody that uses DoEvents now is obligated to explain to others why it is well used is his particular case?


    Forget it, never mind.
    I don't remember attacking you personally but my point against DoEvents is in principle -- there are very few cases where one *can* use it.

    It's not if one wishes to use it but whether one *can* use it, whether the code becoming reentrant is possible and reentrancy handled at all.

    Let me apologize to you if you felt criticized by me -- it was always about the code. It's all about the code, I have nothing against you personally, honestly.

    cheers,
    </wqw>

  13. #13
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by wqweto View Post
    I don't remember attacking you personally but my point against DoEvents is in principle -- there are very few cases where one *can* use it.

    It's not if one wishes to use it but whether one *can* use it, whether the code becoming reentrant is possible and reentrancy handled at all.

    Let me apologize to you if you felt criticized by me -- it was always about the code. It's all about the code, I have nothing against you personally, honestly.

    cheers,
    </wqw>
    I was "surrounded" by several people in that thread because I was using DoEvents, and it served as an excuse for others (better let's not go into details).
    I know it is not personal, of course, never thought that.

    I guess I somewhat share your view... but not literally as expressed above (in short: in most cases you "can", if you take care of the different situations that can happen -although sometimes are hard to foresee some-)

    Anyway IDK if this conversation makes much sense, since I'm quite sure I won't be able to make you change your mind a millimeter on this issue, or on using OERN, or on any issue.

    Happy DoEventing!

  14. #14
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by Eduardo- View Post
    Anyway IDK if this conversation makes much sense, since I'm quite sure I won't be able to make you change your mind a millimeter on this issue, or on using OERN, or on any issue.
    LOL, I just used DoEvents *and* OERN in a single procedure above :-))

    Btw, Dir (and GetAttr) bomb out when file/path does not exist so if one wishes to adhere to the one true Break on All Errors religion they have to stop using these built-in functions and start using API replacements instead.

    cheers,
    </wqw>

  15. #15
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by wqweto View Post
    LOL, I just used DoEvents *and* OERN in a single procedure above :-))
    Really? I see now!!!!!!!!!!!

    I didn't read the code... I just saw DoEvents while scrolling the thread.

    Quote Originally Posted by wqweto View Post
    Btw, Dir (and GetAttr) bomb out when file/path does not exist so if one wishes to adhere to the one true Break on All Errors religion they have to stop using these built-in functions and start using API replacements instead.

    cheers,
    </wqw>
    Yes, I guess you always can replace with something else and avoid OERN entirely, but IMO that complication is not justified.

    The same for GoTo, GoSub and may be any other of the "hard rules" (I'm talk for myself, IDK or don't remember your position on those ones).

    "Never use End" seems to be the one more justified, but I'm quite sure that could be challenged too.

    Talking about how religions "forbids" things, and how the Bible that is not a religion approaches the same things, makes me think on 1 Co 10:23 and 1 Ts 5:21. But that would be a subject for Chit Chat or another section.

  16. #16

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by wqweto View Post
    There is Event Start() declaration in Form2 in the sample above.
    Hi wqweto,
    Yes, you are right, but I do not really understand why in the example, it is ok, and when I add the 2 same forms to my project, to just test before trying to apply the method to my other form, I get this error.
    Sohuld I remove if from Form 2 in my porject ?

    Thanks
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Form show vbmodal and the rest of the sub

    Man, this thread is confusing to me. Personally, I think of showing a form with vbModal as the same as calling a function ... the line of code where it's done is "paused" until the vbModal form is no longer modal (or the function returns, to complete the analogy).

    And, to solve Couin's problem, I don't know why he just doesn't do his work inside the modal form (maybe in the Form_Activate event). To return to the analogy, that'd be like doing your work inside a called function, rather than scratching our head wondering why our code doesn't continue until the function completes and returns. As Dil said, that's the way it works.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  18. #18
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Form show vbmodal and the rest of the sub

    Quote Originally Posted by Couin View Post
    Hi wqweto,
    Yes, you are right, but I do not really understand why in the example, it is ok, and when I add the 2 same forms to my project, to just test before trying to apply the method to my other form, I get this error.
    Sohuld I remove if from Form 2 in my porject ?

    Thanks
    You can use the IDE debugger to trace the code.

    First trace the sample code and notice the control flow -- first Show vbModal, then Activate event which rises Start event.

    Then trace your implementation to see where the control flow deviates from the sample code.

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width