Sample Visual Basic Program


 

The following sample Visual Basic program exercises some of the features of the Sunaccess package. Simply create a program with a single form and a command button. From the Project menu, select "References". Click Browse to locate the Sunaccess DLL file supplied. Click on "Open" and ensure that the line containing "Sunbelt Sunaccess File Routines" is checked. Click OK to close the References window. Attach the following code to the command button of the form. Note that if you are using VB 6.0, change SA_RepositFile to SA_RepositFile32, SA_TxtRead to SA_TxtRead32, and SA_TxtWrite to SA_TxtWrite32 since that is a 32 bit application.

 

Private Sub Command1_Click()

 

Dim file1 As Long, retVal As Long

Const bufferLength = 20

Dim buffer As String * bufferLength

Const Seq = -1

Dim FileList(1) as Long ' one more than actually needed

 

    Debug.Print "*** Testing Sunaccess"

     

' Prepare the file

 

    Debug.Print "*** Creating the file"

     

    file1 = SA_TxtPrep("sa_test1.txt", 12, SA_SHARE, 512)

    If file1 < 0 Then

        MsgBox "'sa_test1.txt' error code on prep = " & file1, , "Prep"

        Exit Sub

    End If

 

' Lock the file

 

    FileList(0) = file1

    retVal = SA_FilePI(FileList(0))

    If retVal < 0 Then

        MsgBox "Lock failed", , "FilePI"

        Exit Sub

    End If

 

' Write 10 records to the file.

 

    Debug.Print "*** Write 10 records"

 

    For cnt = 1 To 10

        buffer = "Test Record " + Format(cnt)

        Debug.Print buffer

        retVal = SA_TxtWrite(file1, buffer, bufferLength, Seq)

        If retVal < 0 Then

            MsgBox "Error writing record: " & retVal, , "Write Sequential"

            Exit Sub

        End If

    Next

     

 

' Rewind the file to the beginning

 

    Debug.Print "*** Rewinding the file"

    retVal = SA_RepositFile(file1, 0, 0)

    If (retVal < 0) Then

        MsgBox "error rewinding the file: " & retVal, , "Rewind"

        Exit Sub

    End If

         

' Read the file.

 

    Debug.Print "*** Reading the file "

 

    Do

        retVal = SA_TxtRead(file1, buffer, bufferLength, Seq)

         

        If retVal = 0 Then ' End of File?

            Exit Do

        End If

             

        If retVal < 0 Then

            MsgBox "Error reading record: " & retVal, , "Write Sequential"

            Exit Sub

        End If

         

        Debug.Print buffer

    Loop

 

 

' Unock the file

 

    retVal = SA_FilePIEnd()

    If retVal < 0 Then

        MsgBox "Unock failed", , "FilePIEnd"

        Exit Sub

    End If

 

' Close the file

     

    Debug.Print "*** Closing the file"

    retVal = SA_TxtClose(file1)

    If retVal < 0 Then

        MsgBox "Error closing the file: " & retVal, , "Close"

        Exit Sub

    End If

     

    MsgBox "Test completed successfully", vbOKOnly, "Done"

    Debug.Print "*** Test complete"

 

End Sub

 

Execution of the program and clicking on the command button should result in a "Test Successful" dialog. Additional messages are available in the Immediate Window.

 



Sunaccess Reference Sample C Program Control File