Newsgroup: it.comp.lang.visual-basic
Titolo: Does anyone know about writing to Random Access files?

Hello fellow VBers,
If you like inexplicable problems then read on....
I've decided to use random access files to store data entered on a form
into various textboxes (9 in all)
This data is then assigned to my data structure, and i then open a file and
write the data to the end of the file.  I have followed the code in my
Dummies book but i can only ever add ONE record; the existing record is
always overwritten.
The problem resides with the variable LastOne which increases to 1, but
when the Command2 is clicked again it starts at 0, despite the fact that a
record has been posted.  It should come up with 1 which is then increased
to 2 (this would then post the next set of details to position 2 in the
file).
Why on earth does it not do this?  I can't see any logical errors here
hence the post.  I really need to get past this since this is the
fundamental way i want to handle data (particularly as I am not too keen on
Access dbs and don't have DAO anyway).
If you can help, please reply and also send your thoughts to my email
address.
Thanks again,
Martin
sta...@iname.com
HERE is the code in question;
Dim Response As String
Dim Position As Long
Dim ILength As Long
Dim Suppliers As SupplierStructure  'i have actually declared this in the
module
Dim InvoiceLength As SupplierStructure
Dim LastOne As Long
Private Sub Command2_Click()
Response = MsgBox("Do you want to post these supplier details?", vbYesNo,
"notification")
If Response = vbYes Then
'assign textbox contents to SupplierStructure defined as Suppliers
Suppliers.SupplierName = txtSupplier1.Text
Suppliers.Address = txtAddress2.Text
Suppliers.City = txtCity3.Text
Suppliers.PostCode = txtPostCode4.Text
Suppliers.Phone = txtPhone5.Text
Suppliers.Fax = txtFax6.Text
Suppliers.Email = txtEmail7.Text
Suppliers.ContactName = txtContact8.Text
Suppliers.VATno = txtVATno9.Text
ILength = Len(InvoiceLength)
Open "c:\windows\system\suppliers.dat" For Random As #2 Len = ILength
LastOne = LOF(2) \ Len(Suppliers)   'calcs # of structures
MsgBox "value of LastOne is " & Str(LastOne) 'testing for value of LastOne
LastOne = LastOne + 1
MsgBox "value of LastOne is " & Str(LastOne) 'testing for value of LastOne
Put #2, LastOne, Suppliers
Close #2
 
txtSupplier1.SetFocus
unload me
ElseIf Response = vbNo Then
txtSupplier1.SetFocus
End If
End Sub
-- 
Martin Dean
if Replying by email remove BIGNONO from address

Hi Martin,
I tried your code on my PC (win95 and vb5.0sp3) and it work fine. I haven't
found the problem you describe.
I think that the problem may be in the SupplierStructure declaration. I
suppose you have a declaration like the following one:
Type SupplierStructure
    SupplierName As String
    Address As String
    City As String
    PostCode As String
    Phone As String
    Fax As String
    Email As String
    ContactName As String
    VATno As String
End Type
In this way the function Len isn't able to retrive the disk space required
to store the structure.
Instead you have to use fixed lenght string in the following way:
Type SupplierStructure
    SupplierName As String * 20
    Address As String * 20
    City As String * 20
    PostCode As String * 20
    Phone As String * 20
    Fax As String * 20
    Email As String * 20
    ContactName As String * 20
    VATno As String * 20
End Type
Where I assumed that all fields have a max lenght of 20.
In this way it work very well.
Bye

...


In order to easily determine the next record position, calculate it just
prior to writing:
 Open "somefile.dat" For Random Access Read Write As FileNo Len =
Len(YourType)
 TotalRecords = LOF(FileNo) \ Len(YourType)
  Put #FileNo, TotalRecords +1, YourType
--
Randy Birch, MVP Visual Basic
http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/
news://
news.mvps.org/ccrp/

Hi!
As replayed Stefano Valsecchi strings in random files must be "fixed length"
type.
Random files aren't text files!!!
Fields are stored in binary way, and, if I don't get wrong, strings are
stored in UNICODE (as like in memory): 2 bytes per char.
To avoid VB reset private vars in sub to zero you must declare them as
"Static" instead of "Dim", so they'll keep theirs last value.
Remember: if you declare any static vars in forms, and you need to unload
that form, you must use
Unload Form1
Set Form1 = Nothing
elsewhere, VB keep a link to the Form in memory and don't really remove it.
Excuse english errors: today I'm very sleepy.
See u!
Fabio.

stored in UNICODE (as like in memory): 2 bytes per char.
Almost!  If you are talking ordinary ASCII text then the beauty of
talking in character numbers from 0 to 255 allows you to slap a single
character within one byte.  Try reading a text file using a hex reader!
There are times when you might need to do a conversion on a file that
contains text within it in random access mode, such as changing the
object name of somthing contained within a project file.
Or....you might want to read a text file in, in random access form just
to convert it from Unix/C style to DOS.
Richard.

Using type structures with fixed lengths
is the way to go, but keep in mind that
VB does limit how much space your fixed
length UDTs can take up.  I believe the
limit is at 64K, but I could be mistaken.


End Type
I've been using this syntax for ages. It works so much faster and trimmer
than
using the bloated (albeit politically correct) database engines.  I 'cheat'
by
assigning the file handles as #200, etc praying they aren't in use.
What is a safer way to assign file handle numbers to open sequential files?
           Nick De Wolf

.>Instead you have to use fixed lenght string in the following way:
<SNIP>





using the bloated (albeit politically correct) database engines.  I 'cheat'
Wow I agree! Why use 1-3 mb of DB files for storing flat file info! It
is a shame that the data aware controls cannot be linked to a type
structure above. Or could they? :-)
THANKS!
Marc
Tech Support Sunbelt Software Inc.
te...@sunbeltinc.com