Results 1 to 6 of 6

Thread: Solved - Bad record length vb.net 2008

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    7

    Solved - Bad record length vb.net 2008

    I posted this error yesterday trying to figure out why I was receiving it. I was able to figure out my issue yesterday (I had failed to name the form I was using. Today however I have encountered the error again however I know why I just do not understand the issue.

    I am receiving the error when I attempt to write to the file. If I set the record length in the fileopen statement I receive the error if I remove it then it works.

    I would like to set the record length. The following displays the error at the fileput statement.

    Chris



    Imports Microsoft.VisualBasic

    Public Class FrmAdd

    Public Structure Person
    <VBFixedString(12)> Public FName As String
    <VBFixedString(12)> Public LName As String
    <VBFixedString(2)> Public YearlyRating() As String
    End Structure

    Dim Employee As New Person
    Dim filenum As Integer
    'infoReader as
    Dim infoReader As System.IO.FileInfo
    Private Sub FrmAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ReDim Employee.YearlyRating(3)

    Employee.FName = "John"
    Employee.LName = "Smith"
    Employee.YearlyRating(0) = "4"
    Employee.YearlyRating(1) = "3"
    Employee.YearlyRating(2) = "4"

    filenum = FreeFile()
    FileOpen(filenum, "C:\Test.dat", OpenMode.Random, , , Len(Employee))


    FilePut(filenum, Employee)
    FileClose(filenum)
    End
    End Sub
    End Class
    Last edited by clstanton; Apr 6th, 2016 at 05:28 PM. Reason: solved

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Error - Bad record length vb.net 2008

    This code looks like it is straight out of classic Visual Basic! Why don't you explain what it is that you're trying to accomplish and I will be happy to provide you with a .NET equivalent example.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    7

    Re: Error - Bad record length vb.net 2008

    I am not a professional coder. I taught myself basic years ago and then I got away from it. Now that I am retired I thought it would be a good hobby. I chose the project that I am working on as it will help a friend record his foot ball elimination pool. I do not understand SQL and other advances in the art. I intend to learn however I was hoping to just become refreshed with the knowledge I currently have. I was doing good until I tried to dimension an array in the structured variable. At first I was receiving an error due to the boundary of the array then it turned into a bad record error.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Error - Bad record length vb.net 2008

    Right... because the array isn't a specified size, you can't specify the reocrd length because you can't possibly know what it is... it will vary by the number of elements in the array. If it's always going to be 3, then you can just dim it that way, then you know the size of the record at all times... then you can set the record size accordingly.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Error - Bad record length vb.net 2008

    I can not back this up with any references as I derived it by inspecting the resultant file, but the following formula will compute the record length for your structure.
    Code:
    Dim reclen As Int32 = Len(Employee) + 6 * (2 * Employee.YearlyRating.Length)
    Len(Employee) returns 28; this is the same value for a similar VBA UDT. So it appears as though the array declaration itself is 4 bytes (chars). The file contains the 24 characters from FName and LName followed by a sequence of 10 non-printable characters (hence the 6 = (10 - 4), in the above formula) that are then followed by the two character array elements.

    Edit: The reason that setting reclen = -1 works in this case is that internally, a record length of 128 characters is applied that is more than sufficient for writing the record.
    Last edited by TnTinMN; Apr 5th, 2016 at 09:26 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    7

    Re: Error - Bad record length vb.net 2008

    Thanks for the help. I spent sometime today converting this over to my project and it works.

    Thanks again
    Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width