Solved

Random File and VB

Posted on 1998-07-16
10
189 Views
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?
Thanks
0
Comment
Question by:dhill10358
10 Comments
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
Considering the ease of using databases with VB, I would suggest you use a database instead of a random access file.
0
 
LVL 9

Expert Comment

by:cymbolic
Comment Utility
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
'prelims
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.
0
 

Author Comment

by:dhill10358
Comment Utility
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.

thanks,





 
0
 
LVL 1

Expert Comment

by:HenryChanE
Comment Utility
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:
   TotAmt=0
   For I = 1 to MaxRec
     Get #1,I, gMyCheck
     TotAmt= TotAmt +gMyCheck.Amount
   Next I
   ControlD.text=TotAmt

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

Author Comment

by:dhill10358
Comment Utility
Henry,

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
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dhill10358
Comment Utility
Adjusted points to 125
0
 
LVL 1

Expert Comment

by:HenryChanE
Comment Utility
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 ?
0
 
LVL 2

Expert Comment

by:seahpc
Comment Utility
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
loop
Close #1
0
 

Author Comment

by:dhill10358
Comment Utility
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.

thanks,

David
0
 

Accepted Solution

by:
HenryChan earned 140 total points
Comment Utility
David,
  I've sent the code to you, it's written in VB4-32bit. It shows the most important feature for radom access file, you should modify the code for a real apps..

Henry
 

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now