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.

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

Leave a Reply

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