hello sir can you help me sir what is the random access files?i want to learn that but i have no idea to make the code...please help me sir, hoping for your positive responds...thank you in advance...

random files is a file type with the following characteristic
- has a fixed length record
- it can be accessed directly by record number so if we want to access record number 8 we can go there directly instead of reading from record 1 as we do in normal text files (also called sequential files)
it has advantages that it is much faster than normal sequential files but it will become larger size if the record size is vary because we hv to use maximum record size as record size
hope will be usefull

In VB's help files, look up the following...

FreeFile Function
Open Statement 'especially this one
Input Function
Line Input Function
Get Statement (under Get Keyword)
Put Statement (under see also)
Seek Function (under see also)
Close Statement

Good Luck

In VB's help files, look up the following...

FreeFile Function
Open Statement 'especially this one
Input Function
Line Input Function
Get Statement (under Get Keyword)
Put Statement (under see also)
Seek Function (under see also)
Close Statement

Good Luck

hello sir thank you for the reply...do you have simple program on this sir so that i have reference..hoping for your positive responds...and sir kindly put some comments on it so that i can easily understand....thank you in advance hoping for your positive responds...

Jemz, by now I would have thought you would have known that a lot of these things that you can look up in help already have examples, not only in the help files, but at msdn, psc, and many other places on the web, you just need to use your friends (yahoo, google, ask, answers, bing).

Good Luck

Jemz, by now I would have thought you would have known that a lot of these things that you can look up in help already have examples, not only in the help files, but at msdn, psc, and many other places on the web, you just need to use your friends (yahoo, google, ask, answers, bing).

Good Luck

hello sir thank you for the reply okey sir i will try sir....thank you sir...i will write again to post my code as soon as possible...

In VB's help files, look up the following...

FreeFile Function
Open Statement 'especially this one
Input Function
Line Input Function
Get Statement (under Get Keyword)
Put Statement (under see also)
Seek Function (under see also)
Close Statement

Good Luck

hello sir,

here is my code.. i have trouble on this, i want to entry the student record and saving this in file but when i look up in the file the output is incorrect..can you please help me sir on this....hoping for your positive responds...please correct my code sir...thank you in advance...

Option Explicit
Public rndfilname As String
Public r_id As String
Public rndnum As Integer
Public age As Integer, stdidno As Integer
Public lname As String, fname As String, address As String


Private Type student
idno As Integer
stdage As Integer
lastname As String * 10
firstname As String * 10
stdadd As String * 20
End Type
Private studentrecord As student


Private Sub Form_Load()

  rndfilname = App.Path & "\student.dat"

   Left = (Screen.Width - Width) / 2
   Top = (Screen.Height - Height) / 2
 End Sub


Private Sub cmdsave_Click()

  rndnum = FreeFile

  Open rndfilname For Random Access Write As #rndnum Len = Len(studentrecord)
 
    
  lname = Trim(txtlname.Text)
  fname = Trim(txtfname.Text)
  age = Trim(txtage.Text)
  address = Trim(txtaddress.Text)
  stdidno = Trim(txtid.Text)
  
  
  
  With studentrecord
   .idno = stdidno
   .lastname = lname
   .firstname = fname
   .stdage = age
   .stdadd = address
  End With
  
  
    Put #rndnum, , studentrecord 
  Close #rndnum

End Sub

Private Sub cmdexit_Click()
 End
End Sub

Okay, you have a lot of error checking to add so you don't add a duplicate record with the same ID number (and what did I tell you about End!!!).

Alrighty then, lines 36 through 50 need to be above line 33 and your type contains two integers. Change those to strings and define their length like the rest of the elements in the student type. You should also set the MaxLength of your text boxes to the same length of the elements in the type.

Good Luck

Okay, you have a lot of error checking to add so you don't add a duplicate record with the same ID number (and what did I tell you about End!!!).

Alrighty then, lines 36 through 50 need to be above line 33 and your type contains two integers. Change those to strings and define their length like the rest of the elements in the student type. You should also set the MaxLength of your text boxes to the same length of the elements in the type.

Good Luck

hello sir thank you for the reply...here is my code please correct me if i'm wrong with my code...hoping for your positive responds..sir i have question about the two integers why do i change this to string sir?...

