Results 1 to 29 of 29

Thread: "Bad record lenght"

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15

    "Bad record lenght"

    Hi !

    Could anybody tell me what the hell is the problem with this:

    -----

    Dim ord() As Orders
    filenum = FreeFile
    Open "C:\orders" For Random As filenum Len = Len((ord(0)))

    recordNo = LOF(filenum) / Len(ord(0))

    ReDim ord(recordNo)

    For k = 1 To recordNo
    Get filenum, k, ord(k - 1)
    Next k

    Close filenum

    -----

    The "Orders" type defined as:

    -----

    Type Orders
    OrderNo As Double
    OrdDate As Date
    item As String * 9
    OrdQty As Double
    OrdPrice As Double
    ReqDelDate As Date
    ConfDelDate As Date
    Over As Boolean
    End Type

    ------


    The error message is : "Bad record lenght" at the row: "Get filenum, k, ord(k - 1)". It is interesting that the same Open statement in an other part of my program works well. I am really getting mad about it. And what is more it is not always behaving like that: sometimes works well sometimes gives this error message. Why ?

    I would be greatful if somebody gave me a useful answer.
    Thanks,

    lazlo

  2. #2
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    ord(0) does not yet exist, it has no length, so you can't open a file with it the way you are trying to.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  3. #3
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Also I think your syntax may be wrong on the open statement. You need the hash

    VB Code:
    1. Open "C:\orders" For Random As #filenum
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  4. #4
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    What is the structure of your orders file? There may be another way of doing this.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    Thanks a lot.

    Maybe you are right but how is it possible that with the same method (using empty array of the appropriate user type to get recordlenght) opening an other file and getting records with GET
    works ? I am really confused. In one case it works in an other case does not ? It is not logical.

    The problem now is that how can I determine with the LEN function the lenght of a record before opening the file and putting data into the user type array ???

    Lazlo

  6. #6
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    You can't. But if you have a structure to the order file, delimiters and such, you can load the file using types. If you can give me the order file structure I can adapt some code I have to help get you started.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    What do you mean on "the structure of orders file" ?
    The orders file has records built with the Open statements with Random mode according to the next record structure:

    Type Orders
    OrderNo As Double
    OrdDate As Date
    item As String * 9
    OrdQty As Double
    OrdPrice As Double
    ReqDelDate As Date
    ConfDelDate As Date
    Over As Boolean
    End Type


    It is interesting that writing to the file at the very beginning works well. The error occurs when opening the file again either for writing or for reading.

  8. #8
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    OK - I see what you are doing now. I missed the bit that you had already saved to the file. Hang on - just need to check something on the Random mode.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    Thanks a lot.

  10. #10
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Ok. All you need to do is work out what the combined length of all of the types is that you have called, manually, and place that in the len value.

    I can't think of a way of getting the length of the type programatically - sorry.

    But seeing as you have a stringly defined type this shouldn't be a problem. Just write one record and check the length of the file.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    Ok ! Good idea. I am trying it.

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    I specified the recordlenght as suggested. So, instead of LEN(ord(0)) I calculated and put the lenght as absolute value like this:

    Open "C:\\orders" For Random As filenum Len = 59

    Believe it or not: first it has worked but after closing the appl. and opening again the same error message was generated. I checked it, the record lenght of the type:

    Type Orders
    OrderNo As Double
    OrdDate As Date
    item As String * 9
    OrdQty As Double
    OrdPrice As Double
    ReqDelDate As Date
    ConfDelDate As Date
    Over As Boolean
    End Type



    is 59. It is absolutely illogical that it temporarly works and after reopening the same error comes in spite of nothing was changed in the meantime.

    lazlo

  13. #13
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Have you got the # in your code just before filenum. Sorry to labour the point but we are looking for something obscure now I think.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  14. #14
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Post your ACTUAL code using the "[vb code] your code here [/vb code]" (without the space between vb and code).
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  15. #15

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    Sorry, I did not yet tell that I am working in VBA now but the same method I want to use in VB as well. Anyway the Random file acces works (should work) in the same way both in VB and VBA.



    vbcode

    Type item_table
    item As String * 9
    desc As String * 22
    Ci As Double
    End Type

    Type Bookings
    BookNo As Double
    OrderNo As Double
    item As String * 9
    BookedQty As Double
    DelDate As Date
    DelEd As Boolean
    End Type

    Type Orders
    OrderNo As Double
    OrdDate As Date
    item As String * 9
    OrdQty As Double
    OrdPrice As Double
    ReqDelDate As Date
    ConfDelDate As Date
    Over As Boolean
    End Type

    Dim recnumIt, recnumBook, recnumOrd As Double
    Dim items() As item_table
    Dim books() As Bookings
    Dim ord() As Orders

    filenum = FreeFile
    Open "C:\Documents and Settings\László Tamás\Asztal\FILEHANDLER2\item_table" For Random As filenum Len = 39
    recnumIt = LOF(filenum) / 39
    ReDim items(recnumIt)

    For k = 1 To recnumIt
    Get filenum, k, items(k - 1)
    Next k
    Close filenum

    filenum = FreeFile
    Open "C:\Documents and Settings\László Tamás\Asztal\FILEHANDLER2\bookings" For Random As filenum Len = 43
    recnumBook = LOF(filenum) / 43
    ReDim books(recnumBook)
    Debug.Print "File hossz:", LOF(filenum), "Rec hossz:", Len(books(0))

    For k = 1 To recnumBook
    Get filenum, k, books(k - 1)
    Next k
    Close filenum

    filenum = FreeFile
    Open "C:\Documents and Settings\László Tamás\Asztal\FILEHANDLER2\orders" For Random As filenum Len = 59
    recnumOrd = LOF(filenum) / 59
    ReDim ord(recnumOrd)
    For k = 1 To recnumOrd
    Get filenum, k, ord(k - 1)
    Next k
    Close filenum

    'from here other part of the code is coming using the datas of items(), books() and ord() arrays.


    /vbcode]


    Still the problem is that consequently the error "Bad record lenght" generated at the rows beginning "GET filenum...".
    I left the original path in the code, I hope it will not disturb.


    Lazlo

  16. #16

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    I forgot something:

    In case of items() I never get the same error. The array is loaded perfectly from the file. The error comes in case of ord() and books().

  17. #17
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    I think it might be the date function that is throwing things.

    Can you be sure that the data format is consistent. ie yyyy-mm-dd or whatever your preference. Failing that force the result of the record number calculation to a LONG type or do a divide using \ rather than / (it forces an integer solution to the divide). Redim may not like to work with a single.

    You could check to see if that is the problem by doing a test and dropping the date from your types.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  18. #18

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    Now I decided to send you attached the whole file. Of course the paths should be changed. The program is far not ready and I do not if it can be followed. Anyway I hope so.

  19. #19
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    No - No problem i can follow your code OK. I had the same problem as you when it came to showing code on the forum inthe early days though. The tags you need to use are OK when you get to know them, but for me to show you in text, problem is they get parsed out of my message.

    See if you can get it from this....

    if you type (vbcode) before your vb code, and (/vbcode) after your VB code but use SQUARE brackets [] rather than the round ones I have used, then your code formats OK.

    :-)
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  20. #20

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    Dates ! It might be the problem. Items() array has no date datas and it is always correct. I am trying it...

  21. #21
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    My thinking precisely
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  22. #22
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    lazlo, apologies if I don't answer any post you send for the next hour or so. I have to step out for a while. Post if you manage to fix it.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  23. #23

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    David !

    I tried everything and failed. Finally I tried with converting date-datas into double and using this when writing to the file in order to eliminate using Date type:


    Type Orders
    OrderNo As Double
    OrdDate As Double
    item As String * 9
    OrdQty As Double
    OrdPrice As Double
    ReqDelDate As Double
    ConfDelDate As Double
    Over As Boolean
    End Type

    I do not understand this phenomena. I really appreciate all your help. It counted a lot but I still have the same error....

  24. #24
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Is it the dates that we have to get around? Was it that that was giving you the problem?

    If so save the date as a string.

    sDate = Format(dDate,"dd-mm-yyy")

    and convert back to a date when you unpack.

    dDate = CDate(sDate)
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  25. #25
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    First, the Date and Double data types are the same thing. Both use 8 bytes.

    The probable cause is that the Orders UDT may be defined incorrectly. Are you sure this is the exact layout of the existing file? There should be no problems with that UDT and random files.

    Can you post the code that writes to the file.

    One more thing, the Redim lines, eg. ReDim ord(recnumOrd)
    should be ReDim ord(recnumOrd - 1).

    As it is now, the array contains 1 more element than records in the file.

  26. #26

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    You are right, indeed. Plus one blank record has been written every time.


    The code which writes to the file:

    VB Code:
    1. Dim ord(1) As Orders
    2. filenum = FreeFile
    3. Open "C:\Documents and Settings\László Tamás\Asztal\FILEHANDLER2\orders" For Random As filenum Len = 59
    4.            
    5.              ord(0).OrderNo = Ordersek.TextBox1.Value
    6.              ord(0).item = Ordersek.TextBox2.Value
    7.              ord(0).OrdQty = Ordersek.TextBox3.Value
    8.              ord(0).OrdPrice = Ordersek.TextBox4.Value
    9.              ord(0).OrdDate = Ordersek.TextBox5.Value
    10.              ord(0).ReqDelDate = Ordersek.TextBox6.Value
    11.              ord(0).ConfDelDate = Ordersek.TextBox7.Value
    12.              ord(0).Over = False
    13.              
    14.              nextrec = (LOF(filenum) / 59) + 1
    15.              Put filenum, nextrec, ord(0)
    16. Close filenum


    This runs everytime when a commandbutton is clicked. Before that the user has to fill in the textboxes with datas. The date datas come from Calendar control and all the other datas can be put with the appropriate type.


    Thanks again for helping .

  27. #27
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Could you attach a copy of the actual data file so that I can have a go reading it in from this end?
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  28. #28

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Budapest
    Posts
    15
    Telling you the thruth at this moment I do not have a reliable file what I can send because I have been adjusting the size of my UDT several times so that now I have several kind of files built with different UDTs. Before sending I have to make a file with a "standard" UDT to let you have something which is at least coherent enough to analize.

    So, I will send it soon.
    (Unfortunately I have to go bed now. You know these bloody working days steal my time not letting me deal with the things I like...)

  29. #29
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457
    Know the feeling
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

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