Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reuse of Sub 3

Status
Not open for further replies.

JadeKnight

IS-IT--Management
Feb 23, 2004
94
NO
I've in middel of creating a script wich is getting some input from a user. The input is parsed, and if the input isn't in a specific format I would like to launch the initial inputbox. The input box is located in a sub.
However, when I try to launch the sub second time nothing happends. I've also tried to replace it with a function with same result.
 
Can you post the actual code?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Here is a function I use, if the user does not input something, then it will re-ask. You can modify it to check for your specific requirements.

'This function will return information entered into an inputbox
'It will recall the function if the user enters nothing or clears the default
'It will close the script if the user hits the cancel button
Function GetInfo(ask,default)
getInfo= InputBOx(ask,"MisterNiceGuy Scripting Solutions",default)
emptyAsk="Sorry you must enter something" &vbCr &"Or press Cancel to quit." &vbCr &vbCr
Ask = replace(Ask,EmptyAsk,"")
If getInfo = vbEmpty Then WScript.Quit
If getInfo = "" Then getInfo EmptyAsk & Ask,""
End Function
 
Ok, here's the code. The problem is located at line 41.

Code:
Option Explicit
Dim WshShell,PopQuestAbort,PopComputerAccountErr
Dim inpComputerAccount,inpGUID
Dim strChkComputerAccount,strChkGUID,ReRun

Dim strMsgHeader,strMsgComputerAccount,strMsgGUID
Dim strPopUpHeader,strPopQuestAbort,strComputerAccountErr,strGUIDErr

strMsgHeader="Some Header"
strMsgComputerAccount=    "Correct string would be Check111111. Please input wrong string"
strPopUpHeader=    "Confirm"
strPopQuestAbort="Abort?"
strComputerAccountErr="Wrong format, please input Check111111"


Const CheckString="CHEC"

Set WshShell = WScript.CreateObject("WScript.Shell")

If inpComputerAccount="" Then
    GetComputerAccount
End If

'Analyze of input


inpComputerAccount=UCase(inpComputerAccount)
inpGUID=UCase(inpGUID)
strChkComputerAccount=Right(inpComputerAccount, 4)
strChkComputerAccount=Left(strChkComputerAccount, 1)

If Len(inpComputerAccount)=11 Then
    If Left(inpComputerAccount, 4)=CheckString Then
        If strChkComputerAccount=1 Then
        'strChkComputerAccount="OK"
        wscript.echo "ComputerAccount OK"
        End If
    End If
Else PopComputerAccountErr=WshShell.Popup(strComputerAccountErr,,strPopUpHeader,4 + 16)
        Select Case PopComputerAccountErr
            Case 6 GetComputerAccount
            Case 7 wscript.quit
        End Select
End If



'Getting ComputerAccount
Sub GetComputerAccount
Do While inpComputerAccount=""
inpComputerAccount = InputBox (strMsgComputerAccount,strMsgHeader)

If inpComputerAccount="" Then
    PopQuestAbort=WshShell.Popup(strPopQuestAbort,,strPopUpHeader,4 + 32)
        Select Case PopQuestAbort
            Case 6    wscript.Quit
        End Select
End If

Loop
End Sub
 
Lack of continuation line ? _

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
It looks like you only tell it to do the sub one time. Try changing this:
Code:
If inpComputerAccount="" Then
    GetComputerAccount
End If
To this:
Code:
Do While inpComputerAccount=""
    GetComputerAccount
Loop

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Lack of continuation line ? _

Nope...

Code:
Do While inpComputerAccount=""
    GetComputerAccount
Loop

Nope, because it isn't enough that it's empty. The string may be wrong...

However, is it possible to use a sub several times? Or do I need antoher approach?
 
It is possible to use a sub several times. That is one of the primary points for using them in the first place. If you want it to continue until a valid name is enetered, then use the Do WHile, but make the condition a boolean that is set to true when the computername is valid. Make sure that you put the validation check inside the loop.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I understand, but in some cases there would be different procedures wich would invoke same sub. This isn't possible? Is that the reason for the fail in this script?
 
Hello JadeKnight,

The logic is lacking clarity, I have to say. If this modification works, it leaves behind an extreme discomfort. So I would propose you do a "big"-scale regrouping.
Code:
Option Explicit
Dim WshShell,PopQuestAbort,PopComputerAccountErr
Dim inpComputerAccount,inpGUID
Dim strChkComputerAccount,strChkGUID,ReRun

Dim strMsgHeader,strMsgComputerAccount,strMsgGUID
Dim strPopUpHeader,strPopQuestAbort,strComputerAccountErr,strGUIDErr

strMsgHeader="Some Header"
strMsgComputerAccount=    "Correct string would be Check111111. Please input wrong string"
strPopUpHeader=    "Confirm"
strPopQuestAbort="Abort?"
strComputerAccountErr="Wrong format, please input Check111111"


Const CheckString="CHEC"
[blue]inpComputerAccount=""[/blue]
Set WshShell = WScript.CreateObject("WScript.Shell")

[red]do while inpComputerAccount=""[/red]
If inpComputerAccount="" Then
    GetComputerAccount
End If

'Analyze of input


inpComputerAccount=UCase(inpComputerAccount)
inpGUID=UCase(inpGUID)
strChkComputerAccount=Right(inpComputerAccount, 4)
strChkComputerAccount=Left(strChkComputerAccount, 1)

If Len(inpComputerAccount)=11 Then
    If Left(inpComputerAccount, 4)=CheckString Then
        If strChkComputerAccount=1 Then
        'strChkComputerAccount="OK"
        wscript.echo "ComputerAccount OK"
        End If
    End If
[red]Else[/red]
    PopComputerAccountErr=WshShell.Popup(strComputerAccountErr,,strPopUpHeader,4 + 16)
        Select Case PopComputerAccountErr
            Case 6 [red]inpComputerAccount=""[/red]
            Case 7 wscript.quit
        End Select
End If
[red]loop[/red]

'Getting ComputerAccount
Sub GetComputerAccount
Do While inpComputerAccount=""
inpComputerAccount = InputBox (strMsgComputerAccount,strMsgHeader)

If inpComputerAccount="" Then
    PopQuestAbort=WshShell.Popup(strPopQuestAbort,,strPopUpHeader,4 + 32)
        Select Case PopQuestAbort
            Case 6    wscript.Quit
        End Select
End If

Loop
End Sub
regards - tsuji
 
Please review the post I made above, in the function I use to return a value, I call the function from itself until it provides the correct type of response. There is a point at which I check for the value to be other than "" , at that point you can verify the value using whatever specification you desire. Again, please take a look at the simple multi-use function.
 
Thx to all of you, for taking time to help me out :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top