Option Explicit
Public rndfilname As String
Public r_id As String
Public rndnum As Integer
Public age As Integer, stdidno As Integer
Public lname As String, fname As String, address As String


Private Type student
idno As String * 10
stdage As String * 10
lastname As String * 10
firstname As String * 10
stdadd As String * 20
End Type
Private studentrecord As student


Private Sub Form_Load()

rndfilname = App.Path & "\student.dat"

Left = (Screen.Width - Width) / 2
Top = (Screen.Height - Height) / 2
End Sub

Private Sub cmdsave_Click()

rndnum = FreeFile


    
  lname = Trim(txtlname.Text)
  fname = Trim(txtfname.Text)
  age = Trim(txtage.Text)
  address = Trim(txtaddress.Text)
  stdidno = Trim(txtid.Text)
  
  
  
  With studentrecord
   .idno = stdidno
   .lastname = lname
   .firstname = fname
   .stdage = age
   .stdadd = address
  End With
  
  Open rndfilname For Random Access Write As #rndnum Len = Len(studentrecord)
  
  Put #rndnum, , studentrecord
  
 
Close #rndnum

End Sub


Private Sub cmdexit_Click()
 Unload Me
End Sub

Looks good to me and it looks like it should work. Does it?

Good Luck

hello sir thank you for helping me..it works sir...i have some question about you said that i should change the 2 integers to string can you explain me sir why...i have also problem sir why is it only one record to be save...example 0001 jemz etc...and then i will click the save button this will now be sav

Looks good to me and it looks like it should work. Does it?

Good Luck

hello sir thank you for helping me..it works sir...i have some question about you said that i should change the 2 integers to string can you explain me sir why...i have also problem sir why is it only one record to be save...example 0001 jemz etc...and then i will click the save button this will now be save to a file...but if i will add another 0002 michael etc..this will overwrite and the one that i save before the 0001 will not be seen...why this happen sir...how can i do this sir inorder to save all data...please help me sir hoping for your positive responds...thank you in advance

Looks good to me and it looks like it should work. Does it?

Good Luck

hello sir, thank you for helping me and it works...i have some question sir can you please explain me sir about you said the 2 integers in my type you want me to change to string, why this should not be integer sir?..and also sir why i can't save more data sir..example student id=0001 and etc...if i click the save button this will now be save in a file.but when i add another student account 0002 and etc...my 0001 will be replace by the 0002 jem gimz 24 California...how can i do this sir inorder to save all data..please help me sir hoping for your positive responds..thank you in advance...

Okay, the reason it is overwriting your record is because you have left an arguement out on this line...

Put #rndnum, [b]missing arguement[/b], studentrecord

That missing arguement is the record number you are writing. So to fix, you will need an integer declared in the general declarations section and in form load set it + 1 of the number of records in the file. If the file does not exist (See Dir Function) then you would set to 0 + 1. If it has one record in it 1 + 1 and so on (number of records + 1).

The reason for changing the integers to strings is because you said what you see in the file is not what you want and the reason for that is because if you start off with a 2 byte integer, which is what vb uses, and the value of that integer is less than 255, the first byte will be a zero (0), which tells notepad that you may be reading a unicode file. Changing it to a string and that first byte, even it is a "0", its ascii value is acually equal to 48 and thus notepad knows it is an ascii file.

Good Luck

Edited 6 Years Ago by vb5prgrmr: n/a

Okay, the reason it is overwriting your record is because you have left an arguement out on this line...

Put #rndnum, [b]missing arguement[/b], studentrecord

That missing arguement is the record number you are writing. So to fix, you will need an integer declared in the general declarations section and in form load set it + 1 of the number of records in the file. If the file does not exist (See Dir Function) then you would set to 0 + 1. If it has one record in it 1 + 1 and so on (number of records + 1).

The reason for changing the integers to strings is because you said what you see in the file is not what you want and the reason for that is because if you start off with a 2 byte integer, which is what vb uses, and the value of that integer is less than 255, the first byte will be a zero (0), which tells notepad that you may be reading a unicode file. Changing it to a string and that first byte, even it is a "0", its ascii value is acually equal to 48 and thus notepad knows it is an ascii file.

