-
Apr 5th, 2016, 05:31 PM
#1
Thread Starter
New Member
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
-
Apr 5th, 2016, 05:36 PM
#2
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.
-
Apr 5th, 2016, 05:48 PM
#3
Thread Starter
New Member
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.
-
Apr 5th, 2016, 07:53 PM
#4
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
-
Apr 5th, 2016, 09:23 PM
#5
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.
-
Apr 6th, 2016, 05:28 PM
#6
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|