Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Access Files

Status
Not open for further replies.

namgc2

Programmer
Sep 8, 2007
12
0
0
GB
Hi,

I'm using a Macro which searches through approx 17000 records. I have only used sequential file handling up to now. Can anyone give a simple example of some random access code so I can figure out how to incorporate it into my programs.

I have tried searches through previous threads will no luck.

Thanks
 
What type of files are you digging through and where? What language is your macro written in?

Can you give us a little more to go on? Perhaps post your current code and notate what you'd like it to do.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Hi,

I'm using Attachmate. I've managed to put some basic code together, the first part creates a new file with some test data in it. The second part opens the file and will read the data. (Some pretty basic stuff here !)

The only problem I have now is when I create my files using Excel and save as a text file (In the same manner as the data for my other scripts which read the files sequentially) the script with the random access file handling doesn't recognise any of the data in the file. I have to copy the data from Excel and paste it into Notepad. The random access file handling then recognises this and can use the data.

Any idea why this is?

Many thanks
 
I'd imagine it has to do with how your handling the txt recieved from the file. Without seeing your code I really have no idea. As you mention the file is being created in excel I'd recomend using the Excel object to pull the necessary information into your macro. Here's an example. You'll need to change the red bits to match your files.

Code:
Sub Main

    'Excel Objects
    Dim appExcel As Object
    Dim wbExcel As Object
    Dim aSheet As Object

    Set AppExcel = CreateObject("Excel.Application")
    Set wbExcel = AppExcel.WorkBooks.Open([COLOR=red]"Drive/YourFile.xls"[/color])
    Set aSheet = wbExcel.Sheets([COLOR=red]"SheetName"[/color])

    For x = 1 to aSheet.UsedRange.Rows.Count
        msgbox aSheet.Cells(x,1).text                     
    Next 
    appExcel.Quit

End Sub
There are many more examples of using the Excel object in this forum including an FAQ by Calculus. Again if you post your code I may be able to help further. Hope this helps.

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top