Results 1 to 14 of 14

Thread: [RESOLVED] How to delete a row from notepad

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Resolved [RESOLVED] How to delete a row from notepad

    Hey, I'm new here and quite new to vb, so please help me out. I'm doing a project for my computing class at the moment and the due date is at the end of this month. So, to the point. I want to know the code for deleting a row from a notepad. When a user inputs their details, it is saved to a notepad.
    On the form, there's already a search option if two details match each other all of the details are shown in a listbox.
    I want a code, where if the user clicks a button, it deletes all the details of the individual user, so in other words, a row from the file. Anyone can help me?

  2. #2
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: How to delete a row from notepad

    Search the faq's on reading and writing files to get you started.



    Good Luck
    Option Explicit should not be an Option!

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    thanks for the tip, but I actually know how to open, read and delete a file. I just want to know how to delete a single line from it. Anyone know? PLease help.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to delete a row from notepad

    Quote Originally Posted by DeathBat View Post
    thanks for the tip, but I actually know how to open, read and delete a file. I just want to know how to delete a single line from it. Anyone know? PLease help.
    If the file isn't huge one way is to load the whole file into an array so each line is an element in the array, then write the array back to the file while skipping the line you don't want.


    I didn't test this but seems logical .....

    RemoveLine("MyTextFile.Txt", 1) ' remove 1st line in text file.


    Code:
    Private Sub RemoveLine(ByVal sFileName As String, ByVal LineToRemove As Long)
        
        Dim ff As Integer
        Dim i As Long
        Dim MyArray() As String
        
        ' load text file into array
        FileToArray sFileName, MyArray
        
        ' array is 0 based so subtract one
        LineToRemove = LineToRemove - 1
        
        If LineToRemove < 0 Or LineToRemove > UBound(MyArray) Then
            MsgBox "Line doesn't exsist in file."
            Exit Sub
        End If
        
        ' save data back to file,
        ff = FreeFile
        Open sFileName For Output As #ff
        For i = 0 To UBound(MyArray)
            ' if not line to remove then save it
            If i <> LineToRemove Then Print #ff, MyArray(i)
        Next i
        Close #ff
        
    End Sub
    
    Private Sub FileToArray(ByVal sPath As String, ByRef sArray() As String)
        Dim ff As Integer
        ff = FreeFile
        On Error GoTo Fini
        Open sPath For Input As #ff
        sArray = Split(Input(LOF(ff), ff), vbCrLf)
    Fini:
        Close #ff
    End Sub
    Last edited by Edgemeal; Aug 5th, 2009 at 12:06 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Hey thanks for the code! but I have a problem. Since I'm quite new, I don't know how to use the code lol. Do I just copy paste? How can I use the code, so that when the user clicks a button, it deletes the line? Your help is much appreciated.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Oh okay. Hmmm... I attached a screenshot of the form for the delete function thing. And the codes are below:

    Code:
    
    Private Sub ClearButton_Click()
    ListFlavour.Clear
    ListSize.Clear
    ListQuantity.Clear
    End Sub
    
    Private Sub DeleteButton_Click()
    
    End Sub
    
    Private Sub MainMenuButton_Click()
    Unload Me
    WelcomePage.Show
    
    End Sub
    
    Private Sub SearchButton_Click()
    If TextID.Text = "" Or TextOrder.Text = "" Then
    MsgBox "Please Fill In All Fields.", vbOKOnly + vbExclamation, "Error!"
    Else
     
    Open "C:\Users\user\Documents\My Works\AS project\OrderSheet.txt" For Input As #1
    While Not EOF(1)
    Input #1, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o
    
    If TextID.Text = b And TextOrder.Text = e Then
    match = True
    
    LabelName.Caption = a
    LabelPrice.Caption = o
    ListFlavour.Clear
    ListSize.Clear
    ListQuantity.Clear
    ListFlavour.AddItem f
    ListFlavour.AddItem i
    ListFlavour.AddItem l
    ListSize.AddItem g
    ListSize.AddItem j
    ListSize.AddItem m
    ListQuantity.AddItem h
    ListQuantity.AddItem k
    ListQuantity.AddItem n
    End If
    Wend
    Close
    If match = False Then
    MsgBox "ID Number And Order Number Does Not Match!", vbOKOnly + vbExclamation, "Error!"
    
    End If
    End If
    End Sub
    
    Private Sub TextID_KeyPress(KeyAscii As Integer)
    
    If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then
    Else
    KeyAscii = 0
    MsgBox "Please Insert Numbers Only.", vbOKOnly + vbExclamation, "Error!"
    
    End If
    End Sub
    
    
    Private Sub TextOrder_KeyPress(KeyAscii As Integer)
    If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then
    Else
    KeyAscii = 0
    MsgBox "Please Insert Numbers Only.", vbOKOnly + vbExclamation, "Error!"
    End If
    End Sub
    So what next?
    Attached Images Attached Images  

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Thanks for the code! I tried it but there seems to be one problem. It says "Compile Error: Sub Or Function not defined" and it highlights the "RemoveLine" in the delete button.

  8. #8
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: How to delete a row from notepad

    Quote Originally Posted by DeathBat View Post
    Thanks for the code! I tried it but there seems to be one problem. It says "Compile Error: Sub Or Function not defined" and it highlights the "RemoveLine" in the delete button.
    hehehe try copy and paste the code he posted at post #4.
    "More Heads are Better than One"

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Hey it actually works! Thanks for all the help...
    but one problem...
    when the file is deleted...
    and i tried searching through the ID and Order No an error comes up.
    It says "Input past end of file" and highlights:
    "Input #1, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o"

    Sorry for troubling you guys... I know I suck at this...

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to delete a row from notepad

    Quote Originally Posted by DeathBat View Post
    Hey it actually works! Thanks for all the help...
    but one problem...
    when the file is deleted...
    and i tried searching through the ID and Order No an error comes up.
    Add an error trap...

    Code:
    Private Sub SearchButton_Click()
        
        Dim LineNum As Long
        On Error GoTo ErrorHandle
        
        If TextID.Text = "" Or TextOrder.Text = "" Then
            MsgBox "Please Fill In All Fields.", vbOKOnly + vbExclamation, "Error!"
        Else
            Open "C:\Users\user\Documents\My Works\AS project\OrderSheet.txt" For Input As #1
            Do Until EOF(1)
            Input #1, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o
            ' keep track what line we're getting
            LineNum = LineNum + 1
            
            If TextID.Text = b And TextOrder.Text = e Then
                match = True
                LabelName.Caption = a
                LabelPrice.Caption = o
                ListFlavour.Clear
                ListSize.Clear
                ListQuantity.Clear
                ListFlavour.AddItem f
                ' save the line number in index 0 of ListFlavour
                ListFlavour.ItemData(0) = LineNum
                ListFlavour.AddItem i
                ListFlavour.AddItem l
                ListSize.AddItem g
                ListSize.AddItem j
                ListSize.AddItem m
                ListQuantity.AddItem h
                ListQuantity.AddItem k
                ListQuantity.AddItem n
                Exit Do ' we found the order so exit the loop
            End If
            Loop
            Close #1
            If match = False Then
                MsgBox "ID Number And Order Number Does Not Match!", vbOKOnly + vbExclamation, "Error!"
            End If
        End If
        
        ' done succesfully so exit
        Exit Sub
        
    ' error trap
    ErrorHandle:
        Close #1
        MsgBox "Error: " & Err.Description, vbCritical
    End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Hey thanks... seems to do the trick, but I keeps popping up every time I click the search button, even if the ID and Order No matches... I'm so confused

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Okay... forget about my last post...
    There seems to be a new problem...
    The code actually clears the line, but it isn't deleted, so when a new user inputs their data, there will be an empty line above the data. The problem is that when searching, the new user's data does not show up...
    A picture is attached.
    Attached Images Attached Images  

  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Hey nevermind. I managed to solve this another way.
    Thanks for the help!

  14. #14

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    Re: How to delete a row from notepad

    Hey nevermind. I managed to solve this problem using another way.
    Thanks for all your help!

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