Good Luck

hello sir good morning,

thank you for explaining me sir it really help me sir..okey sir i will make the missing arguement...and also sir i will make to display all the records i will get back to you to post my display all as soon as possible...thank you again sir for helping me always..more power to you sir....

Okay, since we have this part solved, please mark this thread as solved and use a new thread for the reading...

Good Luck

Okay, since we have this part solved, please mark this thread as solved and use a new thread for the reading...

Good Luck

hello sir i have problem sir..please help me on this sir..it will still overwrite.hoping for your positive responds...

Option Explicit
Public rndfilname As String
Public r_id As String
Public rndnum As Integer
Public age As String, stdidno As String
Public lname As String, fname As String, address As String
Public stdcount As Integer


Private Type student
idno As String * 6
stdage As String * 4
lastname As String * 10
firstname As String * 10
stdadd As String * 20
End Type
Private studentrecord As student


Private Sub Form_Load()

rndfilname = App.Path & "\student.dat"

Left = (Screen.Width - Width) / 2
Top = (Screen.Height - Height) / 2

stdcount = stdcount + 1
End Sub

Private Sub cmdsave_Click()

stdcount = 1
rndnum = FreeFile


  stdidno = Trim(txtid.Text)
  lname = Trim(txtlname.Text)
  fname = Trim(txtfname.Text)
  age = Trim(txtage.Text)
  address = Trim(txtaddress.Text)
 
  
  
  
  With studentrecord
   .idno = stdidno
   .lastname = lname
   .firstname = fname
   .stdage = age
   .stdadd = address
  End With
  

  Open rndfilname For Random Access Write As #rndnum Len = Len(studentrecord)

  stdcount = stdcount + 1
  Put #rndnum, stdcount, studentrecord
  
  
  clear


Close #rndnum


End Sub
Private Sub cmdexit_Click()
Unload Me
End Sub


Private Sub clear()
 txtid.Text = ""
 txtlname.Text = ""
 txtfname.Text = ""
 txtage.Text = ""
 txtaddress.Text = ""
End Sub

Because you set stdcount = 1 on line 32 which should be line 27...

Good Luck

hello sir thank you for the reply..i will write again sir if have doubt..thank you again sir..more power to you...

Because you set stdcount = 1 on line 32 which should be line 27...

Good Luck

hello sir good morning,

sir i have problem i change the line 27 to stdcount = 1.. but if i input 3rd data this will overwrite from my
1st data...example 0001 jemz gimz 24 united states 0002 karl mark 26 canada..
and if i will input 0003 shelly cashman 30 Australia this will overwrite to the 0001..can you help me sir..and also sir is this possible to display like this sir.

0001 jemz gimz 24 united states
0002 karl mark 26 canada
0003 shelly cashman 30 Australia

because when i look at to the file.the display is like this

0001 jemz gimz 24 united states 0002 karl mark 26 canada


please help me sir hoping for your positive responds....thank you in advance...

As I told you before, you will have to figure out a way to know how many lines are in the file to begin with and until that time that you do, of course you are going to overwrite informaiton between instances of your form being activated. You could use GetSetting and SaveSetting if you wish or you could figure out how to calculate the number of records there are based upon file size (that is a big hint there!!!). Then don't forget if you go with the calculation, if you ever delete a record from the file, you will have to move your records so you don't have any extra bytes hanging around...

To make it so that it goes line by line add a member to your type as the last entry of the type...

Private Type student
  idno As String * 6
  stdage As String * 4
  lastname As String * 10
  firstname As String * 10
  stdadd As String * 20
  NewLine As String * 2
End Type

then...

With studentrecord
  .idno = stdidno
  .lastname = lname
  .firstname = fname
  .stdage = age
  .stdadd = address
  .NewLine = vbNewLine
End With

Another way, instead of adding the vbnewline is to not use a UDT (User Defined Type) but instead use a string variable and you concatinate onto that string all the formatting you want and use print instead...

Good Luck

As I told you before, you will have to figure out a way to know how many lines are in the file to begin with and until that time that you do, of course you are going to overwrite informaiton between instances of your form being activated. You could use GetSetting and SaveSetting if you wish or you could figure out how to calculate the number of records there are based upon file size (that is a big hint there!!!). Then don't forget if you go with the calculation, if you ever delete a record from the file, you will have to move your records so you don't have any extra bytes hanging around...

