Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > Reading an Account File


Reply
 
Thread Tools Display Modes
  #1  
Old 07-22-2003, 10:58 AM
Chrono23's Avatar
Chrono23 Chrono23 is offline
Junior Contributor
 
Join Date: Sep 2002
Location: Inside your head
Posts: 242
Default Reading an Account File


I'm trying to make my program have accounts using a UDT and a random access file. But I keep getting errors when I try and load it. Here's the code:

Code:
Public Sub LoadAccounts()
    Dim x As Integer
    Dim iFreeFile As Integer
    
    iFreeFile = FreeFile
    
    Open App.Path & "\Accounts\users.act" For Random As iFreeFile
        ReDim Account(EOF(iFreeFile))
        Debug.Print "Account No. = " & UBound(Account)
        For x = 1 To EOF(iFreeFile) + 1
            Get #iFreeFile, x, Account(x)
        Next x
    Close iFreeFile
End Sub
__________________
Why is there war, famine and poverty in the world? Here's my answer: ¯\(°_o)/¯

Yes, I made my own avatar. And no, I'm not going to make one for you.... sorry.
Reply With Quote
  #2  
Old 07-22-2003, 11:02 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

Whats is this ???

ReDim Account(EOF(iFreeFile))

may be you need another way:

NumberOfRecords = LOF(iFreeFile) / LEN(Random_UDF)
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
Reply With Quote
  #3  
Old 07-22-2003, 01:17 PM
Chrono23's Avatar
Chrono23 Chrono23 is offline
Junior Contributor
 
Join Date: Sep 2002
Location: Inside your head
Posts: 242
Default

Mikerosoft - ok i see what was wrong there, I changed
Code:
EOF(iFreeFile)
to
Code:
LOF(iFreeFile)
Also, here is a brief algorithm of what i want to do:

1. Sub Main Executes, redims account() to account(0) then calls LoadAccounts

(In LoadAccounts() code)
2. determine current freefile
3. open account (*.act) file as random access
4. Resize the account() array to contain all the accounts
5. Print the total # of accounts in the immediate window
6. Read all of the accounts and all their variables (4 per account) and add each one to the corresponding slot in the Account() array (for loop)
7. Close the freefile
8. Continue rest of program.....

No. 6 is what's giving me the problem. It's generating a 'Run-Time Error '59' Bad Record Length' and highlights the Get #iFreeFile argument. Incidentally, if you are to lazy to look at the error lookup (or if you dont have it) RT Error 59 = "Unexpected Network Error Occured." If you need the code for writing the file, I'll be happy to post it.
__________________
Why is there war, famine and poverty in the world? Here's my answer: ¯\(°_o)/¯

Yes, I made my own avatar. And no, I'm not going to make one for you.... sorry.
Reply With Quote
  #4  
Old 07-22-2003, 01:27 PM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

Mikecrosoft is still correct. When you redim the array you still need to divide by the len of the UDT. Otherwise you creating the same number of accounts as there are bytes in the file. This I doubt the UDT is 1 byte long this is incorrect.

You also need to tell the Open statement how long each record is like this:
Code:
Open App.Path & "\Accounts\users.act" For Random As iFreeFile Len = Len(Accounts(0))
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #5  
Old 07-22-2003, 01:47 PM
Chrono23's Avatar
Chrono23 Chrono23 is offline
Junior Contributor
 
Join Date: Sep 2002
Location: Inside your head
Posts: 242
Default

Ok I made progress (i think) not I get 'RT Error 63 "Your file waiting to be printed was deleted." '

here the revised code:

Public Sub LoadAccounts()
Dim x As Integer
Dim iFreeFile As Integer
Dim NoOfAccts As Integer

iFreeFile = FreeFile

