Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Feb 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA-SolidWorks 2008 (VB)

    I am new to VB programming and I am currently working on a project to automate solid model construction using VB in SolidWorks 2008. I have created a userform and I have programmed all of the necessary calculations, but all of my code is written in the userform commandbutton sub. I want to be able to use the variables (approx. 60 different variable) input from this form throughout the entire program, not just in one userform. How do I write the code for the calculations elsewhere in the program and perform these calculations when this button is pressed? Thanks for your advice.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA-SolidWorks 2008 (VB)

    Welcome to Woody's Lounge!

    I don't know Solidworks, but if VBA works the same as in Microsoft Office applications, you should declare the variables as public variables in a standard module, i.e. the type of module that you create by selecting Insert | Module in the Visual Basic Editor.

    For example, if you have the following declarations in the On Click event procedure for the command button:

    Dim intCount As Integer
    Dim strDescription As String

    Select these declarations, then press Ctrl+X to cut them to the clipboard.
    Activate the standard module, and click at the top of the module (below Option Explicit if you have that).
    Then press Ctrl+V to paste the declarations.
    Use Edit | Replace to change the keyword Dim to Public, so that you now have

    Public intCount As Integer
    Public strDescription As String

    These variables can be used in all code within the project, and they will retain their value within a session (unless an unhandled error occurs)

  3. #3
    New Lounger
    Join Date
    Feb 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA-SolidWorks 2008 (VB)

    Thanks, I will try that.

  4. #4
    New Lounger
    Join Date
    Feb 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA-SolidWorks 2008 (VB)

    Declaring the variables worked as you said it would, but now when I execute my program, my userform window shrinks so only the blue title bar is visible and I am unable to change the size of it. Any ideas about why this is happening?

    Thanks

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA-SolidWorks 2008 (VB)

    I don't think that has anything to do with the variables, but I don't know what could cause this. Does the problem persist if you quit and restart SolidWorks, and if you restart your PC?

  6. #6
    New Lounger
    Join Date
    Feb 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA-SolidWorks 2008 (VB)

    The problem does persist if I restart the program. I tried moving the variables back to within the Userform code and changing the Public back to Dim. With these changes, the userform displays correctly when the program is executed.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA-SolidWorks 2008 (VB)

    I'm afraid I have no idea how that can happen, and since I don't have SolidWorks I have no way to investigate the problem. I don't think you'll find many SolidWorks users here (but you never know...), you might try asking your question in the comp.cad.solidworks usenet group.

Posting Permissions

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