|
-
Nov 21st, 2004, 06:04 AM
#1
Thread Starter
New Member
"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
-
Nov 21st, 2004, 06:19 AM
#2
Frenzied Member
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.
-
Nov 21st, 2004, 06:23 AM
#3
Frenzied Member
Also I think your syntax may be wrong on the open statement. You need the hash
VB Code:
Open "C:\orders" For Random As #filenum
-
Nov 21st, 2004, 06:27 AM
#4
Frenzied Member
What is the structure of your orders file? There may be another way of doing this.
-
Nov 21st, 2004, 06:30 AM
#5
Thread Starter
New Member
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
-
Nov 21st, 2004, 06:36 AM
#6
Frenzied Member
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.
-
Nov 21st, 2004, 06:39 AM
#7
Thread Starter
New Member
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.
-
Nov 21st, 2004, 06:43 AM
#8
Frenzied Member
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.
-
Nov 21st, 2004, 06:44 AM
#9
Thread Starter
New Member
-
Nov 21st, 2004, 06:51 AM
#10
Frenzied Member
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.
-
Nov 21st, 2004, 06:53 AM
#11
Thread Starter
New Member
Ok ! Good idea. I am trying it.
-
Nov 21st, 2004, 07:43 AM
#12
Thread Starter
New Member
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
-
Nov 21st, 2004, 07:50 AM
#13
Frenzied Member
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.
-
Nov 21st, 2004, 07:51 AM
#14
Frenzied Member
Post your ACTUAL code using the "[vb code] your code here [/vb code]" (without the space between vb and code).
-
Nov 21st, 2004, 08:16 AM
#15
Thread Starter
New Member
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
-
Nov 21st, 2004, 08:22 AM
#16
Thread Starter
New Member
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().
-
Nov 21st, 2004, 08:33 AM
#17
Frenzied Member
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.
-
Nov 21st, 2004, 08:34 AM
#18
Thread Starter
New Member
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.
-
Nov 21st, 2004, 08:39 AM
#19
Frenzied Member
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.
:-)
-
Nov 21st, 2004, 08:39 AM
#20
Thread Starter
New Member
Dates ! It might be the problem. Items() array has no date datas and it is always correct. I am trying it...
-
Nov 21st, 2004, 08:41 AM
#21
Frenzied Member
-
Nov 21st, 2004, 08:51 AM
#22
Frenzied Member
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.
-
Nov 21st, 2004, 01:03 PM
#23
Thread Starter
New Member
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....
-
Nov 21st, 2004, 01:29 PM
#24
Frenzied Member
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)
-
Nov 21st, 2004, 02:34 PM
#25
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.
-
Nov 21st, 2004, 04:25 PM
#26
Thread Starter
New Member
You are right, indeed. Plus one blank record has been written every time.
The code which writes to the file:
VB Code:
Dim ord(1) As Orders
filenum = FreeFile
Open "C:\Documents and Settings\László Tamás\Asztal\FILEHANDLER2\orders" For Random As filenum Len = 59
ord(0).OrderNo = Ordersek.TextBox1.Value
ord(0).item = Ordersek.TextBox2.Value
ord(0).OrdQty = Ordersek.TextBox3.Value
ord(0).OrdPrice = Ordersek.TextBox4.Value
ord(0).OrdDate = Ordersek.TextBox5.Value
ord(0).ReqDelDate = Ordersek.TextBox6.Value
ord(0).ConfDelDate = Ordersek.TextBox7.Value
ord(0).Over = False
nextrec = (LOF(filenum) / 59) + 1
Put filenum, nextrec, ord(0)
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 .
-
Nov 21st, 2004, 05:08 PM
#27
Frenzied Member
Could you attach a copy of the actual data file so that I can have a go reading it in from this end?
-
Nov 22nd, 2004, 05:24 PM
#28
Thread Starter
New Member
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...)
-
Nov 23rd, 2004, 03:51 AM
#29
Frenzied Member
Know the feeling
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
|