To make it so that it goes line by line add a member to your type as the last entry of the type...

Private Type student
  idno As String * 6
  stdage As String * 4
  lastname As String * 10
  firstname As String * 10
  stdadd As String * 20
  NewLine As String * 2
End Type

then...

With studentrecord
  .idno = stdidno
  .lastname = lname
  .firstname = fname
  .stdage = age
  .stdadd = address
  .NewLine = vbNewLine
End With

Another way, instead of adding the vbnewline is to not use a UDT (User Defined Type) but instead use a string variable and you concatinate onto that string all the formatting you want and use print instead...

Good Luck

hello sir,

good morning sir, thank you for the reply and helping me..okey sir i will try this sir and i will write again if i have doubt..more power to you sir...thank you again sir...

As I told you before, you will have to figure out a way to know how many lines are in the file to begin with and until that time that you do, of course you are going to overwrite informaiton between instances of your form being activated. You could use GetSetting and SaveSetting if you wish or you could figure out how to calculate the number of records there are based upon file size (that is a big hint there!!!). Then don't forget if you go with the calculation, if you ever delete a record from the file, you will have to move your records so you don't have any extra bytes hanging around...

To make it so that it goes line by line add a member to your type as the last entry of the type...

Private Type student
  idno As String * 6
  stdage As String * 4
  lastname As String * 10
  firstname As String * 10
  stdadd As String * 20
  NewLine As String * 2
End Type

then...

With studentrecord
  .idno = stdidno
  .lastname = lname
  .firstname = fname
  .stdage = age
  .stdadd = address
  .NewLine = vbNewLine
End With

Another way, instead of adding the vbnewline is to not use a UDT (User Defined Type) but instead use a string variable and you concatinate onto that string all the formatting you want and use print instead...

Good Luck

hello sir,

sir i have problem and i am confuse..it will overwrite if i will close the form and compile it again..here is my code sir.correct me if i am wrong sir......and also sir if i will use the get #rndnum,stdcount,studentrecord after my put statement i will get error..so i remove it from my form..also sir in my form_load stdcount =0 so that it will not create indention am i right with this sir?..please help me sir hoping for your positive responds..here is my attachment sir...

Okay, lets make this simple and use a CSV format instead of this because you have constantly ignored my advise to you in that you would have to get a count of records that are already in the file...

1. assemble your string to print like so...
strStringToPrint = text1.text & "," & text2.text & "," & text3.text

2. check to see if record has already been entered (see below)
3. open file for Append...
4. use print #FNumb, strStingToPrint

Then to retrieve

Dim FName As String, FNumb As Integer
Dim FCont As String, LineArray() As String, ElementArray() As String

FName = "C:\z\test.txt"
FNumb = FreeFile

