Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Scratching my head for hours trying to work this out

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40861
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Scratching my head for hours trying to work this out
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Scratching my head for hours trying to work this out - 12/13/2006 2:40:26 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
I'm building a variable based on user input in 3 InputBoxes.  I take the results of InputBox 1, 2 and 3 and store then in a new variable like this:

varNew = ib1 & "-" & ib2 & "-" & ib3

So if the user inputs AAA to box 1, BBB to box 2 and CCC to box 3, varNew would have:

AAA-BBB-CCC

This is perfect for what I need.  However my problem comes if a user doesn't enter anything in 1 more InputBoxes (which might legitimately happen).

Say they leave IB1 blank, but put BBB in IB2 and CCC in IB3.  varNew would now be:

-BBB-CCC

And if they left, say, IB2 and IB3 blank, but put AAA in IB1, varNew would be:

AAA--

My problem is I don't want to include the dashes if a box was left blank.  But any possible solution is beyond me, I can't see a way round it.
 
 
Post #: 1
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 2:43:46 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
Use an array to hold the responses then Join() them with a - at the end.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to markmcrobie)
 
 
Post #: 2
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 3:10:32 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Thanks, but I'm not sure how to go about that (complete beginner here - see my post from yesterday!)

How would using an array allow me to store a "-" between the various inputs?

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 3:32:27 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
I thought about it some more and Join() would not be best. Here is an example that would do what you want I believe:

Option Explicit

Dim strResponse
Dim strResult
Dim arrResponses(2)

arrResponses(0) = InputBox("Enter One")
arrResponses(1) = InputBox("Enter Two")
arrResponses(2) = InputBox("Enter Three")

For Each strResponse In arrResponses
   If strResponse <> "" Then
       strResult = strResult & strResponse & "-"
   End If
Next

If strResult <> "" Then strResult = Left(strResult, Len(strResult)-1)

WScript.Echo strResult

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to markmcrobie)
 
 
Post #: 4
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 3:49:12 AM   
  ehvbs

 

Posts: 2222
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
Hi markmcrobie,

Here is another way, using an Array and Join():


      

You may have to look up ReDim Preserve, UBound, and Join in the VBScript Docs, but
that's well worth it.

I was eager to answer to this question, because I liked the 'first script' you posted yesterday.
Regrettably I don't have the time to comment it now, but it is on my todo list.

ehvbs

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 3:57:45 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Wow, thanks for the kind words about my first script, I feel honoured!

And thanks to all for the replies.  What I'm trying to do with this question is part of my script that I posted yesterday.

FYI here's my script from yesterday with some amendments from work I've done on it today:

And please accept my apologies for not using the code tags yesterday!

Also, regarding VBScript docs, I download the .chm file from Microsoft, but no matter which pages I try to open in it I get "Page cannot be displayed" error.


      

(in reply to ehvbs)
 
 
Post #: 6
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 4:18:42 AM   
  ehvbs

 

Posts: 2222
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
Hi markmcrobie,

"Page cannot be displayed" may be caused by putting it on a drive considered
'foreign' (mapped) by Windows.

Good luck!

(in reply to markmcrobie)
 
 
Post #: 7
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 4:19:44 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
There are a few If statements where you don't really need to use the "else" in.

i.e.

If re2.Test(strNewFullName) then
  strNewFullName = re2.replace(strNewFullName, " ")
Else
End If

you can simply have it as

If re2.Test(strNewFullName) then
     strNewFullName = re2.replace(strNewFullName, " ")
End If

As for the CHM, you normally get that error when you try to view it off a site or a network share.  It should work if you download it to your local drive and launch it from there. There is also a registry tweak you can make to fix this. see: http://support.microsoft.com/kb/896054

(in reply to markmcrobie)
 
 
Post #: 8
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 5:35:20 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
I did download it, I was running the .chm from my desktop.  Don't know if it matters, but it was on my work's computer.

The path to the .chm file was C:\Documents and Settings\mark.mcrobie\Desktop (running XP Pro)

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 5:37:11 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Also are you saying you never need to use "Else" if there's nothing following it except "End If"?

