Solved

VBS Input box to display warning/error if empty

Posted on 2010-11-18
8
1,465 Views
Last Modified: 2012-05-10
Hi I am working on a simple script below to ask a user a few questions.

On the input boxes I would like it to come with an error if the users does not input anything into the boxes and not go to the new step until the user has entered data.

if inbox is empty then error

Thanks!
Dim kNameFirst

Dim kNameLast

Dim EmailAdd

MsgBox "welcome"

kNameFirst = InputBox("first name")

kNameLast = InputBox("last name")

EmailAdd = InputBox("email?")

MultiUsrPC = InputBox("shared pc?")



Dim objNetwork, objDrives, intIndex, objFSO, objFile, objUser, strUsername

Set objSysInfo = CreateObject("ADSystemInfo")

Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

strUsername = objUser.samAccountName



Set objFSO = CreateObject("Scripting.FileSystemobject")

Set objFile = objFSO.OpenTextFile("\\shared\results.csv", 8, True)

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 

Set colComputer = objWMIService.ExecQuery("Select Name from Win32_ComputerSystem")

Set colComputerIP = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration")



For Each objComputer In colComputer

    strHostName = objComputer.Name

Next

For Each IPConfig In colComputerIP

	If Not IsNull(IPConfig.IPAddress) Then 

        For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)

			If strIPAddress = "" Then

				strIPAddress = IPConfig.IPAddress(intIPCount)

			Else

				strIPAddress = strIPAddress & " | " & IPConfig.IPAddress(intIPCount)

			End If

		Next

	End If

Next



Set objNetwork = CreateObject("WScript.Network")

Set objDrives = objNetwork.EnumNetworkDrives

For intIndex = 0 To objDrives.Length - 1 Step 2

        objFile.WriteLine  """" & strUsername & """,""" & kNameFirst & """,""" & kNameLast & """,""" & EmailAdd & """,""" & strHostName & """,""" & strIPAddress & """,""" & MultiUsrPC & """,""" & objDrives(intIndex) & """,""" & objDrives(intIndex + 1) & """"

Next

objFile.Close

Set objFile = Nothing

Set objUser = Nothing

Set objFSO = Nothing

Set objDrives = Nothing

Set objNetwork = Nothing



MsgBox "Complete",0,"Complete"

Open in new window

0
Comment
Question by:Eirejp
  • 4
  • 4
8 Comments
 
LVL 10

Accepted Solution

by:
hosneylk earned 350 total points
ID: 34170881
Try something like this
Do

   kNameFirst = InputBox("first name")

   If Len(kNameFirst) = 0 Then MsgBox "Error"

Loop While Len(kNameFirst) = 0

Open in new window

0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34170890
or add a function
Function getInput(msg)

   Do

      inputVal = InputBox(msg)

      If Len(inputVal) = 0 Then MsgBox "Error"

   Loop While Len(inputVal) = 0

   myfunction = inputVal 

End Function



and call it like this



kNameFirst = getInput("first name")

Open in new window

0
 
LVL 1

Author Closing Comment

by:Eirejp
ID: 34170900
Thanks!
0
 
LVL 1

Author Comment

by:Eirejp
ID: 34170905
Actually one last thing. Sorry I know I already close the question.

Now that the look works how about if a user press the cancel button to exit? Is that possible.

if blank then error
if cancel then exit
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

Expert Comment

by:hosneylk
ID: 34170931
try this..
Do

   kNameFirst = InputBox("first name")

   If Len(kNameFirst) = 0 Then MsgBox "Error"

   If VarType(kNameFirst ) = vbEmpty Then End

Loop While Len(kNameFirst) = 0

Open in new window

0
 
LVL 1

Author Comment

by:Eirejp
ID: 34170970
I get a expected statement error

Do
      kNameFirst = InputBox(firstname)
      if Len (kNameFirst) = 0 Then MsgBox "Error please enter something"
      if VarType (kNameFirst) = vbEmpty Then End
Loop While Len(kNameFirst) = 0
0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34171038
that was kinda pseudo code.

replace End with WScript.Quit (1)
0
 
LVL 1

Author Comment

by:Eirejp
ID: 34171067
Fantastic! Thank you
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now