Script to Generate Material Requisition

SOLVED

I am having issues setting the Purchase Order lines with a GL Account code or key gathered from a SQL table. It doesn't matter what I try to pass, the system always defaults it to some obscure account code. All other fields I set work except for the GL account code. Can anybody provide some insight to how I can resolve?

The problem child is listed below with Red font and Yellow highlight.

Below is my code.

'WScript.Sleep(15000)

'declare the variables
Dim Connection
Dim ConnString
Dim HeaderRecordset
Dim SQLHeader
Dim SQLDetail
Dim DocID, Var1

' Set Passable Parameter for Script
'Set DocID = WScript.Arguments
'Var1=DocID(0)
Var1=1315

'define the connection string, specify database driver
ConnString="DRIVER={SQL Server};SERVER=MyServer;UID=MyUser;PWD=MyPassword!;DATABASE=DB01"

'declare the SQL statement that will query the database
SQLHeader = "SELECT * FROM Custom_SMR_Header Where EntryNumber = '" & Var1 & "'"
SQLDetail = "SELECT * FROM Custom_SMR_Detail Where EntryNumber = '" & Var1 & "'"

'create an instance of the ADO connection and recordset objects
Set Connection = CreateObject("ADODB.Connection")
Set HeaderRecordset = CreateObject("ADODB.Recordset")
Set DetailRecordset = CreateObject("ADODB.Recordset")

'Open the connection to the database
Connection.Open ConnString,adOpenKeyset, adLockOptimistic, adCmdTable

'Open the recordset object executing the SQL statement and return records
HeaderRecordset.Open SQLHeader,Connection
DetailRecordset.Open SQLDetail,Connection

Set oScript = CreateObject ("ProvideX.Script")

'Get the ODBC path for the last accessed installation of MAS 90/200
Const HKEY_CURRENT_USER = &H80000001
Set oReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
oReg.GetExpandedStringValue HKEY_CURRENT_USER,"Software\ODBC\ODBC.INI\SOTAMAS90","Directory",PathRoot
PathHome = PathRoot & "\Home"
Set oReg = Nothing

'Init
oScript.Init(PathHome)

'The Session object must be the first MAS 90 object created
Set oSS = oScript.NewObject("SY_Session")
r = oSS.nSetUser("SageAdmin","Kryptic1")
CompanyCode=HeaderRecordset("Company Code")
'MsgBox companycode
r = oSS.nSetCompany(CompanyCode)
r = oSS.nSetModule("PO")

retVal = oSS.nSetProgram(oSS.nLookupTask("PO_PurchaseOrder_ui"))
Set oPurchaseOrderEntry = oScript.NewObject("PO_PurchaseOrder_bus", oSS)
Set oPurchaseOrderLines = oPurchaseOrderEntry.oLines
Set oPurchaseOrderObject = oScript.NewObject("PO_PurchaseOrder_svc", oSS)

'first of all determine whether there are any records
If HeaderRecordset.EOF Then
MsgBox("No records returned.")
Else
'if there are records then loop through the fields
Do While NOT HeaderRecordset.EOF
Company=HeaderRecordset("Company")
CompanyCode=HeaderRecordset("Company Code")
OrderType=HeaderRecordset("Order Type")
APDivisionNo=HeaderRecordset("APDivision")
VendorNumber=HeaderRecordset("Vendor No")
VendorName=HeaderRecordset("Vendor Name")
RequiredDate=HeaderRecordset("Required Date")
ShipVia=HeaderRecordset("Ship Via")
WarehouseCode=HeaderRecordset("Warehouse Code")
EmailAddress=HeaderRecordset("Email Address")
Comment=HeaderRecordset("Comment")
OnHold=HeaderRecordset("On Hold")
RequestNo=HeaderRecordset("Request No")
RequestedBy=HeaderRecordset("Requested By")
RequestDate=HeaderRecordset("Request Date")
EntryNumber=HeaderRecordset("EntryNumber")
RequiredDate=Replace(CStr(RequiredDate),"-","")
RequestDate=Replace(CStr(RequestDate),"-","")

retval = oPurchaseOrderEntry.nSetKeyValue("Company$",CompanyCode)
strEntryNo = ""

retval= oPurchaseOrderEntry.nSetKeyValue("PurchaseOrderNo$",RequestNo)
retVal = oPurchaseOrderEntry.nSetKey()
retval = oPurchaseOrderEntry.nSetValue("PurchaseOrderDate$",RequestDate)
retval = oPurchaseOrderEntry.nSetValue("APDivisionNo$",APDivisionNo)
retval = oPurchaseOrderEntry.nSetValue("OrderType$",OrderType)
retVal = oPurchaseOrderEntry.nSetValue("RequiredExpireDate$",RequiredDate)
retVal = oPurchaseOrderEntry.nSetValue("ShipVia$",ShipVia)
retVal = oPurchaseOrderEntry.nSetValue("WarehouseCode$",WarehouseCode)
retVal = oPurchaseOrderEntry.nSetValue("EmailAddress$",EmailAddress)
retVal = oPurchaseOrderEntry.nSetValue("Comment$",Comment)
retVal = oPurchaseOrderEntry.nSetValue("OnHold$",OnHold)
retVal = oPurchaseOrderEntry.nSetValue("ConfirmTo$",RequestedBy)

'sqlstr = "Delete from Custom_SMR_Header where EntryNumber = '" & Var1 & "'"
'Connection.Execute sqlstr

Do While Not DetailRecordset.EOF
'Write Detail Lines
ItemCode=DetailRecordset("Item Code")
ItemDescription=DetailRecordset("Item Description")
QtyOrdered=DetailRecordset("Qty Ordered")
UnitPrice=DetailRecordset("Unit Cost")
'ExtendedAmount=DetailRecordset("Extended Amount")
GLAccountCode=DetailRecordset("GL Account Code")
DetailComment=DetailRecordset("Detail Comment")
GLAccountKey = DetailRecordset("Account Key")
EntryNumber=DetailRecordset("EntryNumber")

retVal = oPurchaseOrderLines.nAddLine()
retVal = oPurchaseOrderLines.nSetValue("ItemCode$",ItemCode)

retVal = oPurchaseOrderLines.nSetValue("PurchaseAccountKey$",GLAccountKey)

retVal = oPurchaseOrderLines.nSetValue("QuantityOrdered",QtyOrdered)
retVal = oPurchaseOrderLines.nSetValue("UnitCost",UnitPrice)
'Extended Amount not needed as Sage computes it based on qty and unit cost.
'retVal = oPurchaseOrderLines.nSetValue("ExtensionAmt",ExtendedAmount)
retVal = oPurchaseOrderLines.nSetValue("CommentText$",DetailComment)
retVal = oPurchaseOrderLines.nWrite()

DetailRecordset.MoveNext
Loop
'sqlstr = "Delete from Custom_SMR_Detail where EntryNumber = '" & Var1 & "'"
'Connection.Execute sqlstr

HeaderRecordset.MoveNext
Loop
End If
retVal = oPurchaseOrderEntry.nWrite()
If retVal = 0 Then MsgBox (oPurchaseOrderEntry.sLastErrorMsg)Else MsgBox ("Transaction Write complete - Script Complete")End If

'close the connection and recordset objects to free up resources
HeaderRecordset.Close
DetailRecordset.Close
Set HeaderRecordset=Nothing
Set DetailRecordset=Nothing
Set DocID = Nothing
Connection.Close
Set Connection=nothing