Results 1 to 4 of 4

Thread: delete strings from a text file

  1. #1

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Post

    what i want to do is use a text file with a .zan extention to store users in, thats no problem. but when i want to delete a user i get all users deleted. here is the code I'm using...

    Option Explicit

    Private Sub cboUser_KeyPress(KeyAscii As_ Integer)
    'don't allow the user to type in the
    ' combo
    cboUser.Text = ""
    End Sub

    Private Sub cboUser_KeyUp(KeyCode As_ Integer, Shift As Integer)
    'make sure the user can't type
    cboUser.Text = ""
    End Sub

    Private Sub DelUser()
    'sub to search for user name in my txt
    ' file
    Dim intStart As Integer, intPos As_ Integer, intResponse As Integer, intOffSet_ As Integer
    Dim strFindString As String,_ strSourceString As String

    'Start from the top
    frmDel.txtUsers.SetFocus
    frmDel.txtUsers.SelStart = 0
    intOffSet = 1
    intStart = frmDel.txtUsers.SelStart + 1
    strFindString = frmDel.cboUser.Text
    strSourceString = frmDel.txtUsers.Text

    intPos = InStr(intStart + 1,_ strSourceString, strFindString)

    If intPos Then
    'if the string comes after the first
    'line....
    frmDel.txtUsers.SelStart = intPos - 1
    frmDel.txtUsers.SelLength = Len_(strFindString)
    frmDel.txtUsers.SelText = ""
    SendKeys "{BKSP}" ' make sure there
    ' are no empty lines
    Else
    'if the string is on the first
    ' line....
    frmDel.txtUsers.SelStart = intPos
    frmDel.txtUsers.SelLength = Len_(strFindString)
    frmDel.txtUsers.SelText = ""
    SendKeys "{DEL}"
    End If
    End Sub

    Private Sub cmdCancel_Click()
    Unload Me
    End Sub

    Private Sub cmdDel_Click()
    If cboUser.Text = "" Then
    MsgBox "Please select a user to_ remove.", , "Error"
    Exit Sub
    End If
    Dim I As Integer
    For I = 1 To 20
    'check to see if the user has to
    'remain
    If Cn.mnuMsg(I).Caption =_ cboUser.Text Then
    MsgBox "You can't delete this_ user until all there Msg's have been_ viewed", , "Error"
    Exit Sub
    End If
    Next
    If txtPass.Text = "" Then
    'make sure there is a password
    MsgBox "Please enter your_ password.", , "Error"
    Exit Sub
    End If
    Dim pass As String, cPass As String
    'get the user pass from the reg (eww_ secure Not)
    pass = GetSetting(AppTitle, "Users",_ cboUser.Text)
    cPass = txtPass.Text
    'compare the pWords
    If pass = cPass Then
    Call DelUser
    'delete the users pWord from reg
    DeleteSetting AppTitle, "Users",_ cboUser.Text
    'update user file
    Open App.Path & "\Users.zan" For_ Output As #1
    Print #1, txtUsers.Text
    Close #1
    Else
    MsgBox "Please re-enter your_ password", , "Invalid Password"
    Exit Sub
    End If
    End Sub

    Private Sub Form_Load()
    frmZoomToFromTray Me, ZoomFormOpen,_ FromCenter, False
    KillX Me
    'load my user file into the txtbox to
    'work with
    LoadComboBox App.Path & "\Users.zan",_ cboUser
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    frmZoomToFromTray Me, ZoomFormClosed,_ FromCenter, False
    End Sub


    ok pasting it into this box threw it out of whack but i think i fixed all the wrapped lines. it worked fine until i tried it a minute ago. If anybody has a better way to store the Passwords fill me in. I origanly went ini but the passwords would match and my program couldn't tell even after i trimed the ini's return

    thanks

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    The problem is you're opening the users file for Output, this Automatically creates a New file, removing all your Users.

    Instead of updating the File on every delete and keeping the file in a Textbox,
    Why don't you just update the file when the user closes the form.
    That way you can skip the entire DelUser Sub Routine and You won't need to keep the file in a Textbox, eg.
    Code:
    Option Explicit
    
    Private Sub cmdDel_Click()
        Dim I As Integer
        Dim sPass As String
        
        If cboUser.ListIndex < 0 Then Exit Sub 'No Users
        
        If Len(txtPass) = 0 Then
            'make sure there is a password
            MsgBox "Please enter your password.", , "Error"
            Exit Sub
        End If
        
        For I = 1 To 20
            'check to see if the user has to remain
            If Cn.mnuMsg(I).Caption = cboUser Then
                MsgBox "You can't delete this user until all there Msg's have been viewed", , "Error"
                Exit For
            End If
        Next
        If I < 21 Then Exit Sub
        
        'get the user pass from the reg (eww_ secure Not)
        sPass = GetSetting(AppTitle, "Users", cboUser)
        'compare the pWords
        If sPass = txtPass Then
            cboUser.RemoveItem cboUser.ListIndex
            DeleteSetting AppTitle, "Users", cboUser.Text
            MsgBox "User Removed", vbInformation + vbOKOnly, "User Removed"
        Else
            MsgBox "Please re-enter your password", , "Invalid Password"
        End If
    End Sub
    
    Private Sub Form_Load()
        'Load the User File
        Dim iFile As Integer
        Dim sUserList As String
        
        iFile = FreeFile
        Open App.Path & "\Users.zan" For Input As iFile
        sUserList = Input(LOF(iFile), iFile)
        Close iFile
        While InStr(sUserList, vbCrLf)
            cboUser.AddItem Left(sUserList, InStr(sUserList, vbCrLf) - 1)
            sUserList = Mid$(sUserList, InStr(sUserList, vbCrLf) + 2)
        Wend
        If Len(sUserList) Then cboUser.AddItem sUserList
        If cboUser.ListCount Then cboUser.ListIndex = 0
        txtPass.PasswordChar = "*"
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        'Update the User File
        Dim iFile As Integer
        Dim iIndex As Integer
        
        iFile = FreeFile
        Open App.Path & "\Users.zan" For Output As iFile
        For iIndex = 0 To cboUser.ListCount - 1
            Print #iFile, cboUser.List(iIndex)
        Next
        Close iFile
    End Sub
    
    Private Sub cmdCancel_Click()
        Unload Me
    End Sub
    N.B. If you don't want a User to Type into a Combo, set the Style Property to DropDown List..

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

  3. #3

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Post

    nope i wanted it to output and overwrite the old file to erase the old user list the reason it didn't work was i had forgotten to call the file at the start you know open it to be read and searched. when i was testing the code i had been typing in string and just forgot to add that one line

    ------------------
    SomeTimes Coffee Just Isn't Enough.
    Zan Magi

  4. #4

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    Post

    forgot to say it but thanks anyway i got it working though

    ------------------
    SomeTimes Coffee Just Isn't Enough.
    Zan Magi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width