VBScript looping

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

VBScript looping

Post by VegasNath »

I have an application at work (that I appreciate you won't be able to see and will have no knowledge of), but it's macro's (.mac) use basic VBScript which is what my query here concerns. I am attaching 2 portions of code, the first being the recorded version that uses sendkeys to work through the screens, and the second being written by myself using screen coordinates, less sendkeys, and functions for the most part of the repeated instructions. The latter code sample has not yet been tested, but I am confident that it will work based upon other projects that I have completed, the only *usual* problems that I need to overcome revolve around the 'wait' statements.

My only reason for attaching the first (recorded) code sample is for reference. My question relates to the second sample of code.

In the main sub routine, I have the line PutText 9, 2, "s" followed by Update which is a function. There are seven instances of this instruction where only the row number changes (9, 11, 13, 15, 17, 19, 21).

What I would like to do is incorporate the 14 instructions into a single function loop, where the row numbers change within the function and there is just one call to that function for each screen.

In an ideal nutshell, the Update function should loop through an array of (9, 11, 13, 15, 17, 19, 21) and update each row (current update function), before moving to the next screen and recalling itself over and over.

Hopefully, someone here will be able to help. Many Thanks

Code: Select all

[PCOMM SCRIPT HEADER]
LANGUAGE=VBSCRIPT
DESCRIPTION=
[PCOMM SCRIPT SOURCE]
OPTION EXPLICIT
autECLSession.SetConnectionByName(ThisSessionName)

REM This line calls the macro subroutine
subSub1_

sub subSub1_()
   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "s"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,19,"38","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 2,8,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "01"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"30","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf9]"
   
   autECLSession.autECLPS.WaitForAttrib 9,1,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 9,2,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "s"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,19,"38","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 2,8,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "01"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"30","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf9]"
   
   autECLSession.autECLPS.WaitForAttrib 9,1,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 9,2,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "s"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,19,"38","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 2,8,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "01"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"30","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf9]"
   
   autECLSession.autECLPS.WaitForAttrib 9,1,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 9,2,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "s"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,19,"38","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 2,8,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "01"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"30","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf9]"
   
   autECLSession.autECLPS.WaitForAttrib 9,1,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 9,2,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "s"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,19,"38","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 2,8,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "01"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"30","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf9]"
   
   autECLSession.autECLPS.WaitForAttrib 9,1,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 9,2,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "s"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,19,"38","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 2,8,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "01"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"30","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf9]"
   
   autECLSession.autECLPS.WaitForAttrib 9,1,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 9,2,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[newline]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "s"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,19,"38","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"00","3c",3,10000

   autECLSession.autECLPS.WaitForCursor 2,8,10000

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[backtab]"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "01"
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf2]"
   
   autECLSession.autECLPS.WaitForAttrib 2,7,"30","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf9]"
   
   autECLSession.autECLPS.WaitForAttrib 9,1,"00","3c",3,10000

   autECLSession.autECLPS.Wait 10 

   autECLSession.autECLOIA.WaitForAppAvailable
   
   autECLSession.autECLOIA.WaitForInputReady
   autECLSession.autECLPS.SendKeys "[pf8]"
   call subSub1_()

end sub

Code: Select all

LANGUAGE=VBSCRIPT
DESCRIPTION=

[PCOMM SCRIPT HEADER]

Language = VBSCRIPT

Description =

[PCOMM SCRIPT SOURCE]

Option Explicit

autECLSession.SetConnectionByName (ThisSessionName)

OrisWriteOff



Sub OrisWriteOff()
PutText 20, 42, "20"
PressEnter
PutText 17, 44, "7"
PressEnter
PutText 5, 72, "001999"
PressEnter
PutText 9, 2, "s"
Update
PutText 11, 2, "s"
Update
PutText 13, 2, "s"
Update
PutText 15, 2, "s"
Update
PutText 17, 2, "s"
Update
PutText 19, 2, "s"
Update
PutText 21, 2, "s"
Update
PutText 1, 1, "[pf8]" 'move to next screen which will hopefully go inside (at the end of) the update function, before recalling itself over and over??
End Sub



Function WaitInpApp()

autECLSession.autECLOIA.WaitForAppAvailable

autECLSession.autECLOIA.WaitForInputReady

Do Until autECLSession.autECLOIA.InputInhibited = 0

autECLSession.autECLOIA.WaitForInputReady

