VBA question reguarding syntax

Discussion in 'AutoCAD' started by MiD-AwE, Dec 14, 2004.

  1. MiD-AwE

    MiD-AwE Guest

    I'm very new to VBA and I'm still struggling with simple syntax issues. That said I admit that I'm stupid about this and I'm asking for help please?

    I'm writing a VBA app to open a simple dialogue with Checkboxes and Option buttons which settings determine the electrical and plumbing schematic block is to be inserted into a drawing. Then, the correct AUTOLISP command is compiled and output to the Command line while exiting the dialogue.

    This all works fine until I need to check if two or more statements are true at the same time. I tried to use a if, and, then, elseif, else statement but is doesn't seem to recognize the statement at all? here is an example:

    Function GetEQ()
    If SpaOnly And Raised = True Then
    EQ$ = "RSWV"
    ElseIf PoolSpa And PCC = True Then EQ$ = "PCS"
    ElseIf PoolOnly And PCC = True Then EQ$ = "PC2"
    ElseIf SpaOnly And Vac = True Then EQ$ = "SWV"
    ElseIf PoolOnly = True Then EQ$ = "NP"
    ElseIf SpaOnly = True Then EQ$ = "SWV"
    ElseIf PoolSpa = True Then EQ$ = "PAS"
    Else
    PoolOnly = False: SpaOnly = False: Raised = False: Vac = False: PVal = False: NoBooster = False: PCC = False: Att = False: Sepr = False: SpaB = False: TwoSkims = False: Frog = False: Salt = False: DiChlor = False: WfBooster = False: Wfall = False: ShDescent = False: PcLay = False: SpLay = False
    End If
    End Function

    Any and all help with this likely to be easy question is greatly appreciated. Thanks.
     
    MiD-AwE, Dec 14, 2004
    #1
  2. MiD-AwE

    Jackrabbit Guest

    [pre]
    1. Your function needs a return type (and it needs to return something).

    2. A function named GetEQ should only return a single value. It should not
    change the values of a bunch of other (I assume global) variables.

    Public Function GetEQ() As String
    If SpaOnly And Raised Then
    GetEQ = "RSWV"
    ElseIf PoolSpa And PCC Then
    GetEQ = "PCS"
    ElseIf PoolOnly And PCC Then
    GetEQ = "PC2"
    ElseIf SpaOnly And Vac Then
    GetEQ = "SWV"
    ElseIf PoolOnly Then
    GetEQ = "NP"
    ElseIf SpaOnly Then
    GetEQ = "SWV"
    ElseIf PoolSpa Then
    GetEQ = "PAS"
    Else
    GetEQ = ""
    End If
    End Sub

    To use: EQ$ = GetEQ
    [/pre]
     
    Jackrabbit, Dec 15, 2004
    #2
  3. MiD-AwE

    MiD-AwE Guest

    Thank you very much that is an interesting , but I Think my code is too BASIC and not enough VBA :)

    In the line, "If SpaOnly And Raised = True Then
    EQ$ = "RSWV"" RSWV is the actual LISP command that I want to enter at the command line of AutoCAD. I put it together like this:
    ___________________
    Private Sub CommandButton1_Click()

    GetEQ ;;find equipment schematic

    GetLay ;;find layer to insert to

    EQ$ = "I" + EQ$ + Lay$ ;; make LISP command

    ThisDrawing.SendCommand EQ$ + " " ;;insert command followed by space (enter)

    UserForm1.Hide ;;close dialogue

    End Sub
    __________________________

    It is working. Yet, I had hoped to find a better way of finding the correct string data to send. We have many schematics to sort through. It seems overly cumbersome doing it my way.
     
    MiD-AwE, Dec 15, 2004
    #3
  4. MiD-AwE

    Jackrabbit Guest

    Can you zip up your VBA project (*.dvb file) and a sample drawing with all of the blocks defined and attach it (if under 1MB)? If the zipped file is too large, email it to me at

    I'll have a look and see if I can't provide some help.
     
    Jackrabbit, Dec 15, 2004
    #4
  5. MiD-AwE

    MiD-AwE Guest

    Thank you,

    I appreciate the offer but I got it working.
     
    MiD-AwE, Dec 17, 2004
    #5
  6. MiD-AwE

    MiD-AwE Guest

    I'd like to add a msgbox that will notify the user if the schematic they are looking for doesn't exists in the list.

    Can anyone help this is what I have but it doesn't work. What am I forgetting?

    Dim SpaOnly As Boolean
    Dim PoolOnly As Boolean
    Dim PoolSpa As Boolean
    Dim Msg, Style, Title, Response

    Function GetEQ()
    If SpaOnly And Raised = True Then
    EQ$ = "RSWV"
    ElseIf PoolSpa And PCC = True Then EQ$ = "PCS"
    ElseIf PoolOnly And PCC = True Then EQ$ = "PC2"
    ElseIf SpaOnly And Vac = True Then EQ$ = "SWV"
    ElseIf PoolOnly = True Then EQ$ = "NP"
    ElseIf SpaOnly = True Then EQ$ = "SWV"
    ElseIf PoolSpa = True Then EQ$ = "PAS"
    Else
    EQ$ = " "
    End If
    End Function

    GetEQ
    If EQ$ = " " Then
    Msg = "Sorry, Schematic Unavalable"
    Style = vbOKOnly + vbCritical
    Title = "Unavailable Schematic !"
    Response = MsgBox(Msg, Style, Title)
    End If

    If I paste the code into a command button it works fine. Why does it fail in the above code? Thanks again.
     
    MiD-AwE, Dec 17, 2004
    #6
  7. MiD-AwE

    Jackrabbit Guest

    This works for me:
    [pre]
    Public Sub Test01()
    Dim BlockName As String
    Dim ErrorMessage As String
    Dim Response As Integer
    Dim Title As String

    BlockName = ""

    If BlockName = "" Then
    ErrorMessage = "Sorry, Schematic Unavalable"
    Title = "Unavailable Schematic"
    Response = MsgBox(ErrorMessage, vbCritical, Title)
    End If
    End Sub
    [/pre]
     
    Jackrabbit, Dec 17, 2004
    #7
  8. MiD-AwE

    Ed Jobe Guest

    It has to do with variable scope. If you paste all the code together, it all
    has access to EQ$, (BTW, I don't see it Dim'd anywhere). But when you call
    the function, you are expecting EQ$ to be set and it isn't. Have your
    function return the value and then use it to set a var, e.g.

    Inside the function, at the end:
    GetEQ = EQ$

    BTW, change your function's signature to declare the return value's type:
    Function GetEQ () As String

    Then when you call the function, set your var:
    EQ$ = GetEQ()
    If EQ$ = " " Then

    --
    ----
    Ed
    ----
    are looking for doesn't exists in the list.
    in the above code? Thanks again.
     
    Ed Jobe, Dec 17, 2004
    #8
  9. MiD-AwE

    MiD-AwE Guest

    Hey thanks, I think I understood most of what you said. (this is my first VBA project ever so please be kind). Except the last part:

    "Then when you call the function, set your var:
    EQ$ = GetEQ()
    If EQ$ = " " Then"

    Where do I put this? Thanks again.
     
    MiD-AwE, Dec 17, 2004
    #9
  10. MiD-AwE

    Ed Jobe Guest

    The second line is the beginning of your If...then statement in your first
    post. The first line just sets EQ$, which wasn't getting set before.

    --
    ----
    Ed
    ----
    VBA project ever so please be kind). Except the last part:
     
    Ed Jobe, Dec 17, 2004
    #10
  11. MiD-AwE

    MiD-AwE Guest

    This works great, but once the message displays and the user clicks the "OK" button the app closes automatically. How do I tell it to continue the app instead?

    Thank you.
     
    MiD-AwE, Dec 27, 2004
    #11
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.