 |

07-22-2003, 10:58 AM
|
 |
Junior Contributor
|
|
Join Date: Sep 2002
Location: Inside your head
Posts: 242
|
|
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.
|

07-22-2003, 11:02 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
|
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
|

07-22-2003, 01:17 PM
|
 |
Junior Contributor
|
|
Join Date: Sep 2002
Location: Inside your head
Posts: 242
|
|
Mikerosoft - ok i see what was wrong there, I changed
to
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.
|

07-22-2003, 01:27 PM
|
 |
Hell's Angel
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
|
|
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"
|

07-22-2003, 01:47 PM
|
 |
Junior Contributor
|
|
Join Date: Sep 2002
Location: Inside your head
Posts: 242
|
|
|
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.
|

07-22-2003, 01:53 PM
|
 |
Google Hound
Retired Moderator * Guru *
|
|
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
|
|
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
|

07-22-2003, 02:22 PM
|
 |
Junior Contributor
|
|
Join Date: Sep 2002
Location: Inside your head
Posts: 242
|
|
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.
|

07-22-2003, 04:01 PM
|
 |
Hell's Angel
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
|
|
|
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"
|

07-23-2003, 01:08 PM
|
 |
Junior Contributor
|
|
Join Date: Sep 2002
Location: Inside your head
Posts: 242
|
|
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.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|