autECLSession.autECLOIA.WaitForAppAvailable

Loop

autECLSession.autECLOIA.WaitForInputReady

autECLSession.autECLOIA.WaitForAppAvailable

Do While autECLSession.Ready = False

autECLSession.autECLOIA.WaitForInputReady

autECLSession.autECLOIA.WaitForAppAvailable

Loop

End Function



Function WaitCursor()

WaitInpApp

autECLSession.autECLPS.WaitForCursor 12, 21

End Function



Function PutText(rw, cl, txt)

WaitInpApp

autECLSession.autECLPS.SetCursorPos rw, cl

autECLSession.autECLPS.SendKeys "[eraseeof]"

autECLSession.autECLPS.SendKeys txt

End Function



Function PressEnter()

WaitInpApp

PutText 1, 1, "[enter]"

End Function

Function Update()
WaitInpApp
PutText 1, 1, "[pf2]"
PutText 1, 1, "[pf2]"
PutText 1, 7, "01"
PutText 1, 1, "[pf2]"
PutText 1, 1, "[pf9]"
End Function
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBScript looping

Post by HansV »

Try

Code: Select all

  For i = 9 To 21 Step 2
    PutText i, 2, "s"
    Update
  Next i
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

Thanks Hans, you make it look far too easy!

2 questions: Will I need the line "Dim i"? Also, I will put this to the test tomorrow, but is there anything in the attached code that would make you think that this would not work? (Based upon the recorded code in my first post). In particular, is it acceptable to have the function call itself over and over the way I have added in your loop? I will need to build in an exit somewhere, but I can work on that once I have the basic concept working.

Code: Select all

LANGUAGE=VBSCRIPT
DESCRIPTION=
[PCOMM SCRIPT HEADER]
Language = VBSCRIPT
Description =
[PCOMM SCRIPT SOURCE]
Option Explicit
autECLSession.SetConnectionByName (ThisSessionName)
OrisWriteOff

Sub OrisWriteOff()
PutText 20, 42, "20"
PressEnter
PutText 17, 44, "7"
PressEnter
PutText 5, 72, "001999"
PressEnter
Update
End Sub

Function WaitInpApp()
autECLSession.autECLOIA.WaitForAppAvailable
autECLSession.autECLOIA.WaitForInputReady
Do Until autECLSession.autECLOIA.InputInhibited = 0
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLOIA.WaitForAppAvailable
Loop
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLOIA.WaitForAppAvailable
Do While autECLSession.Ready = False
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLOIA.WaitForAppAvailable
Loop
End Function

Function WaitCursor()
WaitInpApp
autECLSession.autECLPS.WaitForCursor 12, 21
End Function

Function PutText(rw, cl, txt)
WaitInpApp
autECLSession.autECLPS.SetCursorPos rw, cl
autECLSession.autECLPS.SendKeys "[eraseeof]"
autECLSession.autECLPS.SendKeys txt
End Function

Function PressEnter()
WaitInpApp
PutText 1, 1, "[enter]"
End Function

Function Update()
WaitInpApp
For i = 9 To 21 Step 2
PutText i, 2, "s"
PutText 1, 1, "[pf2]"
PutText 1, 1, "[pf2]"
PutText 1, 7, "01"
PutText 1, 1, "[pf2]"
PutText 1, 1, "[pf9]"
Next i
PutText 1, 1, "[pf8]"
call Function Update()
End Function

:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBScript looping

Post by HansV »

In VBScript, you don't need to declare variables.

Your Update function will get stuck into an endless loop. You shouldn't let it call itself.
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

Yes. If you notice the recorded code, the sub is called over and over at the end of the routine. This also does not currently 'stop' automatically, we have to select the stop macro command in the application. There is a variable amount of pages to be worked each time.

I have several issues that I will still need to overcome. Firstly, there are 7 data rows that need to be updated, but only when there is data there, IOW, I need to handle a "stop" when a blank data line occurs. The last line(s) of the function PutText 1, 1, "[pf8]" is the 'select next page' command, (and then re-run function) which should only occur if I am not on the "last page"

I'm not sure exactly how this will work, but I suspect (HOPE) something like this.

Code: Select all

