VBA question reguarding syntax
CADForums.net Forum Index CADForums.net
Discussion of AutoCAD and other CAD software.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web cadforums.net
VBA question reguarding syntax

 
Post new topic   Reply to topic    CADForums.net Forum Index -> VBA
Author Message
MiD-AwE
Guest





Posted: Wed Dec 15, 2004 2:46 am    Post subject: VBA question reguarding syntax Reply with quote

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.

Back to top
Jackrabbit
Guest





Posted: Wed Dec 15, 2004 5:57 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

[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]
Back to top
MiD-AwE
Guest





Posted: Wed Dec 15, 2004 7:40 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

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.

Back to top
Jackrabbit
Guest





Posted: Wed Dec 15, 2004 8:03 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

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.
Back to top
MiD-AwE
Guest





Posted: Fri Dec 17, 2004 9:23 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

Thank you,

I appreciate the offer but I got it working.
Back to top
MiD-AwE
Guest





Posted: Fri Dec 17, 2004 9:35 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

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.
Back to top
Jackrabbit
Guest





Posted: Fri Dec 17, 2004 10:15 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

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]
Back to top
Ed Jobe
Guest





Posted: Fri Dec 17, 2004 10:17 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

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" <nospam@address.withheld> wrote in message
news:20949046.1103301373463.JavaMail.jive@jiveforum2.autodesk.com...
Quote:
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.
Back to top
MiD-AwE
Guest





Posted: Sat Dec 18, 2004 2:59 am    Post subject: Re: VBA question reguarding syntax Reply with quote

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.
Back to top
Ed Jobe
Guest





Posted: Sat Dec 18, 2004 3:09 am    Post subject: Re: VBA question reguarding syntax Reply with quote

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" <nospam@address.withheld> wrote in message
news:14697330.1103320779295.JavaMail.jive@jiveforum1.autodesk.com...
Quote:
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.
Back to top
MiD-AwE
Guest





Posted: Mon Dec 27, 2004 11:11 pm    Post subject: Re: VBA question reguarding syntax Reply with quote

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.
Back to top
 
Post new topic   Reply to topic    CADForums.net Forum Index -> VBA All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Windows Server DSP VoIP Electronics New Topics
Contact Us
Powered by phpBB