• Welcome to Valhalla Legends Archive.
 

Loading Access From A File And Splitting It

Started by HardCoded., August 15, 2006, 08:06 PM

Previous topic - Next topic

HardCoded.

Components:
ListBox - ListUser
ListBox - ListAccess
File - Users.txt


Public Sub AddUsers()
Dim String1 As String,S() As String
Open App.Path & "\Users.txt" For #1 As Input
Do Until EOF(1)
Line Input #1,String1
S() = Split(String1," ",2)
Form1.ListUser.Additem S(0)
Form1.ListAccess.Additem S(1)
Loop
Close #1
End Sub

ZOMG

rabbit

Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.


HardCoded.

yes im Ratio@useast and there are people out there that are newbs to vb6 so pleasE?
ZOMG

rabbit

And if they ask a question we will answer.  Preemptive answering is called spam.  Please stop.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

FrOzeN

Quote from: Ratio on August 15, 2006, 10:07 PM
.. and there are people out there that are newbs to vb6 so pleasE?
Incase you didn't notice, your a newb to VB6 aswell. In both of your posts the code you posted is poorly written.
~ FrOzeN

warz

Well, no offense, but how else would you write the code in this post so that it is not poorly written? :-P

FrOzeN

#7
Probably like..
Public Sub AddUsers()
    Dim strLine As String, strParts() As String, intFile As Integer
    intFile = FreeFile
   
    Open App.Path & "\Users.txt" For Input As intFile
        Do Until EOF(intFile)
            Line Input #intFile, strLine
            strParts() = Split(strLine, Space$(1), 2)
            Form1.ListUser.AddItem strParts(0)
            Form1.ListAccess.AddItem strParts(1)
        Loop
    Close intFile
End Sub


[EDIT] Also, both pieces of his code are incorrect when opening the file.
The "For [Number] As [Scope]" should be "For [Scope] As [Number]".
~ FrOzeN

HardCoded.

that still is not better code just a differnt way
ZOMG

rabbit

No, it's better.  It's still not good, though.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

FrOzeN

Quote from: rabbit on August 28, 2006, 08:42 PM
No, it's better.  It's still not good, though.
Post what you'd consider "good".

The only downsides that I can see with my code, is that it could error if the program (.exe) was placed in a root directory, where App.Path would return the path along with the backslash. Also, there should probably be a check on strLine before trying to Split() it, or I could add an "& Space$(1)" after the strLine inside the Split() call.
~ FrOzeN

rabbit

Option Explicit

Private Type UserAccessType
    strName As String
    intAccess As Integer
End Type

Private arrUserList() As UserAccessType

Public Sub LoadUsers()
    Dim strLine As String
    Dim strParts() As String
    ReDim arrUserList(0)
   
    On Error GoTo LoadUsers_Err
   
    Open "D:\users.txt" For Input As #1
        Do Until EOF(1)
            Line Input #1, strLine
            strParts() = Split(strLine, " ", 2)
           
            Call AddUser(strParts(0), strParts(1))
        Loop
    Close #1
   
    Exit Sub
   
LoadUsers_Err:
    Debug.Print "Error #" & Err.Number
    Debug.Print "Error : " & Err.Description
    Debug.Print "Source : " & Err.Source
    Debug.Print "Help File : " & Err.HelpFile
    Debug.Print "Help Context : " & Err.HelpContext
    Err.Clear
End Sub

Public Sub AddUser(ByVal UserName As String, ByVal UserAccess As String)
    On Error GoTo AddUser_Err
   
    If arrUserList(UBound(arrUserList)).strName <> vbNullString Then
        ReDim Preserve arrUserList(UBound(arrUserList) + 1)
    End If
   
    If UserName <> vbNullString And IsNumeric(UserAccess) Then
        ' assuming it's an integer, which is bad, but w/e
        arrUserList(UBound(arrUserList)).strName = UserName
        arrUserList(UBound(arrUserList)).intAccess = CInt(UserAccess)
    Else
        Debug.Print "Error: username is null or access is not numeric"
        Exit Sub
    End If
   
    Exit Sub
   
AddUser_Err:
    Debug.Print "Error #" & Err.Number
    Debug.Print "Error : " & Err.Description
    Debug.Print "Source : " & Err.Source
    Debug.Print "Help File : " & Err.HelpFile
    Debug.Print "Help Context : " & Err.HelpContext
    Err.Clear
End Sub
Something of that nature, of course with the error handlers and such tailored to the program.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Warrior

Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

UserLoser

#13
Lot of unnecessary and messy code there...prone to bugs and errors.

Personally, I would not do it my self the following way, but try something along the lines of this (just wrote this up quickly did not test thoroughly)... It's basically rabbit's function cleaned up and crash free.


Private Const Config_Filename As String = ".\Config.ini"

Private Type DatabaseEntry
    Usermask As String
    Flags As Long
End Type

Private DatabaseUsers() As DatabaseEntry

Public Sub LoadUserDatabase()
    On Error GoTo ErrorHandler
   
    Dim FileNumber As Integer
    Dim DataArray As Variant
    Dim FileInput As String
   
    'Reset our internal database list
    ReDim DatabaseUsers(0)
   
    'Local constants
    Const Username = 0, Access = 1
   
    'Get filenumber
    FileNumber = FreeFile()
   
    'Check file, set function line numbers on places where error could possibly occur for debugging purposes post compilation and release.
17: If (FileLen(Config_Filename) > 0) Then
        'Open file
19:         Open Config_Filename For Input As FileNumber
            'Loop through the file
            While EOF(FileNumber) = False
                'Get our next line
23:             Line Input #FileNumber, FileInput
                'Split our data
25:             DataArray = Split(FileInput, " ", 2)
                'Check bounds of array
                If (UBound(DataArray) = 1) Then
                    'Add user to database
29:                 AddDatabaseEntry DataArray(Username), DataArray(Access)
                End If
            Wend
            'End of file, erase data
33:         Erase DataArray
            'Close file
        Close FileNumber
    End If
   
    Exit Sub
   
ErrorHandler:
    MsgBox "Error (LoadUserDatabase @ line " & Erl & "): " & Error(Err.Number), vbCritical Or vbMsgBoxSetForeground
    Err.Clear
End Sub

Private Sub AddDatabaseEntry(ByVal Usermask As String, ByVal Flags As Long)
    Dim NewSize As Long
   
    'Get current size and one
    NewSize = UBound(DatabaseUsers) + 1
   
    'Resize our array
    ReDim Preserve DatabaseUsers(0 To NewSize)
   
    'Add entry
    DatabaseUsers(NewSize).Usermask = Usermask
    DatabaseUsers(NewSize).Flags = Flags
End Sub


Have not written in VB in a few months.  UL FTW.

FrOzeN

#14
rabbit, your code isn't much better. It just has error handlers which I mistakenly left out.

Your not checking if the file exists.
Your using "#1" which could cause errors if that number is already in use for file handling.
Your not checking the size of strParts() before parsing it. Incase of a corrupt user.txt file.
Your error handling debug printout is a fair overkill.

The line "If UserName <> vbNullString And IsNumeric(UserAccess) Then" is bad coding practise because it can cause problems at times where the compiler may misinterpret this as a binary comparison. A better/more efficient equivalent would be, "If (LenB(UserName) <> 0) And (IsNumeric(UserAccess) = True) Then".

---
UL, the only comment on yours would be the naming convention ".ini" for something that isn't formatted correctly. Personally, I think it's best to stick to the .txt extension unless your specifically using .ini files to store data in there intended format.

:)
~ FrOzeN