Thanks a lot, I'm learning already!

(in reply to markmcrobie)
 
 
Post #: 10
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 6:13:10 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Yes, you do not need the "else" if you don't have anything following it other than the "end if". In fact, if your "IF" statement is simple enough you do not even need the "end if".

simple example. As you can see the whole IF statement is on one line without an "else" or "end if" and the value of str was changed because it matched the criteria I was looking for.

Dim str

str = "Just some text."

If InStr(str, "some") Then str = "Something else"

WScript.Echo str

(in reply to markmcrobie)
 
 
Post #: 11
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 6:28:57 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Cool - so when would End If be absolutely necessary?

(in reply to dm_4ever)
 
 
Post #: 12
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 6:56:38 AM   
  Country73


Posts: 735
Score: 10
Status: offline
Here's some help on viewing your CHM file:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\HTMLHelp\1.x\ItssRestrictions
MaxAllowedZone = dword 1

(in reply to markmcrobie)
 
 
Post #: 13
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 7:09:35 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You would absolutely use the "end if" if you want several things to happen and putting it on one line simply wouldn't make sense. on the same lines of the previous example.

Dim str

str = "Just some text."

If InStr(str, "some") Then
   str = "Something else"
   WScript.Echo str
   testsub
   WScript.Echo testfunction(str)
End If

Sub testsub
   WScript.Echo "test sub"
End Sub

Function testfunction(strIn)
   testfunction = UCase(strIn)
End Function

(in reply to markmcrobie)
 
 
Post #: 14
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 7:19:51 AM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Or, you could put them all on one line using :'s


      


_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to dm_4ever)
 
 
Post #: 15
 
 RE: Scratching my head for hours trying to work this out - 12/13/2006 8:04:56 AM   
  dm_4ever


Posts: 2721
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You are absolutely right Digital. I like using the ":" on certain things, but it looks a lot neater and easier to read when you have them on seperate lines.  It is a good thing to show though. 

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 16
 
 RE: Scratching my head for hours trying to work this out - 12/14/2006 12:49:18 AM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
No doubt.  It makes things nicer here and there, but - as I think I showed - it *can* be taken to extremes!

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to dm_4ever)
 
 
Post #: 17
 
 RE: Scratching my head for hours trying to work this out - 12/14/2006 1:01:00 AM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Thanks for the tips.

This bit is bugging me from the suggestion posted earlier:

For Each strResponse In arrResponses
  If strResponse <> "" Then
      strResult = strResult & strResponse & "-"
  End If
Next

What does it mean "For Each strResponse"? Nothing is done with strResponse until *after* the For... statement.  So how does the code know how many times to loop?


(in reply to dm_4ever)
 
 
Post #: 18
 
 RE: Scratching my head for hours trying to work this out - 12/14/2006 1:07:21 AM   
  ehvbs

 

Posts: 2222
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
I prefer

    Dim vVar : vVar  = initialize()

to

    Dim vThis, vThat, vVar, ...        Or   Dim vThis
                                                              Dim vVar
     .... many lines of code ....

     vVar = initialize()

because it reduces the lines you have to look at, if you want to know 'everything about vVar'.

Sometimes I use

     .... few lines of faily decent code ....
     ['] CleanUp() : KeepBooks() : Exit Function|For
     .... many lines of code not to trusted yet  ....

to enable/disable a premature exit with just one '. I try to coax myself to use

     .... few lines of faily decent code ....
     If True Then Message() : CleanUp() : KeepBooks() : Exit Function|For : End If
     .... many lines of code not to trusted yet  ....

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 19
 
 RE: Scratching my head for hours trying to work this out - 12/14/2006 1:20:02 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
For Each strResponse In arrResponses
  If strResponse <> "" Then
      strResult = strResult & strResponse & "-"
  End If
Next

This code is treating the array as a collection. Behind the scenes, the compiler is really doing this:

For i = 0 To UBound(arrResponses)
strResponse = arrResponses(i)
If strResponse <> "" Then
   strResult = strResult & strResponse & "-"
End If
Next

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Scratching my head for hours trying to work this out Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts