Macro showing error when used by Multiple Users

Joni_Depp

New Member
Joined
Mar 22, 2016
Messages
26
Hi Everyone!

I was able to create a simple command in AS400 by recording some macros from AS400 then adding some codes thru the .ebs file. It was working fine when I was using it but when it was applied to 10 users, it usually shows an error saying, "Someone else is working in 'J:\AS400 Automation\tk.csv' right now. Please try again later."

Below is the code that I have made:

Code:
'----------------------------------------------------------------------
' This macro was created by the macro recorder.
' Macro File: \\******\****\Global\AS400 Automation\TKFinder.ebs
' Date: Thu Sep 20 07:01:38 2018
' Recorded for profile: N***e*n AS400 S1
'----------------------------------------------------------------------


Function requestData(connStr As String, sqlStr As String)
' returns the result of the data.
' use isempty(requestData) to identify if the request has result or none


Dim xlCon As Object
Dim xlRs As Object
Dim rsData As Variant


Set xlCon = CreateObject("ADODB.Connection")
Set xlRs = CreateObject("ADODB.Recordset")


xlCon.Mode = 1 ' adModeRead / Indicates read-only permissions.
xlCon.Open connStr ' open the csv database


xlRs.LockType = 1 ' adLockReadOnly / Indicates read-only records. You cannot alter the data.xlRs.Open sqlStr, xlCon ' send query
xlRs.Open sqlStr, xlCon


If Not xlRs.EOF Then
    ' execute only if there is record found in the query
    if isnull(xlRs.Fields.item(0).value) then
        msgbox "No requirement."
    else
        msgbox xlRs.Fields.item(0).value
    end if
Else
    msgbox "Query not found."
End If


xlRs.Close
xlCon.Close


Set xlRs = Nothing
Set xlCon = Nothing


End Function


Sub Main
Dim HostExplorer as Object
Dim MyHost as Object
Dim Rc as Integer


On Error goto GenericErrorHandler


Set HostExplorer = CreateObject("HostExplorer") ' Initialize HostExplorer Object
Set MyHost = HostExplorer.HostFromProfile("N***e*n AS400 S1") ' Set object for the desired session
If MyHost is Nothing Then Goto NoSession


    If Not MyHost.Area(6,2,6,3).Value="SC" Then Exit Sub


    On Error goto ShipperCodeError
    Dim shipperCode As long
    shipperCode = MyHost.Area(6,5,6,11).Value
    shipperCode = trim(shipperCode)


    Dim dbName As String, connStr As String, sqlStr As String
    Dim leadData


    dbpath = "J:\global\AS400 Automation"
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited;"""


    if isempty(shippercode) then exit sub


    dbName = "tk.csv"
    sqlStr = "SELECT Requirement " & _
            "FROM [" & dbName & "] Where [Shipper Code] = " & shipperCode & ";"


    requestData connStr, sqlStr


Exit Sub


'-------------------- Runtime Error Handlers --------------------
GenericErrorHandler:
    Msgbox "Error " & Err & " : """ & Error(Err) & """ has occurred on line " & Erl-1 & "." & Chr(10) & "Unable to continue macro execution.", 16, "HostExplorer Basic Macro Error"
    Exit Sub


ShipperCodeError:
    Msgbox "Shipper code is empty."
    Exit sub


NoSession:
    Msgbox "Profile ""NewPenn AS400 S1"" is not running." & Chr(10) & "Unable to execute macro.", 16, "HostExplorer Macro Error"
    Exit Sub


OnKeyboardError:
    Msgbox "Unable to type string on host screen." & Chr(10) & "Unable to continue macro execution.", 16, "HostExplorer Basic Macro Error"
    Exit Sub


End Sub

Can 'Return' function solve this issue? And if 'Return' function is the answer, I would like to ask for your expertise on how to do it.

Thank you in advance!

-Joni
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top