[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript Loop Question

Posted on 2011-10-20
5
Medium Priority
?
301 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ajl7519
  • 3
5 Comments
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1000 total points
ID: 37002103
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
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 1000 total points
ID: 37024468
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37025141
==> - 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
 
LVL 1

Author Comment

by:ajl7519
ID: 37025431
I got it working. I still needed the Exit Do when it found an available name, but it works fine.

Thanks!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37025529
Welcome, thanks.

~bp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

825 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