Open FName For Binary As #FNumb
FCont = Input(FileLen(FName), #FNumb)
Close #FNumb

LineArray = Split(FCont, vbNewLine)
MsgBox UBound(LineArray) + 1 & " number of lines in file"

ElementArray = Split(LineArray(0), ",")
MsgBox "number of fields in line = " & UBound(ElementArray) + 1

After that, if you edit a record you will need to edit the array that holds the line (LineArray) and then rewrite the whole file by opening for output and using print #FNumb, Join(LineArray, vbNewLine)

If you add a record, you can open for append and just write the append information or you can redim preserve LineArray(0 To UBound(LineArray) + 1) and save the information into the last element of the LineArray. Then Open For Output to overwrite as mentioned just above.

To delete a line, you would need to move any lines after the line you are deleting up one... (deleting line 3 of ?)

For LoopCount = LineToDelete To UBound(LineArray) - 1
  LineArray(LoopCount) = LineArray(LoopCount + 1)
Next LoopCount
Redim Preserve LineArray(0 To UBound(LineArray) - 1)

Then open for output...

Okay, lets make this simple and use a CSV format instead of this because you have constantly ignored my advise to you in that you would have to get a count of records that are already in the file...

1. assemble your string to print like so...
strStringToPrint = text1.text & "," & text2.text & "," & text3.text

2. check to see if record has already been entered (see below)
3. open file for Append...
4. use print #FNumb, strStingToPrint

Then to retrieve

Dim FName As String, FNumb As Integer
Dim FCont As String, LineArray() As String, ElementArray() As String

FName = "C:\z\test.txt"
FNumb = FreeFile

Open FName For Binary As #FNumb
FCont = Input(FileLen(FName), #FNumb)
Close #FNumb

LineArray = Split(FCont, vbNewLine)
MsgBox UBound(LineArray) + 1 & " number of lines in file"

ElementArray = Split(LineArray(0), ",")
MsgBox "number of fields in line = " & UBound(ElementArray) + 1

After that, if you edit a record you will need to edit the array that holds the line (LineArray) and then rewrite the whole file by opening for output and using print #FNumb, Join(LineArray, vbNewLine)

If you add a record, you can open for append and just write the append information or you can redim preserve LineArray(0 To UBound(LineArray) + 1) and save the information into the last element of the LineArray. Then Open For Output to overwrite as mentioned just above.

To delete a line, you would need to move any lines after the line you are deleting up one... (deleting line 3 of ?)

For LoopCount = LineToDelete To UBound(LineArray) - 1
  LineArray(LoopCount) = LineArray(LoopCount + 1)
Next LoopCount
Redim Preserve LineArray(0 To UBound(LineArray) - 1)

Then open for output...

hello sir thank you for the reply


sir i would like to ask apologize to you if there are some instruction that i did not follow or i failed to follow it's because i don't have idea what are you trying to say..
in this line sir "you will have to figure out a way to know how many lines are in the file to begin with and until that time that you do, of course you are going to overwrite information between instances of your form being activated"...and also sir the one that you said calculate the records...i don't know how sir...

sir before i am going to try the code using CSV, can you please teach me first the one that you mentioned above..please help me sir hoping for your positive responds...

>As I told you before, you will have to figure out a way to know how many lines are in the file to begin with and until that time that you do, of course you are going to overwrite informaiton between instances of your form being activated. You could use GetSetting and SaveSetting if you wish or you could figure out how to calculate the number of records there are based upon file size (that is a big hint there!!!).

To calculate how many records are in the file by filesize, you would need to know the size of the file (FileLen Function) divided by the record size you are storing (Hint: You know this...).

See the green words above... Look them up!

Good Luck

Edited 6 Years Ago by vb5prgrmr: n/a

>As I told you before, you will have to figure out a way to know how many lines are in the file to begin with and until that time that you do, of course you are going to overwrite informaiton between instances of your form being activated. You could use GetSetting and SaveSetting if you wish or you could figure out how to calculate the number of records there are based upon file size (that is a big hint there!!!).

To calculate how many records are in the file by filesize, you would need to know the size of the file (FileLen Function) divided by the record size you are storing (Hint: You know this...).

See the green words above... Look them up!

Good Luck

Hello sir thank you for the reply,

okay sir i will try this sir...by the way sir if i will use the GetSetting and SaveSetting where am i going to put this after my PUT statement?please correct me if i am wrong sir hoping for your positive responds...

GetSetting at form load and SaveSetting at form query unload or form unload.

Good Luck

hello sir, i'm back...sir correct me if i am wrong i already get the record without overwriting the first record..please correct me if i am wrong...thank you in advance hoping for your positive responds...

Private Sub cmdsave_Click()


rndnum = FreeFile



  stdidno = Trim(txtid.Text)
  lname = Trim(txtlname.Text)
  fname = Trim(txtfname.Text)
  age = Trim(txtage.Text)
  address = Trim(txtaddress.Text)
 
  
  
  
  With studentrecord
   .idno = stdidno
   .lastname = lname
   .firstname = fname
   .stdage = age
   .stdadd = address
   .nextline = vbNewLine
  End With
  

  Open rndfilname For Random Access Write As #rndnum Len = Len(studentrecord)
  stdcount = stdcount + 1

  Put #rndnum, Int(LOF(rndnum) / Len(studentrecord)) + 1, studentrecord

  clear


Close #rndnum


End Sub
This article has been dead for over six months. Start a new discussion instead.