[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vbscript and goto

Posted on 2005-04-19
11
Medium Priority
?
11,786 Views
Last Modified: 2012-06-27
hi,

can anybody translate this working vb code into vbscript ?
i have a problem with the labels and jumping to it

Sub GotoStatementDemo()
Dim Number, MyString
   Number = 1    ' Initialize variable.
   ' Evaluate Number and branch to appropriate label.
   If Number = 1 Then GoTo Line1 Else GoTo Line2
   
Line1:
   MyString = "Number equals 1"
   GoTo LastLine   ' Go to LastLine.
Line2:
   ' The following statement never gets executed.
   MyString = "Number equals 2"
LastLine:
   Debug.Print MyString   ' Print "Number equals 1" in
      ' the Immediate window.
End Sub

0
Comment
Question by:McRight
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 13821734
Not a very good idea to work with goto... it makes a program very complex. Why don't you do it this way:

Sub GotoStatementDemo()
Dim Number, MyString
   Number = 1    ' Initialize variable.
   ' Evaluate Number and branch to appropriate label.
   If Number = 1 Then
       MyString = "Number equals 1"
   else
        MyString = "Number equals 2"
   end if
   Debug.Print MyString  
End Sub
0
 
LVL 2

Expert Comment

by:mjwills
ID: 13821783
http://www.visualbasicscript.com/topic.asp?TOPIC_ID=1572

You probably want to use functions...
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13822094
Couldnt you use select case instead of If else statements ?

Look here :

http://www.winguides.com/scripting/reference.php?id=17

Then include the select case within a function :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:wraith821
ID: 13825968
Sub GotoStatementDemo()
Dim Number, MyString
   Number = 1    ' Initialize variable.
   ' Evaluate Number and branch to appropriate label.
   If Number = 1 Then GoTo Line1 Else GoTo Line2
   
Select case Number
    case 1
   MyString = "Number equals 1"
   
    case 2
   ' The following statement never gets executed.
   MyString = "Number equals 2"

end select

   Debug.Print MyString   ' Print "Number equals 1" in
      ' the Immediate window.
End Sub
0
 
LVL 8

Accepted Solution

by:
wraith821 earned 100 total points
ID: 13825984
oops sorry, use this one. i left the gotot lin1 else goto line2 in the sub. it should not be there.

 Sub GotoStatementDemo()
Dim Number, MyString
   Number = 1    ' Initialize variable.
   ' Evaluate Number and branch to appropriate label.
 
   
Select case Number
    case 1
   MyString = "Number equals 1"
   
    case 2
   ' The following statement never gets executed.
   MyString = "Number equals 2"

end select

   Debug.Print MyString   ' Print "Number equals 1" in
      ' the Immediate window.
End Sub
0
 
LVL 1

Author Comment

by:McRight
ID: 13828958
thanks for the comments but this is not what i'm looking for.
i seldom use goto's in vb but in this case i have no choice
i have to read a completely unformatted document and translate it to xls
dependant on the field's result i have to jump in the code
the vb code works just like i want it, but i want to translate the code in vbscript
is it possible to use the labels in vbscript ?
0
 
LVL 2

Assisted Solution

by:mjwills
mjwills earned 100 total points
ID: 13829107
No - goto is not allowed in vbscript.
0
 
LVL 2

Expert Comment

by:mjwills
ID: 13830464
If the code works like you want it, a good solution would be to leave the code in VB and make the project an ActiveX DLL. Then your vbscript code can talk to your ActiveX DLL which will do all the work.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question