Using Multiple Sessions 

Use

You can use up to six sessions for every R/3 connection you have. The first session is always created when you establish a connection to the R/3 application server.

You can only have one connection is SAPGUI Front is running.

The following procedure shows how to handle multiple R/3 sessions.

Procedure

  1. Ask for a new connection to R/3, by using the Connect method of SapEvent.
  2. You can optionally specify that the standard SAPGUI is to be displayed. In this case, a SAPGUI and one Front are invoked.

  3. Log onto the SAP system, by using the Logon method of SapEvent.
  4. Handle the event of a new session being created, by assigning the new session object to a variable.

The new session can be created as a result of an end user asking for it, by using the menu option SystemàCreate session, for example.

Example

The following example handles two sessions.

The main program handles the connection to R/3 and handles the first session. The first session is in myEvt. In the first session, the program invokes a specific transaction, namely transaction bibs.

The second session is in sap2. In the second session, the program invokes another R/3 transaction, namely transaction se38.

Dim WithEvents myEvt As SapEvent
Public sap2 As SapEvent
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub ScreenCheck(ByVal ProgName As String, _
ByVal ScrName As String)
If myEvt.ProgramName <> ProgName Or _
(Len(ScrName) > 0 And myEvt.ScreenName <> ScrName) Then
MsgBox "Unexpected screen " & myEvt.ProgramName & " " & myEvt.ScreenName, vbCritical
Stop
End If
End Sub

Private Sub OKCheck(ByVal bIsOK As Boolean, ByVal sMsg As String)
If Not bIsOK Then
MsgBox sMsg, vbCritical
Stop
End If
End Sub

Sub Sessns()
Dim iCtrl As Integer
Dim bOK As Boolean
'Create the GUI Component object:
' When using the OCX, use the following line
Set myEvt = CreateObject("SapAutoGui.Control.1")

'Connect to R/3
' Replace host name with the actual value:
bOK = myEvt.Connect("myhost", "sysnum", SapGuiMerlin Or SapGuiFront Or SapGuiFullMenu)
OKCheck bOK, "Error in opening connection"
'Set the dimensions of the SAPGUI window
myEvt.RowDimension = 24
myEvt.RowListDimension = 24
myEvt.ColumnDimension = 80
myEvt.ColumnListDimension = 80
myEvt.SetSizeFlag = True
OKCheck bOK, "Error in opening connection"
' Logon to the first session
' Replace client username and password with actual values
bOK = myEvt.Logon("client", "user", "passwd", "en")
OKCheck bOK, "Error in logon"

myEvt.OKCode = "bibs" ' go to transaction bibs
bOK = myEvt.SendEvent
OKCheck bOK, "Error in sending default key"
ScreenCheck "SAPMBIBS", "0100"
' Create a new session as a user would
bOK = myEvt.SendMenuName("System") ' index 24
OKCheck bOK, "Error in sending menu"
bOK = myEvt.SendMenuName("Create Session") ' index 44
OKCheck bOK, "Error in sending menu"

'The second session
Sleep (1500)
If Not sap2 Is Nothing Then
bOK = sap2.GetEvent()
sap2.OKCode = "se38" 'go to transaction SE38
bOK = sap2.SendEvent
End If
End Sub
Private Sub myEvt_OnNewSession(ByVal sapEvt As Object)
Set sap2 = sapEvt
End Sub

To log off the R/3 system, use the following code:
' Use the next line for standalone SAP program
' myEvt.Quit
bOK = myEvt.Logoff()

See Also

Handling multiple sessions when using the GUI Library