Function Update()
WaitInpApp
For i = 9 To 21 Step 2
x = GetText(i, 20, 1)
If x <> ""
PutText i, 2, "s"
PutText 1, 1, "[pf2]"
PutText 1, 1, "[pf2]"
PutText 1, 7, "01"
PutText 1, 1, "[pf2]"
PutText 1, 1, "[pf9]"
End If
Next i
x = GetText(20, 20, 9)
If x <> "Last Page"
PutText 1, 1, "[pf8]"
call Function Update()
Else
Exit Function
End Function

Function GetText(rw, cl, ln)
GetText = autECLSession.autECLPS.GetText(rw, cl, ln)
Set rw = Nothing
Set cl = Nothing
Set ln = Nothing
End Function

Thanks again for your support. I am making a lot of assumptions above, one being that I am thinking in terms of VBA as I am not farmiliar with VBScript, but it seems to be pretty similar in it's capabilities?
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBScript looping

Post by HansV »

VBScript is very similar to VBA in syntax, but it is more limited. Search Google or Bing for vba vs vbscript to learn about the resemblances and differences.
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

Thankyou Hans, I'll have a poke around.
:wales: Nathan :uk:
There's no place like home.....

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

HansV wrote:VBScript is very similar to VBA in syntax, but it is more limited. Search Google or Bing for vba vs vbscript to learn about the resemblances and differences.
Unless I have stumbled upon some rubbish in google, then the If.... then...... else syntax is not available in VBScript. So I will need to revise my approach. :groan:
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBScript looping

Post by HansV »

Stop! If ... Then ... Else is definitely available in VBScript, including EndIf:

Code: Select all

If condition1 Then
  Action1
ElseIf condition2 Then
  Action2
Else
  Action3
End If
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

Naughty man over here telling me porkies (unless I have my wrong eyes in again)
If.GIF
You do not have the required permissions to view the files attached to this post.
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBScript looping

Post by HansV »

Notice the subtle difference: the keywords aren't If and Else but #If and #Else. That's not the same!

#If is used for so-called conditional compilation. See Conditional Compilation (Visual Basic) and the links provided there.

Conditional compilation, with #If etc. is not available in VBScript. If ... Then ... Else ... *is* available!
Best wishes,
Hans

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

HansV wrote:Notice the subtle difference: the keywords aren't If and Else but #If and #Else. That's not the same!

#If is used for so-called conditional compilation. See Conditional Compilation (Visual Basic) and the links provided there.

Conditional compilation, with #If etc. is not available in VBScript. If ... Then ... Else ... *is* available!
Aaaah, thanks Hans. I did not give the # a thought. (I also did not see the *bear* dancing accross the basketball court) :evilgrin:

Hopefully I will be able to get this up and running tomorrow. Thanks again.
:wales: Nathan :uk:
There's no place like home.....

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16172
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: VBScript looping

Post by ChrisGreaves »

VegasNath wrote:Will I need the line "Dim i"?
Hans is correct; you don't need the Dim i
But I would add that it is Good Programming Practice to declare all identifiers, which means by inclusion, Dimensioning local variables.
Why do this?
Especially in languages such as VBA (I know you are in VBScript) where we can tell the compiler that all identifiers must be declared ("Option Explicit"), we receive an alert when we are playing, for example, with a variable "alpha" and use "alaph" by mistake.
The brain is a three-pound mass you can hold in your hand that can conceive of a universe a hundred billion light-years across (Marian C. Diamond)

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

Thankyou Chris, I will indeed do this so to avoid typo's. One of my main problems is that I have no idea how to step through the VBScript in my application, so debugging and setting up the code corectly is not easy. There must be a way, I just have not found it yet.

More important matters surfaced today, so I was not able to continue this project, but I hope that I can complete in the next few days.
:wales: Nathan :uk:
There's no place like home.....

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBScript looping

Post by VegasNath »

HansV wrote:Try

Code: Select all

  For i = 9 To 21 Step 2
    PutText i, 2, "s"
    Update
  Next i
Hans, there is no need for an "end for" here is there? There is no way to debug the code within my app, and for the life of me, can't get this working.

The stupid thing just tells me that there is an error on line .... which does not exist.
:wales: Nathan :uk:
There's no place like home.....

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBScript looping

Post by HansV »

Sorry, my mistake. A "For" statement is closed by a "Next" statement. In VB/VBA it is good practice to specify the loop variable after Next, in this example

For i = ...
...
Next i

But it turns out that you aren't allowed to put anything after Next in VBScript. So you should change

Next i

to simply

Next
Best wishes,
Hans