Solved

VBS Input box to display warning/error if empty

Posted on 2010-11-18
8
1,563 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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