How to detect wrong username or password in SAP

Mistakes happen. There is always a possibility to type wrong your credentials. Also SAP from time to time ask You to change the password, so that already written in code will be out of date. In this article I’m going to show You how to detect wrong username or password typed in SAP.

Here I will only show the part of code with credentials, for the full code I invite You to Connect to SAP via Excel VBA article. But let’s go straight to the topic.

Deliver username and password to SAP

There are several approaches to pass credentials into code:
– user input form,
– simple worksheet,
– put the data straight into code.

In this case I put the credentials inside the code, shown below.

session.findbyid("wnd[0]/usr/txtRSYST-BNAME").Text = "username"
session.findbyid("wnd[0]/usr/pwdRSYST-BCODE").Text = "password"
session.findbyid("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"

After that You need to confirm them, for example using ENTER.

session.findbyid("wnd[0]").sendVKey 0

In case of correct credentials, You will go to the SAP session, or the session number check appears or copyright window (language change issue).

In other case, unfortunately, SAP will go no further and no additional window will appear.

Solution

But there is 1 thing, which tells You about mistake – status bar. After a typo the Name or password is incorrect (repeat logon) shows in the status bar.

So using simple conditional You can check if the credentials are correct or not.

If session.findbyid("wnd[0]/sbar").Text = "Name or password is incorrect (repeat logon)" Then
    MsgBox "Name or password is incorrect"
    session.findbyid("wnd[0]/tbar[0]/btn[15]").press
    Exit Sub
End If

In this example, if above condition is met, firstly appear message box with Name or password is incorrect text, secondly code close SAP session and macro ends its work.

Summary

You need to put this code right after the logon credentials. It can be before the session number check and copyright window code line.

session.findbyid("wnd[0]/usr/txtRSYST-BNAME").Text = "username"
session.findbyid("wnd[0]/usr/pwdRSYST-BCODE").Text = "password"
session.findbyid("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"

session.findbyid("wnd[0]").sendVKey 0

If session.findbyid("wnd[0]/sbar").Text = "Name or password is incorrect (repeat logon)" Then
    MsgBox "Name or password is incorrect"
    session.findbyid("wnd[0]/tbar[0]/btn[15]").press
    Exit Sub
End If

On Error Resume Next
    session.findbyid("wnd[1]/tbar[0]/btn[0]").press 'language version
On Error GoTo 0

Now, even if You do a typo in credentials, You can detect wrong username or password while logging into SAP session. Now nothing will worry You and your code.

Tomasz Płociński

Author: Tomasz Płociński

Excel VBA enthusiast who is also open for other languages. Mainly working in VBA, some SQL, hungry for more.

Leave a Reply

Your email address will not be published. Required fields are marked *