Dwallace
27th May 2005, 06:41
We're having a problem using VBScript within an SQL Server 2000 DTS package to properly insert a record using the Baan API (IVc4). We're able to insert records just find, but we cannot get the session to close nor are we able to close computer the package runs on without manually ending the "bw.exe" process. Does anyone know the correct syntax for closing the client and ending the session within VBScript? Our code is included below.

'**********************************************************************
' Visual Basic ActiveX Script
'************************************************************************
Function Main()
Dim cn, rs, sSql
Dim BaanObj, rc, sErrMsg
Dim sItem, sCwar, iQty, iEmno, sJobNum, sLabMat, sAcct, sIdDept
Dim sSession, sTable, sField, sValue

'stop ' for testing and debugging w/ Visual Studio

' ********************************************************************
' **** creating and opening connection
' ********************************************************************
set cn=createobject("ADODB.CONNECTION")
cn.Provider = "sqloledb"
cn.Properties("Data Source").Value = [server]
cn.Properties("Initial Catalog").Value = [database]
cn.Properties("Integrated Security").Value = "SSPI"
cn.commandTimeout = 0
cn.Open

' ********************************************************************
' *** creating and opening recordset of items that have been ordered
' ********************************************************************
set rs=createobject("ADODB.RECORDSET")
' ... only returned 5 records at a time for testing
sSql= "SELECT top 5 d.* " & _
", o.Employee_ID " & _
"FROM OrderDetail d " & _
"LEFT OUTER JOIN InvOrder o ON d.intCustOrderPK = o.intCustOrderPK " & _
"WHERE baanLoaded=0 and numTries < 3 "
rs.open sSql, cn, 1,2

' ********************************************************************
' *** creating Baan Object
' *** ".test" extension denotes which Baan connection is being used on the local machine
' ********************************************************************
Set BaanObj = CreateObject("Baan4.Application.test")
BaanObj.Timeout = 10
sSession = "tdinv1101m000" ' assigning session name

' ********************************************************************
' *** looping through the recordset
' ********************************************************************
While Not rs.EOF
' ********************************************************************
' *** trimming/cleansing the data in the recordset, placing in variables
' ********************************************************************
sItem = Trim(rs("vchItem"))
If Left( sItem, 2 ) = "20" then
sCwar = "MOB"
else
sCwar = "CSR"
End If
iQty = rs("intQty")
iEmno = rs("Employee_Id")
sJobNum = Trim(rs("Job_Number"))
sLabMat = Trim(rs("Labor_Material_Code"))
sAcct = Trim(rs("Acct"))
sIdDept = Trim(rs("ID_Dept"))

' ********************************************************************
' *** putting values into Baan fields
' ********************************************************************
sTable = "tdinv100"

' Populating Item
sField = "item"
sValue = sItem
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Warehouse
sField = "cwar"
sValue = sCwar
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Input Date
sField = "idat"
sValue = "STR$( date() )"
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Transaction Type
sField = "kowt"
sValue = "5"
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Order Date
sField = "odat"
sValue = "STR$( date() )"
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Delivered Quantity
sField = "dqan"
sValue = iQty
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Transaction Date
sField = "trdt"
sValue = "STR$( date() )"
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Reason Code
sField = "recd"
sValue = ""
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Transaction Status
sField = "wtst"
sValue = "1"
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Employee
sField = "a.emno"
If IsNull(iEmno) Then
iEmno = 1
End If
sValue = iEmno
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Job Number
sField = "a.jnum"
If IsNull(sJobNum) Then
sJobNum = ""
End If
sValue = sJobNum
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Material Labor Code
sField = "a.matlab"
If IsNull(sLabMat) Then
sLabMat = ""
End If
sValue = sLabMat
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating Ledger Account
sField = "sacn"
If IsNull(sAcct) Then
sAcct = ""
End If
sValue = sAcct
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' Populating ID Department
sField = "dim1"
If IsNull(sIdDept) Then
sIdDept = ""
End If
sValue = sIdDept
Call PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)

' ********************************************************************
' *** Using "stpapi.insert" command to insert record
' ********************************************************************
sErrMsg = Space(50)
rc = BaanObj.ParseExecFunction ("ottstpapihand", "stpapi.insert(" & Chr(34) & sSession & Chr(34) & ",1," & Chr(34) & sErrMsg & Chr(34) & ")")

' *** For testing, showing Error and ReturnValue, etc.
msgbox(Trim(CStr(rs("intCustOrderPK"))) & "." & Trim(CStr(rs("intLineNo"))) & Chr(10) & _
"rc = " & CStr(rc) & Chr(10) & _
"sErrMsg = " & sErrMsg & chr(10) & _
"BaanObj.Error = " & BaanObj.Error & Chr(10) & _
"BaanObj.ReturnValue = " & BaanObj.ReturnValue & Chr(10) & _
"BaanObj.FunctionCall = " & BaanObj.FunctionCall )

If BaanObj.Error <> 0 or BaanObj.ReturnValue <> "1" Then
' There was a problem, record not recorded
' Update the records in SQL database below
With rs
.fields("baanLoaded") = 0
.fields("numTries") = .fields("numTries") + 1
.update
End With
' Send mail (sometime in the future?)
else
' record recorded
With rs
.fields("baanLoaded") = 1
.fields("numTries") = .fields("numTries") + 1
.update
End With
end if

' have to have the "recover" command after the error check or else it clears the error check codes
If Not rc Then
'Perform Baan reset
rc = BaanObj.ParseExecFunction( "ottstpapihand", "stpapi.recover( " & Chr(34) & sSession & Chr(34) & " ," & Chr(34) & sErrMsg & Chr(34) &")" )
End If

rs.MoveNext
Wend

' ********************************************************************
' *** cleaning up
' ********************************************************************
rs.close
set rs=nothing

' ***** trying to end session
' ***** following just hung, never closed
'Call BaanObj.ParseExecFunction ("ottstpapihand", "stpapi.end.session(" & Chr(34) & sSession & Chr(34) & ")")

BaanObj.Quit
Set BaanObj = Nothing
cn.close
set cn=nothing

Main =DTSTaskExecResult_Success
End Function

Function PopulateBaanField(BaanObj, sSession, sTable, sField, sValue)
' filling the Baan field with the value
Dim s
s = "stpapi.put.field(" & Chr(34) & sSession & Chr(34) & ", " & _
Chr(34) & sTable & "." & sField & Chr(34) & ", " & _
Chr(34) & sValue & Chr(34) & ")"
Call BaanObj.ParseExecFunction("ottstpapihand", s)
If BaanObj.Error <> 0 OR BaanObj.ReturnValue <> "" then
BaanObj.Quit
End If
End Function

mark_h
27th May 2005, 15:28
While this script is in debug mode and before the stpapi.end is executed do you see any other sessions being opened? After you make the connection you can start a shell and do a PS. Since we use ILC I am not sure if tdinv1101m000 starts any sub-sessions.