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?
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.
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.
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.
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
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.
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.
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...
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
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
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.