Pause VBA while dll function runs

Discussion in 'AutoCAD' started by Jon, Jan 30, 2004.

  1. Jon

    Jon Guest

    Hi,
    I have created a VB6 dll which processes files, running through a few forms
    and subs before terminating. Upon finishing, I want the VBA program which
    called it to continue.
    How do you pause a VBA program while a called VB dll does its thing?

    Thanks,
    Jon Rasmussen
     
    Jon, Jan 30, 2004
    #1
  2. Jon

    Norman Yuan Guest

    You do not have to anything to "pause" VBA, since the ActiveX dll used in
    your VBA process is "in process" operation, just like executing a procedure
    of VBA. Take following VBA procedure as an example,

    Private Sub DoSomething()

    'Do something first - pure VBA opeation

    Dim obj As MyDll.MyObject
    Set obj = New MyDll.MyObject 'Call object in you dll
    obj.DoLengthyProcess 'Do lengthy process, it run
    inside VBA process boundary

    End Sub

    The potential problem is that when the length process (whether it is code in
    DLL or pure VBA code, the computer may look frozen. You better add some
    visual indicator to inform user something is going on. With VBA code, it is
    easy to put a label on form and update label's caption periodically (or use
    a progressbar, but license could be an issue). With dll, how do you return a
    progress indicator to its calling VBA?
    I usually add an optional argument (Label, or ProgressBar) to the lenthy
    process method:

    Class MyObject
    ....
    Public Sub DoLengthyProvess(arg1 As ...,...,Optional promptLabel As Lable)

    End Sub

    In this method, if the optinal argument is supplied, then it gets updated
    periodically. So, when I need visual indicator on a form when call the
    DoLengthyProcess(), I simply pass a label (or progressbar) on the form in
    the method call. Oh, do not forget call Label.Refresh, or DoEvents to
    refresh the Label inside the method.
     
    Norman Yuan, Jan 30, 2004
    #2
  3. Jon

    Jon Guest

    Thanks Norman, I appreciate your time.

    You're right, I was looking at it the wrong way.
    The snippets of code were great.

    Thanks,

    Jon Rasmussen
     
    Jon, Jan 30, 2004
    #3
  4. Jon

    Jon Guest

    I have struck a problem:

    When I try to run the dll program, which has multiple forms, I get the
    message
    Can't show non-modal form when modal form is displayed.

    The code that is calling the dll is:

    Private Sub CB_REDUCE_SURVEY_Click()
    surveyreduce.fn_reduce_survey
    End Sub

    which is command button on a form. If I tell the form to hide itself, the
    program runs, but then the form menu will not be displayed when the dll
    program is completed.

    Any hints wiould be appreciated.

    Jon Rasmussen
     
    Jon, Jan 31, 2004
    #4
  5. Hi Jon,

    All you need in general terms is:

    Private Sub CB_REDUCE_SURVEY_Click()
    Me.Hide
    surveyreduce.fn_reduce_survey
    Me.Show
    End Sub

    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au

    Me.Hide
     
    Laurie Comerford, Jan 31, 2004
    #5
  6. Jon

    Jon Guest

    Hi Laurie,
    Except the program doesn't wait for the dll to finish before it redisplays
    the calling form, so I end up with the dll form in the background.

    Jon Rasmussen
     
    Jon, Jan 31, 2004
    #6
  7. Jon

    Norman Yuan Guest

    Since UserForm in Acad is usually shown as modal form (unless you show it as
    modeless with AcFocusControl applied on Acad2002/2004, which I have never
    used), if you wnat to open another form without closing/hiding the UserForm,
    you must open it as modal form. See following code sample:

    In ActiveX dll project:

    The Class called DllFormTest:
    ===========================
    Option Explicit

    Private dlg As dlgTest

    Public Function GetDialogBoxReturnedValue() As String

    Set dlg = New dlgTest
    dlg.Show vbModal

    Dim ret As String
    ret = dlg.DialogResult

    Unload dlg

    GetDialogBoxReturnedValue = ret

    End Function
    ================================

    Here is the form in *.dll project that will be opened as dialog box:
    This is a blank form with two buttons: OK and Cancel
    ==================================
    Option Explicit

    Private strReturn As String

    Public Property Get DialogResult() As String
    DialogResult = strReturn
    End Property

    Private Sub CancelButton_Click()
    strReturn = "DialogBox is cancelled!"
    Me.Hide
    End Sub

    Private Sub OKButton_Click()
    strReturn = "DialogBox is OKed!"
    Me.Hide
    End Sub
    =================================

    After compiling the *.dll project, now in Acad, you call the dll class to
    show a dialog form by clicking a button on the UserForm:
    ==============================

    Private Sub CommandButton1_Click()

    Dim dlg As DllForm.DllFormTest
    Dim dlgResult As String

    dlg=New DllForm.DllFormTest
    dlgResult=dlg.GetDialogBoxReturnedValue

    MsgBox dlgResult

    'Do something depending on the dialog result

    End Sub
    =============================

    HTH
     
    Norman Yuan, Jan 31, 2004
    #7
  8. Jon

    Jon Guest

    HTH
    This helped heaps!!

    Thanks Norm.

    I can't help feeling that I am missing something however. I have a VBA menu,
    which activates this dll programme (which has it own forms as it is also a
    stand alone program), and I want the calling vba menu to come back when the
    dll is finished. How do programers normally do this?

    Jon
     
    Jon, Jan 31, 2004
    #8
  9. Hi Jon,

    I do it exactly as I posted above. I do not change the modality of the
    forms in either program and have never had a problem.

    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au
     
    Laurie Comerford, Feb 1, 2004
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.