If anybody meet the same problem?

I meet a wired problem when I am upgrading export interface for Sage 50 2016. I am not sure what is the exactly reason cause the problem even I seemed find a solution. I just public some codes here, could anyone take a look and give me any advice? I modified this code base on the VB.NET sample code in SDK. 

====================================================================================================================================

Sample.vb

====================================================================================================================================

Option Strict Off
Option Explicit On

Imports System.Runtime.InteropServices
Imports System.Text


Friend Class frmSample
Inherits System.Windows.Forms.Form

Private Sub cmdOK_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOK.Click
Dim lNum As Integer
Dim lId As Integer
Dim iRet As Short
Dim pRecord(1999) As Byte
Dim dtDateTemp(5) As Byte
Dim iDBLink As Short
Dim iTBLink As Short
Dim iFileVer As Short
Dim iCtyCode As Short
Dim iDBCode As Short
Dim bIsAcctCopy As Boolean
Dim szUserName As String
Dim szPassword As String
Dim szFileName As String
Dim szCompName As String
Dim sFindData As WIN32_FIND_DATA

szFileName = Trim(txtFileName.Text)
szUserName = Trim(txtUserName.Text)
szPassword = Trim(txtPassword.Text)

sFindData = New WIN32_FIND_DATA
If szFileName = "" Or FindFirstFile(szFileName, sFindData) = -1 Then
DisplayError(("The file does not exsit. Please select another file."))
Exit Sub
End If

Dim szHost As String
Dim szPort As String
Dim szFileDir As String
Dim lLength As Long
Dim dbClient As New Simply.ConnectionManagerServiceClient.ConnectionManagerServiceClient
Dim dbError As Simply.ConnectionManagerService.ConnectionManagerError

szFileDir = LCase$(szFileName)
lLength = Len(szFileDir)
If (Strings.Right(szFileDir, 4) = ".sai") Then
szFileDir = Strings.Left(szFileDir, lLength - 4) + Replace(Strings.Right(szFileDir, 4), ".sai", ".saj")
End If

szHost = ""
szPort = ""
dbError = dbClient.GetConnectionInfo(szFileDir, szHost, szPort)

If dbError = Simply.ConnectionManagerService.ConnectionManagerError.No_Error Then

If wSDBOpenDatabase(iDBLink, FLG_SHARED, "Sample", szUserName, szPassword, szHost, "", szPort) = DBS_SUCCESS Then
If wSDBGetDBVer(iDBLink, iFileVer, iCtyCode, iDBCode, bIsAcctCopy) = DBS_SUCCESS Then
If (iFileVer < 23101) Then
DisplayError(("The SDK will only work with databases created in Sage 50 Accounting (Release 2016.2). Please select another file."))
Call wSDBCloseDatabase(iDBLink)
Call vSDBUnloadDAO()
Exit Sub
End If
End If
If wSDBOpenTable(iDBLink, "tCompany", FLG_READONLY, iTBLink) = DBS_SUCCESS Then
'Read record #1 in the company info record
lId = 1
Call SetBytes(pRecord, System.BitConverter.GetBytes(lId), 0, 4)
'Call SetBytes(dtDateTemp, System.BitConverter.GetBytes(123456), 0, 6)
iRet = wSDBGetRecord(iDBLink, iTBLink, FLG_KEY0, pRecord)

szCompName = ""
If (iFileVer > 9000) Then
Call Byte2String(pRecord, szCompName, 34, 52)
Else
Call Byte2String(pRecord, szCompName, 28, 52)
End If
lblCompanyName.Text = Trim(szCompName)

Call CopyMemory(dtDateTemp(0), pRecord(405), 5)


lbsDateTime.Text = BcdToByteDouble(dtDateTemp)
End If
Call wSDBCloseTable(iDBLink, iTBLink)

'Get the number of records per module
lNum = GetNumRecModule(iDBLink, "tAccount")
lblNumAccounts.Text = Trim(Str(lNum))
lNum = GetNumRecModule(iDBLink, "tVendor")
lblNumVendors.Text = Trim(Str(lNum))
lNum = GetNumRecModule(iDBLink, "tCustomr")
lblNumCustomers.Text = Trim(Str(lNum))
lNum = GetNumRecModule(iDBLink, "tEmp")
lblNumEmployees.Text = Trim(Str(lNum))
lNum = GetNumRecModule(iDBLink, "tInvent")
lblNumInventory.Text = Trim(Str(lNum))
lNum = GetNumRecModule(iDBLink, "tProject")
lblNumProjects.Text = Trim(Str(lNum))

Call wSDBCloseDatabase(iDBLink)
Else
DisplayError(("Cannot open the database. Please select another file."))
End If
Call vSDBUnloadDAO()
Else
DisplayError(("Cannot connect to database server."))
End If

End Sub


Private Sub Byte2String(ByRef ArrayOfBytes() As Byte, ByRef StringEqv As String, ByVal start As Short, ByVal strSize As Short)
Dim i As Short
StringEqv = ""
For i = 0 To strSize - 1
If ArrayOfBytes(start + i) = 0 Then
Exit For
Else
StringEqv = StringEqv + Convert.ToChar(ArrayOfBytes(start + i))
End If
Next i
End Sub


