I am /trying/ to get the hang of udt's and random access files. When
I press F5, the computer creates the file "items.dat", that is 514
bytes long. All hex $00. Would someone please show me where I'm
screwing up?
Thanks,
---------------------------------------------
Private Type Item
Number As Integer
Description As String * 30
Price As Currency
End Type
Dim NewItem As Item
Dim ItemCount As Integer
Option Base 1
Private Sub Form_Load()
Open "a:\items.dat" For Random As 1
For ItemCount = 1 To 5
NewItem.Number = ItemCount
NewItem.Description = "Item number " & Str(ItemCount)
NewItem.Price = ItemCount
Put #1, , Item
Next ItemCount
Close 1
End
End Sub
> I am /trying/ to get the hang of udt's and random access files. When
> I press F5, the computer creates the file "items.dat", that is 514
> bytes long. All hex $00. Would someone please show me where I'm
> screwing up?
You're doing a couple of things wrong. First is that you're not using Option Explicit, which would have shown you that you're Putting a variable named Item that you never declared... you meant NewItem.
Second is that you're using Random without specifying a record length, so records are Put at 128 byte intervals (the default Len). You want to either use Binary mode, and calculate the offsets yourself, or specify the length in the Open statement:
Open "a:\items.dat" For Random As 1 Len = Len(NewItem)

Signature
Jim Mack
MicroDexterity Inc
www.microdexterity.com
> ---------------------------------------------
>
[quoted text clipped - 20 lines]
> End
> End Sub
Nobody You Know - 30 Aug 2005 09:35 GMT
>> I am /trying/ to get the hang of udt's and random access files. When
>> I press F5, the computer creates the file "items.dat", that is 514
[quoted text clipped - 6 lines]
>
> Open "a:\items.dat" For Random As 1 Len = Len(NewItem)
Thanks alot! Making those changes reduced the data file size by 87,912
bytes. Over two-thirds of it's original size.
. On 29.08.05
wrote nobody@nowhere.com (Nobody You Know)
on /COMP/LANG/BASIC/VISUAL/MISC
in e6v6h1pedfn2r3gm2iof7icusnj1e21c1e@4ax.com
about Need help with UDT
NYK> For ItemCount = 1 To 5
NYK> NewItem.Number = ItemCount
NYK> NewItem.Description = "Item number " & Str(ItemCount)
NYK> NewItem.Price = ItemCount
NYK> Put #1, , Item
NYK> Next ItemCount
You are filling "NewItem" with data, but apperently you don't write
it, instead you write "Item" which does not exist as variable.
Yours,
Lüko Willms http://www.willms-edv.de
/--------- L.WILLMS@jpberlin.de -- Alle Rechte vorbehalten --
Das Buch muß erst ausgedroschen werden. -G.C.Lichtenberg