We help IT Professionals succeed at work.

Random File and VB

dhill10358 asked
Last Modified: 2010-05-03
I am trying to write a small check program.  I want it to request information (payee, check number, and amount) for each check and store this
information in a random file. I need it to display the information for a check by reading the file when
user input a check number. And I would like the program to dislplay the total amount of all issued checks after every transaction.

Any ideas?
Watch Question

Considering the ease of using databases with VB, I would suggest you use a database instead of a random access file.

The essential method for doing this lies in the fundamental FIle I/O provided with VB, which came along relatively unchanged from Quick  Basic and Professional basic.  Generally, your random file is a fixed length record file.  You can create it by writing records to it sequentially, of by using binary file i/o (open "myfile" for binary as #1, etc).  The only trick is placing the relative record number of the record written into your related record.  Then when you read through your related (master) records you do the following to get your random related record:

dim x$, pos as long, relrec as long, lrecl as integer
Open "RandomFileName" for binary as #1
lrecl=320  'whatever your fixed length is for each record
x$=space$(lrecl) ' make buffer

'then to get a record
relrec= <relative record pointer value from master record>
'in one statement
get #1,((relrec-1)*lrecl)+1,x$

'or seprate seek
 seek #1,((relrec-1)*lrecl)+1
 get #1,,x$

'your random record is in x$.  TO get bunches, read down through your master, reading all related random records & summing? whatever you want.

This is a very fast method and quite useable if your random file does not have a lot of update activity (deletes, etc), otherwise you need more sophisticated processes to preserve file space over time.

Of course, you could always use a database.  

And, if you are trying to write your own version of Quicken (that's what it sounds like from your question) you really need to get into methods of indexing and handling deleted records, etc.   In this case, a real database will save you a lot of grief.


I don't need anything complex like quicken, ect.  I just need a very basic program that will request information (payee, check number, and amount) for each check and display the information when I input the check number.  Also I want it to total the number of all checks when I input the check number.

I am new to VB.  I use Access97. Random files are new to me.  I am talking about maybe 5 to 10 checks.  I need to see an example so I can learn how to do this.  If I can get this example, I will apply it to other projects.


Pls. put the code below into approiate position:

1) Declare your field in a module (*.BAS) :
   Type MyCheck
     Payee as String * X (X=Max. len. for this field)
     Chk_No as Long
     Amount as Currency
   End Type

2) Declare the variables in your form :
   Dim gMyCheck as MyCheck
   Dim MaxRec as Long
   Dim TotAmt as Currency
   Dim I as Long  

3) To open your file:
   Open "Filename" For Random As #1 Len=(X+4+8)
   MaxRec=FileLen("Filename") / Len(X+4+8)

4) To search and display record:
   For I = 1 to MaxRec
     Get #1,I, gMyCheck
     If gMyCheck.Chk_No = (Check No you want to display) then
       ControlA.text= gMyCheck.Payee
       ControlB.text= gMyCheck.Amount
       Exit For
     End If
   Next I

5) To add your record:
       gMyCheck.Payee = ControlA.text
       gMyCheck.Amount = CCur(ControlB.text)
       gMyCheck.Chk_No = CLng(ControlC.text)
       Put #1,MaxRec+ 1, gMyCheck
       MaxRec =MaxRec + 1

6) To sum the amount:
   For I = 1 to MaxRec
     Get #1,I, gMyCheck
     TotAmt= TotAmt +gMyCheck.Amount
   Next I

Remember to close to file using Close #1 after use.



Thank you for you response.  I am getting error messages.  such as "invalid outside procedure".  VB is new to me.  If you can be so kind, would you edit the code you gave me so it works as close as possible to the directions listed in my description?  I have tried to fix it myself, but I don't know what I am doing.  

Thank you


Adjusted points to 125
It's not easy to write a complete list of code inside this text box. Can you post your e-mail so that I can send the source code to you directly ?

try this :
' text1 = Payee
' text2 = CheckNumber
' text3 = Amount

'This will write teh record into a file
'Each field are delimted by a comma
Open FileName for append as #1
   Print #1, text1 & "," & text2 & "," & text3
Close #1

' text4 = CheckNumberForSearch
open FileName for input as #1
do until eof(1)
  line input #1, record
  if instr(1,record,text4.text) then
           msgbox record
  end if
Close #1


My Email Address is Dhill10358@aol.com.   If you send me the code, please make sure you lock the question, so I can give you credit for it.


Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!



Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)