Private Sub SetBytes(ByRef byteCopyto() As Byte, ByRef byteCopyFrom() As Byte, ByVal nStart As Short, ByVal nSize As Short)
Dim n As Short
For n = 0 To nSize - 1
'Make sure we don't copy something we don't have.
If n < byteCopyFrom.Length - 1 Then
If byteCopyFrom(n) = 0 Then
Exit For
Else
byteCopyto(nStart + n) = byteCopyFrom(n)
End If
End If
Next n

End Sub


Private Sub DisplayError(ByRef szErrorString As String)
Call MsgBox(szErrorString, MsgBoxStyle.ApplicationModal + MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Sample - Error")
lblCompanyName.Text = ""
lblNumAccounts.Text = ""
lblNumVendors.Text = ""
lblNumCustomers.Text = ""
lblNumInventory.Text = ""
lblNumEmployees.Text = ""
lblNumProjects.Text = ""
txtUserName.Text = ""
txtPassword.Text = ""
txtFileName.Text = ""
txtFileName.Focus()
End Sub


Private Function GetNumRecModule(ByRef iDBLink As Short, ByRef szTableName As String) As Integer
Dim iTBLink As Short
Dim lNum As Integer

lNum = 0
If wSDBOpenTable(iDBLink, szTableName, FLG_READONLY, iTBLink) = DBS_SUCCESS Then
Call wSDBGetNumRecs(iDBLink, iTBLink, lNum)
Call wSDBCloseTable(iDBLink, iTBLink)
End If
GetNumRecModule = lNum
End Function


Private Sub cmdClose_Click()
Me.Close()
End Sub

Public Function BcdToByteDouble(ByRef pBufferDate() As Byte) As Double
Dim dDate As Double
Dim strDate As String = ""
Dim fdate(7) As Byte
Dim i As Short

fdate(0) = pBufferDate(0)
fdate(1) = pBufferDate(1) \ 16
fdate(2) = pBufferDate(1) - (fdate(1) * 16)
fdate(3) = pBufferDate(2) \ 16
fdate(4) = pBufferDate(2) - (fdate(3) * 16)
fdate(5) = pBufferDate(3) \ 16
fdate(6) = pBufferDate(3) - (fdate(5) * 16)
fdate(7) = IIf((pBufferDate(4)) > 0, (pBufferDate(4) - 12) / 16, 0)


For i = 0 To 7
strDate = strDate & fdate(i)
Next i

dDate = Val(strDate)

BcdToByteDouble = dDate
End Function

End Class

====================================================================================================================================

  • 0
    Module1.vb
    =====================================================================================================
    Option Strict Off
    Option Explicit On
    Imports System.Runtime.InteropServices
    Module Module1

    Public Const FLG_SHARED As Short = &H0s
    Public Const FLG_READONLY As Short = &H1s
    Public Const FLG_KEY0 As Short = &H0s

    'return codes
    Public Const DBS_SUCCESS As Short = &H0s

    Public Const MAX_PATH As Short = 260

    Public Structure FILETIME
    Dim dwLowDateTime As Integer
    Dim dwHighDateTime As Integer
    End Structure

    Public Structure WIN32_FIND_DATA
    Dim dwFileAttributes As Integer
    Dim ftCreationTime As FILETIME
    Dim ftLastAccessTime As FILETIME
    Dim ftLastWriteTime As FILETIME
    Dim nFileSizeHigh As Integer
    Dim nFileSizeLow As Integer
    Dim dwReserved0 As Integer
    Dim dwReserved1 As Integer
    <VBFixedString(MAX_PATH), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=MAX_PATH)> Public cFileName() As Char
    <VBFixedString(14), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=14)> Public cAlternate() As Char
    End Structure

    Public Declare Function wSDBOpenDatabase Lib "Sage_SA_dblyr.dll" (ByRef pDBLinkNo As Short, ByVal wFlags As Short, ByVal pszAppID As String, ByVal pszUserID As String, ByVal pSZPWD As String, ByVal szHost As String, ByVal szCompId As String, ByVal szPort As String) As Short
    Public Declare Function wSDBCloseDatabase Lib "Sage_SA_dblyr.dll" (ByVal wDBLinkNo As Short) As Short
    Public Declare Function wSDBGetDBVer Lib "Sage_SA_dblyr.dll" (ByVal wDBLinkNo As UShort, ByRef pnFileVer As Short, ByRef pnCtyCode As Short, ByRef pnDBCode As Short, ByRef pnIsAcctCopy As Boolean) As Short

    Public Declare Function wSDBOpenTable Lib "Sage_SA_dblyr.dll" (ByVal wDBLinkNo As Short, ByVal pszTableName As String, ByVal wFlags As Short, ByRef pTBLinkNo As Short) As Short
    Public Declare Function wSDBCloseTable Lib "Sage_SA_dblyr.dll" (ByVal wDBLinkNo As Short, ByVal wTBLinkNo As Short) As Short
    Public Declare Function wSDBGetNumRecs Lib "Sage_SA_dblyr.dll" (ByVal wDBLinkNo As Short, ByVal wTBLinkNo As Short, ByRef pNumRecs As Integer) As Short

    Public Declare Function wSDBGetRecord Lib "Sage_SA_dblyr.dll" (ByVal wDBLinkNo As Short, ByVal wTBLinkNo As Short, ByVal wKey As Short, ByVal pRecord As Byte()) As Short

    Public Declare Sub vSDBUnloadDAO Lib "Sage_SA_dblyr.dll" ()

    Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As Integer
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Byte, ByRef Source As Byte, ByVal Length As Integer)
    End Module
    =====================================================================================================