Link to home
Start Free TrialLog in
Avatar of Stefan Motz
Stefan MotzFlag for United States of America

asked on

VBScript If Statement - Trim X Letter after \

The script below captures the logged in user's domain and employee number.
This can be letters followed by a backslash \
e.g. ABC\12345
ABCD\123456
ABCDE\E539Z
etc.
The characters after the \ is the employee number.
The following line trims the domain and \, and keeps only the employee number:

UserID = Mid(UserID,instr(UserID,"\")+1)

Unfortunately there is an exception, and I need to add an if statement.
Some employee numbers have an X letter prefix, and I need to trim the X too.
For example if the employee number is X123456 I need only 123456 returned.
i.e if the logged in user is ABC\X123456 I need to trim ABC\X and leave only 123456

This is the only exception; for all the other logins I just need to trim the domain and \
like the script below shows.
Would you please help me out with an if statement that would do that for me?
Thank you.
<%
If Request.ServerVariables("LOGON_USER") = "" Then
Response.Status = "401 Access Denied"
Response.End
End If
%>
<%
set rs = Server.CreateObject("ADODB.recordset")
Dim UserID
UserID = Request.ServerVariables("LOGON_USER")

UserID = Mid(UserID,instr(UserID,"\")+1)



rs.Open "exec GetEmployees'" & UserID & "'", Conn

do until rs.EOF
Emp_Id = rs("Emp_Id")
First_Name = rs("First_Name")
Last_Name = rs("Last_Name")


rs.MoveNext
loop
rs.Close
Set rs = Nothing
Conn.Close
Set Conn = Nothing
%>

Employee Number: <%=Emp_Id%>
<br />
First Name: <%=First_Name%>
<br />
Last Name: <%=Last_Name%>

Open in new window

Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Add this between lines 10 and 12.

UserId = Replace(UserId, "\X", "\")
SOLUTION
Avatar of byundt
byundt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stefan Motz

ASKER

This was the only solution that worked for me, but thank you very much all of you.
Thank you for the points!  

All the solutions here should work equally well.  I do like the solution by MartinLiss.  Perhaps this statement with the replace after you split out the UserID.

UserID = Mid(UserID,instr(UserID,"\")+1)
UserID= replace(UserID,"X")

At least you have it working.
I've tried again all solutions and I see that the first two didn't work because the X is case sensitive. When I changed it to lower case, all three solutions worked.
I'm not sure how to make the first two solutions work both with lower case and upper case X
I will post the question.
Thanks again
Yes, Byundt's solution worked like this:
If Left(UserID, 1) = "u" or Left(UserID, 1) = "U" Then UserID = Mid(UserID, 2)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much, I'm sorry I didn't distribute the points to all of you.
It doesn't matter all that much to me but if you want to you can 'Request Attention' and ask the moderator to reopen the question so that you can do that.
If you want to spread the points, I think that is a good idea.  All  good.
Thank you very much again; I'm really grateful to all of you.
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013