Simplify SAP R/3 development with Excel VBA/VB RFC  
Home> Sap_Active_X>Log on by Function Control


Log on by Function Control

Most popular way to logon to SAP is to go with ActiveX object. This utilizes the ActiveX module in :...SAP/FrontEnd/Controls/wdtfuncs.OCX.

(A) Logon to the system hard coded in the VBA/VB macro
If you want to hide the logon system, user name or password from end user, you can put them in this VBA/VB macro and lock with password.
Sub TempLogon1()

Dim sapConn As Object 'Declare connection object
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

'Specify user
sapConn.Connection.user = "USER"
'Then password
sapConn.Connection.Password = "PASSWORD"
sapConn.Connection.client = "001"
'Target server address
sapConn.Connection.ApplicationServer = ""
'Language code
sapConn.Connection.Language = "EN"

'Finally, try to logon to the specified system and check if the connection established
If sapConn.Connection.Logon(0, True) <> True Then
  MsgBox "Cannot Log on to SAP" 'Issue message if cannot logon
  MsgBox "Logged on to SAP!"
End If

End Sub

(B) Logon to SAP with dialog
Here we will see how we do the server selection with dialog box like saplogon. In case if the system is fixed or the number of the selectable system is as little enough to control with user defined form then the above works ok. But you may need users to select the target system actively from the list of servers. Then this code doesn't work. (or work with effort on the dialog module creation) As written in the below code, if you specify "False" in the second parameter for Logon method, then the dialog screen below will come up and users can select the system from it. The dialog will derive the servers and settings from the saplogon.
Sub TempLogon2()

Dim sapConn as object
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

If sapConn.Connection.Logon(0, False) <> True Then 'Try Logon
  Msgbox "Cannot Log on to SAP"
end if

End Sub

(C) Logon to SAP with dialog
Instead of let the system to do the whole process, you may want to let users to use SAP screen during RFC. By giving True to the RfcWithDialog property, SAP screen will be ready hidden in background. This is required in case if you try to execute function module which will show SAP screen (e.g. CAD dialog).
Sub TempLogon3()

Dim sapConn as object
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

sapConn.Connection.RfcWithDialog = True
If sapConn.Connection.Logon(0, False) <> True Then 'Try Logon
  msgbox "Cannot Logon to SAP"
end if

End Sub

(D) Appendix - Methods for connection object
Create another session from Same connection.
Show last error issued by SAP.
Logoff from SAP for the connection.
Logon to SAP.
ReconnectNone (Boolean) Re-Logon to SAP with same user, password and etc information which were provided last time.
SystemInformation hWnd (Void) Show system information in detail with built-in dialog.
SystemMessages None (Void) Show system messages.

(E) Appendix - Properties for connection object
ABAPDebugBoolean @
ApplicationServer String Logon Application server name.
AutoLogonLong @
ClientString Logon client. (Read/Write)
CodePageString Logon code page. (Read/Write)
ConnectionHandle Long Connection handle number. (Read/Write)
DestinationString Destination name (xxx)
GatewayHostString Gateway host name
GatewayService String Gateway service name
GroupNameString Group name (xxx)
GroupSelection Boolean @
GRTDataString @
HostNameString Turn on ABAP Debug
IsConnectedEnum(Long) Represents connection status with following values (Read)
tloRfcNotConnected = 0
tloRfcConnected = 1
tloRfcConnectCancel = 2
tloRfcConnectFailed = 8
tloRfcConnectParameterMissing = 4


Logon language (Read/Write)


Turn on ABAP Debug
MessageServer String Turn on ABAP Debug
ParentObject Turn on ABAP Debug
PasswordString Password (Read/Write)
RfcWithDialog Long Connection go through SAP GUI.
*Any other values than 0 will be treated as "On". (Read/Write)
SAPReleaseString SAP Release of connected system (Read)
SAPRouterString @
SNCBoolean SNC enabled connection. (Read/Write)
SNCNameString SNC name
SNCQualityString @
SystemString @
SystemIDString System ID name
SystemNumber Long Logon system number. (Read/Write)
TicketString @
TraceLevelLong @
UseDefaultSystem Boolean @
UserString User name. (Read/Write)
UseSAPLogonIni Boolean Reference to Saplogon.ini file
UseSAPRFCIni Boolean Reference to Saprfc.ini file