I currently user logon information stored in one file, i wish to be able to delete users from this file currently i am selecting the users from a lstbox which retrieves all the user names from the txtfile.

Public Sub CmdRetrieveUsers_Click()
LstUsr.Clear
CmdEditUsr.Enabled = True
Open App.Path & "\Logon.Password Info.txt" For Input As #1
        Do While Not EOF(1)
            Input #1, Username, PasswordFromFile, AccountType               
            LstUsr.AddItem (Username)
        Loop
Close #1
End Sub

What i want to be able to do is, select the user you wish to delete by selecting/clicking on their username from the list box. So far i have read that i need to open the file for input (reading), open a temp file for output/append?? then transfer the data across and kill the original file then rename the temp file. The code i have managed to write so far.

Private Sub CmdDelete_Click()
Open App.Path & "\Logon.Password Info.txt" For Input As #1
Open App.Path & "\Logon.Password Temp.txt" For Output As #2
       
        For i = 0 To LstUsr.ListIndex - 1
            Input #1, Username, Password, AccountType
            Write #2, Username, Password, AccountType
        Next i
        For i = LstUsr.ListIndex To LstUsr.ListCount -1
            Input #1, Username, Password, AccountType
            Write #2, Username, Password, AccountType
        Next i
    
    Close #2
    Close #1
    'Kill App.Path & "\Logon.Password Info.txt"
    'Name App.Path & "\Logon.Password Temp.txt" As App.Path & "\Logon.Password Info.txt"
End Sub

I know i've gone wrong somewhere in it but just cant figure out where.

Thanks in advance :)

Edited 5 Years Ago by gowans07: n/a

Reading from the file is not dependent on the index in your list box. So, to skip one row, you have to do an input from #1 and then DON'T WRITE to #2. So, after line 8, put in an Input #1, then start your next loop on line 9 like you have now. Should look like this:

Private Sub CmdDelete_Click()
Open App.Path & "\Logon.Password Info.txt" For Input As #1
Open App.Path & "\Logon.Password Temp.txt" For Output As #2
       
        For i = 0 To LstUsr.ListIndex - 1
            Input #1, Username, Password, AccountType
            Write #2, Username, Password, AccountType
        Next i
        Input #1, Username, Password, AccountType     '    <---- Here's the new line
        For i = LstUsr.ListIndex + 1 To LstUsr.ListCount
            Input #1, Username, Password, AccountType
            Write #2, Username, Password, AccountType
        Next i
    
    Close #2
    Close #1
    'Kill App.Path & "\Logon.Password Info.txt"
    'Name App.Path & "\Logon.Password Temp.txt" As App.Path & "\Logon.Password Info.txt"
End Sub

You were very close, though!

Edited 5 Years Ago by BitBlt: n/a

Comments
Thanks :)

Haha knew it was just something so simple.... thanks :)
Line 10 just needs lstusr.listcount -1 for anyone that needs it

Edited 5 Years Ago by gowans07: n/a

This question has already been answered. Start a new discussion instead.