VBScript Loop Question

I have the following code that allows users to lookup available user ID's, but I would like to have it go back to original Input Box when an ID is not available to start the search again. I am not quite sure how to setup the loop.

strUserName = "Prefix" & InputBox("Please Enter 4 digit ID without dashes.", "ID Lookup")

dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
 
objCommand.CommandText = _
"<LDAP://OU=TEST,OU=TEST,DC=TEST,DC=NET>;" & _  
"(&(objectCategory=User)" & "(samAccountName=" & strUserName & "));samAccountName;subtree"  

Set objRecordSet = objCommand.Execute
Do 
 If objRecordset.RecordCount = 0 Then
    WScript.Echo "Prefix" & strUserName & " is available."
    Exit Do
 Else 
    WScript.Echo strUserName & " is not available"  
    Exit Do
End If
Loop
objConnection.Close

Open in new window

LVL 1
ajl7519Asked:
Who is Participating?
 
Bill PrewCommented:
How about this:

dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Do 
  strUserName = "Prefix" & InputBox("Please Enter 4 digit ID without dashes.", "ID Lookup")

  If strUserName = "" Then
    Exit Do
  End If

  objCommand.CommandText = _
    "<LDAP://OU=TEST,OU=TEST,DC=TEST,DC=NET>;" & _  
    "(&(objectCategory=User)" & "(samAccountName=" & strUserName & "));samAccountName;subtree"  

  Set objRecordSet = objCommand.Execute

  If objRecordset.RecordCount = 0 Then
    WScript.Echo "Prefix" & strUserName & " is available."
  Else 
    WScript.Echo strUserName & " is not available"  
  End If
Loop

objConnection.Close

Open in new window

~bp
0
 
Robert SchuttSoftware EngineerCommented:
couple of problems with that:

- the test for an empty string is never true
- if I read the question correctly, the loop should stop when the username is available, possibly do something after the loop like create the account

here's my adaptation:

dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Do 
  strUserName = InputBox("Please Enter 4 digit ID without dashes.", "ID Lookup")

  If strUserName = "" Then
    Exit Do
  End If

  If Len(strUserName) = 4 And IsNumeric(strUserName) Then

    strUserName = "Prefix" & strUserName
  
    objCommand.CommandText = _
      "<LDAP://OU=TEST,OU=TEST,DC=TEST,DC=NET>;" & _  
      "(&(objectCategory=User)" & "(samAccountName=" & strUserName & "));samAccountName;subtree"  

    Set objRecordSet = objCommand.Execute

    If objRecordset.RecordCount = 0 Then
      WScript.Echo strUserName & " is available."
      Exit Do
    Else 
      WScript.Echo strUserName & " is not available"  
    End If

  End If
Loop

objConnection.Close

Open in new window

0
 
Bill PrewCommented:
==> - the test for an empty string is never true

I don't understand that comment.  If the user doesn't enter any characters, and just hits enter then that condition certainly will be true, and the loop will exit.

~bp
0
 
ajl7519Author Commented:
I got it working. I still needed the Exit Do when it found an available name, but it works fine.

Thanks!
0
 
Bill PrewCommented:
Welcome, thanks.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.