Read a binary file with

Posted on 2009-02-19
Last Modified: 2012-06-21
I wil convert an access procedure in  This procedure "Create_StkBobis" read the file StkBobix.txt and fill a access table.  Therefore I use a structure type "RecordStkBobis".  Which is the equivalent statement in ?
Type RecordStkBobis

    S_CodeProduit As String * 5

    S_VL As String * 3

    S_LibelléProduit As String * 42

    S_CodeFacturation As String * 4

    S_Cubage As String * 11

    S_PoidsBrut As String * 11

    S_PoidsNet As String * 11

    S_ColisPalette As String * 8

    S_ArticlesColis As String * 8

    S_DlcMin As String * 4

    S_DlcMax  As String * 4

    S_Palettes  As String * 11

    S_Colis  As String * 11

    S_Articles As String * 11

End Type

Function Create_StkBobis()

    Dim iNumRecord As Double

    Dim MyRecord As RecordStkBobis

    Dim MyDb As Database, MyRecordSet As Recordset

    Set MyDb = DBEngine.Workspaces(0).Databases(0)

    Set MyRecordSet = MyDb.OpenRecordset("StkBobis", dbOpenTable)

    Open "StkBobis.txt" For Random As #1 Len = Len(MyRecord)

    iNumRecord = 0

    While Not EOF(1)

        iNumRecord = iNumRecord + 1

        Get #1, iNumRecord, MyRecord


        MyRecordSet("S_RéfArticle") = iNumRecord - 1

        MyRecordSet("S_CodeProduit") = Left$(MyRecord.S_CodeProduit, 4)

        MyRecordSet("S_VL") = Left$(MyRecord.S_VL, 2)

        MyRecordSet("S_LibelléProduit") = MyRecord.S_LibelléProduit

        MyRecordSet("S_CodeFacturation") = MyRecord.S_CodeFacturation

        MyRecordSet("S_Cubage") = Val(MyRecord.S_Cubage)

        MyRecordSet("S_PoidsBrut") = Val(MyRecord.S_PoidsBrut)

        MyRecordSet("S_PoidsNet") = Val(MyRecord.S_PoidsNet)

        MyRecordSet("S_NbreColisPalette") = Val(MyRecord.S_ColisPalette)

        MyRecordSet("S_NbreArticlesColis") = Val(MyRecord.S_ArticlesColis)

        MyRecordSet("S_DlcMin") = IIf(IsNull(MyRecord.S_DlcMin), 0, Val(MyRecord.S_DlcMin))

        MyRecordSet("S_DlcMax") = IIf(IsNull(MyRecord.S_DlcMax), 0, Val(MyRecord.S_DlcMax))

        MyRecordSet("S_StockPalettes") = Val(MyRecord.S_Palettes)

        MyRecordSet("S_StockColis") = Val(MyRecord.S_Colis)

        MyRecordSet("S_StockArticles") = Val(MyRecord.S_Articles)



    Close #1


End Function

Open in new window

Question by:Phve_364
    LVL 4

    Assisted Solution

    Public Function GetFile(ByVal filename As String) As Byte()
        Dim binReader As New BinaryReader(File.Open(Server.MapPath(filename), FileMode.Open, FileAccess.Read))
        binReader.BaseStream.Position = 0
        Dim binFile As Byte() = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length))
        Return binFile
    End Function
    LVL 4

    Assisted Solution

    Imports System.IO

                    Dim binReader As New BinaryReader(New StreamReader(FileName).BaseStream)
                    'Dim binReader As New BinaryReader(File.Open((FileName), FileMode.Open, FileAccess.Read))
                    binReader.BaseStream.Position = 0
                    Dim binFile As Byte() = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length))

                    Return Convert.ToBase64String(binFile)
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    It is ~possible~ to write .Net code that works in the same fashion as reading/writing a UDT to a random access file just like in VB6:
    (follow the links at the bottom for other pages that discuss this topic)

    Accepted Solution

    I have found an other solution which is exactly what I searched>

    Structure RecordStkBobis
            <VBFixedString(5)> Public S_CodeProduit As String
            <VBFixedString(3)> Public S_VL As String
            <VBFixedString(42)> Public S_LibelléProduit As String
            <VBFixedString(4)> Public S_CodeFacturation As String
            <VBFixedString(11)> Public S_Cubage As String
            <VBFixedString(11)> Public S_PoidsBrut As String
            <VBFixedString(11)> Public S_PoidsNet As String
            <VBFixedString(8)> Public S_ColisPalette As String
            <VBFixedString(8)> Public S_ArticlesColis As String
            <VBFixedString(4)> Public S_DlcMin As String
            <VBFixedString(4)> Public S_DlcMax As String
            <VBFixedString(11)> Public S_Palettes As String
            <VBFixedString(11)> Public S_Colis As String
            <VBFixedString(11)> Public S_Articles As String
        End Structure
    Sub Create_StkBobis()
            Dim myRecord as RecordStkBobis
            Dim RecNum As Integer
            Dim myNumRecord As Integer
            Dim MyDb As dao.Database
            Dim MyRecordSet As dao.Recordset
            MyDb = DAODBEngine_definst.OpenDatabase("c:\test.mdb")
            MyRecordSet = MyDb.OpenRecordset("StkBobis")
           FileOpen(1, myFolderName & "\Stkbobis.fic", OpenMode.Random, OpenAccess.Read, OpenShare.Shared, Len(myRecord))
           myNumRecord = 0
           While Not EOF(1)
                myNumRecord = myNumRecord + 1
                FileGet(1, myRecord, myNumRecord)
                MyRecordSet("S_RéfArticle").Value = myNumRecord - 1
                MyRecordSet("S_CodeProduit").Value = Left$(myRecord1.S_CodeProduit, 4)
                MyRecordSet("S_VL").Value = Left$(myRecord1.S_VL, 2)
                MyRecordSet("S_LibelléProduit").Value = myRecord1.S_LibelléProduit
                MyRecordSet("S_CodeFacturation").Value = myRecord1.S_CodeFacturation
                MyRecordSet("S_Cubage").Value = Val(myRecord1.S_Cubage)
                MyRecordSet("S_PoidsBrut").Value = Val(myRecord1.S_PoidsBrut)
                MyRecordSet("S_PoidsNet").Value = Val(myRecord1.S_PoidsNet)
                MyRecordSet("S_NbreColisPalette").Value = Val(myRecord1.S_ColisPalette)
                MyRecordSet("S_NbreArticlesColis").Value = Val(myRecord1.S_ArticlesColis)
                MyRecordSet("S_DlcMin").Value = IIf(IsNull(MyRecord.S_DlcMin), 0, Val(MyRecord.S_DlcMin))
                MyRecordSet("S_DlcMax").Value = IIf(IsNull(MyRecord.S_DlcMax), 0, Val(MyRecord.S_DlcMax))
                MyRecordSet("S_StockPalettes").Value = Val(myRecord1.S_Palettes)
                MyRecordSet("S_StockColis").Value = Val(myRecord1.S_Colis)
                MyRecordSet("S_StockArticles").Value = Val(myRecord1.S_Articles)
            End While
        End Sub

    Open in new window

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Right...that is also in the series I linked to in my previous post.
    Here is the first article in the series:

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now