Search code examples
vbahostcursor-positionattachmate-reflection

IBM Reflection programming- MoveCursor function issue


In IBM Reflection host, i am trying to run a VBA macro for traversing through different screens and fill required information (in a predefined template kind of a screen) to create orders in the system.

In the process, i am trying to utilise the functions- "Session.MoveCursor" and "Session.CursorRow", "Session.CursorColumn" for moving the cursor to a desired position and then reading the cursor position information (to verify the position before writing data on HOST screen)

Code:

Dim currRowPos as Integer
Dim currColPos as Integer
Session.MoveCursor targetRowPos, targetColPos ' move cursor position
DoEvents ' custom logic to wait or inlcude delay 1 sec or more, mentioned only single code statement here 
currRowPos = Session.CursorRow 'get cursor current row position
currColPos = Session.CursorColumn 'get cursor current column position
'check current cursor position and write data onto HOST screen
If targetRowPos = currRowPos And targetColPos = currColPos Then
    Session.TransmitANSI "xyz" 'write xyz on HOST screen
End If

I am trying to write some information on to Host at the desired cursor position and traverse to next screen. In the due process, i am going going back and forth to fill information for multiple items (one at a time).

Sometimes I am facing an issue where the above logic (code) is executed and the cursor is still in old position (not in the desired new row, column position) and the program started writing data in the old position rather than the desired/ target position resulting in a programming error (i.e. 'Session.CursorRow' and 'Session.CursorColumn' are outputting new desired cursor position, when in real time the cursor in its old position on HOST screen).

If anyone has encountered this issue before and/ or have any solution in mind, can you please share the same . Thank you.

Pasted Code from comments below

here is the delay function being used in the vba program- code

Public Sub DelayScript(Seconds As Integer) 
   Dim PauseTime, START 
   PauseTime = Seconds 
   START = Timer ' Set start time. 
   Do While Timer < START + PauseTime 
      DoEvents ' Yield to other processes. 
   Loop 
End Sub

IBM HOST Programming Reference: http://docs.attachmate.com/reflection/14.x/prog-ref/ibm/


Solution

  • If I understand your issue correctly, latency in the underlying connection is interfering with your code's timing. In general, instead of DoEvents or timing code, you should be using the Wait* methods and let Reflection take care of the timings for you. My guess is that you need something more like this:

    With Session
        .MoveCursor targetRowPos, targetColPos 
        '10 second timeout.
        .WaitForEvent rcEnterPos, "10", "", targetRowPos, targetColPos 
        'Verify that you didn't time out.
        If targetRowPos = .CursorRow And targetColPos = .CursorColumn Then
            Session.TransmitANSI "xyz" 'write xyz on HOST screen
        End If
    End With
    

    Alternately (assuming it's a field) you wait on rcEnterField instead of rcEnterPos. See the documentation here.