Open App.Path & "\Accounts\users.act" For Random As iFreeFile Len = Len(Account(0))
NoOfAccts = (LOF(iFreeFile) / Len(Account(0)))
ReDim Account(NoOfAccts)
Debug.Print "Account No. = " & UBound(Account)
For x = 0 To LOF(iFreeFile)
Get #iFreeFile, x, Account(x)
Next x
Close iFreeFile
End Sub
__________________
Why is there war, famine and poverty in the world? Here's my answer: ¯\(°_o)/¯

Yes, I made my own avatar. And no, I'm not going to make one for you.... sorry.
Reply With Quote
  #6  
Old 07-22-2003, 01:53 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

Try changing:
Code:
For x = 0 To LOF(iFreeFile)
to:
Code:
For x = 0 To NoOfAccts
and I think you're about there.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #7  
Old 07-22-2003, 02:22 PM
Chrono23's Avatar
Chrono23 Chrono23 is offline
Junior Contributor
 
Join Date: Sep 2002
Location: Inside your head
Posts: 242
Default

No, I still have RT Error 63 Maybe it's how i wrote to the file. Here's the code for that:
Code:
Public Sub CreateAccount(Username As String, Password As String, Email As String)
    Dim x As Integer
    Dim iFreeFile As Integer
    
    For x = 0 To UBound(Account) 'look for empty existing account slot
        If Account(x).ID = 0 Then
            Account(x).ID = x + 1
            Account(x).Username = Username
            Account(x).Password = Password
            Account(x).Email = Email
            
            iFreeFile = FreeFile
            Open App.Path & "\Accounts\users.act" For Random As iFreeFile
                Put #iFreeFile, x + 1, Account(x).ID
                Put #iFreeFile, x + 1, Account(x).Username
                Put #iFreeFile, x + 1, Account(x).Password
                Put #iFreeFile, x + 1, Account(x).Email
            Close iFreeFile
            
            Exit Sub
        End If
    Next x
    
    ReDim Preserve Account(UBound(Account) + 1)
    Account(UBound(Account)).ID = UBound(Account)
    Account(UBound(Account)).Username = Username
    Account(UBound(Account)).Password = Password
    Account(UBound(Account)).Email = Email
            
    iFreeFile = FreeFile
    Open App.Path & "\Accounts\users.act" For Random As iFreeFile
        Put #iFreeFile, UBound(Account), Account(UBound(Account)).ID
        Put #iFreeFile, UBound(Account), Account(UBound(Account)).Username
        Put #iFreeFile, UBound(Account), Account(UBound(Account)).Password
        Put #iFreeFile, UBound(Account), Account(UBound(Account)).Email
    Close iFreeFile
End Sub
__________________
Why is there war, famine and poverty in the world? Here's my answer: ¯\(°_o)/¯

Yes, I made my own avatar. And no, I'm not going to make one for you.... sorry.
Reply With Quote
  #8  
Old 07-22-2003, 04:01 PM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

Again you are not specifying the record length in your open statements. You must do this when dealing with Random access files.

Which is producing the error?
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #9  
Old 07-23-2003, 01:08 PM
Chrono23's Avatar
Chrono23 Chrono23 is offline
Junior Contributor
 
Join Date: Sep 2002
Location: Inside your head
Posts: 242
Default

So you mean I have to specify the Len each tike i open a Random Access file? This padawan has much to learn! lol

As for the question of "which is causing the error", the LoadAccounts() Sub is the one that finds an error, but I posted the code writing to the file in case I somehow wrote to the file differently than I'm reading it.
__________________
Why is there war, famine and poverty in the world? Here's my answer: ¯\(°_o)/¯

Yes, I made my own avatar. And no, I'm not going to make one for you.... sorry.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation Problem - PLs help urgenlty dpdsouza Installation / Documentation 4 12-02-2004 07:09 PM
reading particular values in a file bigpapasmurf Miscellaneous Languages 2 03-12-2003 04:38 PM
Doesn't want to register! MikeyM Installation / Documentation 5 03-02-2003 08:22 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->