VBA question reguarding syntax

VBA question reguarding syntax

mid-awe
Collaborator Collaborator
649 Views
10 Replies
Message 1 of 11

VBA question reguarding syntax

mid-awe
Collaborator
Collaborator
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.
0 Likes
650 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
[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]
0 Likes
Message 3 of 11

mid-awe
Collaborator
Collaborator
Thank you very much that is an interesting structure, 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.
Message was edited by: MiD-AwE
0 Likes
Message 4 of 11

Anonymous
Not applicable
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 Jack_Rabbit@NOSPAM.excite.com

I'll have a look and see if I can't provide some help.
0 Likes
Message 5 of 11

mid-awe
Collaborator
Collaborator
Thank you,

I appreciate the offer but I got it working.
0 Likes
Message 6 of 11

mid-awe
Collaborator
Collaborator
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.
0 Likes
Message 7 of 11

Anonymous
Not applicable
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]
0 Likes
Message 8 of 11

Anonymous
Not applicable
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 ---- "MiD-AwE" wrote in message news:20949046.1103301373463.JavaMail.jive@jiveforum2.autodesk.com... > 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.
0 Likes
Message 9 of 11

mid-awe
Collaborator
Collaborator
Hey thanks, I think I understood most of what you said. 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.(this is my first VBA project ever so please be kind).
Message was edited by: MiD-AwE
0 Likes
Message 10 of 11

Anonymous
Not applicable
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 ---- "MiD-AwE" wrote in message news:14697330.1103320779295.JavaMail.jive@jiveforum1.autodesk.com... > 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.
0 Likes
Message 11 of 11

mid-awe
Collaborator
Collaborator
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.
0 Likes