Forms in Solidworks VBA Editor

Discussion in 'SolidWorks' started by Swartzy, Mar 20, 2007.

  1. Swartzy

    Swartzy Guest

    I have a pretty extensive macro written in the VBA editor w/ one form
    for entering a set of data to be used by the macro. The macro runs
    fine when run from the editor itself. However... when I run it from
    the Solidworks macro toolbar, it skips over the form altogether and
    goes to the next line of code.

    I've tried modal and not modal w/ the same result. I've used VB6 to
    compile to an .exe in the past, but I was hoping to avoid that here (I
    don't have VB6 where I work now).

    Is there anyway to run a macro w/ forms? Do I need to register the
    form somehow within the macro or call it some other way (other than
    frmMyForm.show)?
     
    Swartzy, Mar 20, 2007
    #1
  2. Swartzy

    fcsuper Guest

    Swartzy,

    You need to load the form before your show it. You will also need to
    unload before hiding it.

    In the Module to call the form:

    Load frmMyForm
    frmMyForm.show

    and in the Form at the end:

    frmMyForm.Hide
    Unload frmMyForm


    Enjoy,

    Matt
    http://sw.fcsuper.com
     
    fcsuper, Mar 20, 2007
    #2
  3. Swartzy

    Swartzy Guest

    Interesting... I tried that. However... it was before I exported the
    form. Does the form in fact have to be exported?

    Thx,
    Eric
     
    Swartzy, Mar 20, 2007
    #3
  4. Swartzy

    Swartzy Guest

    Tried it again... still no luck.
     
    Swartzy, Mar 20, 2007
    #4
  5. Swartzy

    That70sTick Guest

    I've written macros that use forms w/ no problem. Some examples
    available at <http://www.esoxrepublic.com/freeware/>.

    For some reason, sometimes the position of "Sub Main" makes a
    difference. It sometimes needs to be at the very bottom of your code
    module.
     
    That70sTick, Mar 21, 2007
    #5
  6. Swartzy

    fcsuper Guest

    Swartzy,

    Without seeing the code, it is hard to tell. There's a lot of things
    it can be. Can you upload the macro somewhere so we can look at it?

    Ideas so far:

    The module that you think is supposed to be opening the form is
    different from where it actually is. Solution: Find it and fix the
    issue there.

    As noted by others, your macro isn't starting from the correct Sub.
    Several solutions: Move "main" to the end. This doesn't always work,
    but worth a try. Also, if possible, turn other subs into functions
    instead. Many times macros are unnecessarily using "Sub" instead of
    "Function" for subroutines.

    Make sure your Calls between subroutines are logically set up (things
    are happening in the order you intend them to).

    Also, if it is running from the editor, but not live, then make sure
    you are running the macro from the module sub Main, and NOT from the
    form itself. VBA will allow you to run a form by itself without the
    module code. This could be creating the illusion of a functioning
    macro when it really isn't.

    Check to make sure you have the Microsoft Forms reference library
    loaded in the macro, otherwise forms will not. Also, make sure you
    don't have any unnecessary libraries loaded which may interfer with
    the function of the macro.

    Just some ideas. I hope they help.

    Matt
    http://sw.fcsuper.com
     
    fcsuper, Mar 22, 2007
    #6
  7. Swartzy

    Swartzy Guest

    Thanks for all the input. For what it's worth... the main subroutine
    is actually in a module all by itself. I suppose that could be part
    of the problem, but if you can't seperate out your subs to different
    modules then what's the point?

    I ended up compiling it to an .exe in VB6 and it works fine that way.
    It seems more stable as well for one reason or another... I was
    randomly getting "Server Threw an Exception" errors when trying to
    close a drawing after processing it. It also does a better job of
    updating the form (w/ what's going on) then when run as a Solidworks
    macro.
     
    Swartzy, Mar 22, 2007
    #7
  8. Swartzy

    ben Guest

    I'd say your best bet is trying to "Divide and Conquer" the problem.
    Make a simple macro with a form and get that working and slowly
    introduce copies of your code until you see the behavior in your full
    macro.

    I'm currently combined a few macros I had and made a gui interface
    right in Solidworks's VB. It has a module I called "main" with a
    subroutine called "main." The main sub just says formName.show and
    from there you should be able to handle the rest.

    Ben
     
    ben, Mar 23, 2007
    #8
  9. Swartzy

    Ed Guest

    It has been a while since I have used VBA with SW but it seems like I
    ran into an issue that the code wouldn't work properly if there was a
    minor error. But, instead of reporting the error and stopping the VBA
    just skipped over some of the code, (something like this). The way
    that I found and fixed these problems was to move the code over to
    VB6. As a result I quit using VBA. Just too many limitations and
    problems.

    As far as VB6 I was very reluctant to switch over to VB.NET, (put this
    off for several years). When I finally did change over I really
    regretted not doing so much sooner. There are some slight differences
    in thinking but for the most part .net is what one would expect. But,
    there are TREMENDOUS improvements in .net. The interface works
    better, errors seem to be much easier to find and correct etc. There
    are so many subtle improvements that the over all effeceincy with it
    is much better even when learning a new piece of software. And if you
    startout with Visual Studio 2005 MS has made considerable progress
    with "automating" many of the coding functions. This means that just
    by dragging many of the tools into your project that a fairly amount
    of code is automatically inserted and all that is often needed is to
    fill in a few properties.

    Hope this helpes,

    Ed
     
    Ed, Mar 23, 2007
    #9
  10. Swartzy

    That70sTick Guest

    Try triggering your macro from the macro toolbar with a button
    pointing to your "Sub Main".

    If you moved "Sub Main" from its original module (or renamed the
    module) that can cause issues. I don't know why, but sometimes it
    just does.
     
    That70sTick, Mar 23, 2007
    #10
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.