[RESOLVED] flat files, reading writing and amending records
Right I can read the file and write to the file but I need to work out how I could amend an entry in the file the entries are;
fred-211085
so in the text file name and then telephone number, I use two text boxes for input so I would suppose I would be using getleft and getright to edit either entry, the name or the number but I cant work out how to do it.
Also how could I delete an entry in the flat file, both the name and the number?
Thanks for any help.
Re: flat files, reading writing and amending records
Here is a sample code:
VB Code:
'How to write text in a given text file
Sub WriteTextFileContents(Text As String, filename As String, Optional AppendMode As Boolean)
Dim fnum As Integer, isOpen As Boolean
On Error GoTo Error_Handler
' Get the next free file number.
fnum = FreeFile()
If AppendMode Then
Open filename For Append As #fnum
Else
Open filename For Output As #fnum
End If
' If execution flow gets here, the file has been opened correctly.
isOpen = True
' Print to the file in one single operation.
Print #fnum, Text
' Intentionally flow into the error handler to close the file.
Error_Handler:
' Raise the error (if any), but first close the file.
If isOpen Then Close #fnum
If Err Then Err.Raise Err.Number, , Err.Description
End Sub
Private Sub Command1_Click()
Call WriteTextFileContents(Text1.Text, "C:\Temp\Testing.txt", True)
End Sub
Re: flat files, reading writing and amending records
Quote:
Originally Posted by hitman_leon
Right I can read the file and write to the file but I need to work out how I could amend an entry in the file
You don't. You read the old file, line by line, and write the lines to a new file. You amend the individual lines before you write them.
Quote:
Also how could I delete an entry in the flat file, both the name and the number?
Same way - just don't write that entry to the new file.
When you're done, if there were no errors, delete the old file and rename the new file. If there were errors you'd probably want to delete the new file and notify the user.
Re: flat files, reading writing and amending records
Sort of worked it out, ill do somthing like this then delete the entry I want out of the file, list box resaving the file and so the changes that have been made, I was not too sure how to use the code posted above but the tips put me on the right track thanks very much.
Private Sub cmdFind_Click()
Dim length As Integer
Dim index As Integer
Dim flag As Boolean
Dim wanted As String
length = lstContact.ListCount
wanted = InputBox("Type in the contact")
flag = False
index = 0
While ((flag = False) And (index < length))
If (lstcontact.List(index) = wanted) Then
flag = True
MsgBox "contact found"
End If
index = index + 1
Wend
End Sub
so now how would I delete the one I find?
Re: flat files, reading writing and amending records
Is your question how to save the contents of a Listbox to a text file?
Re: flat files, reading writing and amending records
I got there in the end, selecting, delete, and getting info by splitting string ect all working thanks now. Dont know if I did any of it wrong or in a way that go's round the houses but it seems to do what I want it to. :wave:
Selected bits of the code doing what I needed to do.
Dim selectedIndex As Integer
Private Sub CmdPurge_Click() 'to clear the whole contacts list
listContacts.Clear 'purge all from list box
End Sub
Private Sub Cmdfindc_Click() 'search for a contact
Dim length As Integer
Dim Index As Integer
Dim flag As Boolean
length = listContacts.ListCount
wanted = findcon()
flag = False
Index = 0
While ((flag = False) And (Index < length))
If (listContacts.List(Index) = wanted) Then
flag = True
MsgBox "contact found"
End If
Index = Index + 1
Wend
End Sub
Private Sub CmdQdial_Click() 'quickdial the selected contact
txtNumber = ""
counter = 0
txtNumber = getRight(removtxt, "-") 'put the number of the searched contact
counter = 4 'in the dialing box
CmdEnd.Enabled = True
CmdClear.Enabled = False
CmdDial.Enabled = False
End Sub
Private Function getRight(ByVal numberq As String, ByVal separator As String) As String
Dim middle As Integer ' where the separator is
middle = InStr(numberq, separator) ' find position of separator in wanted
If middle = 0 Then ' separator not there
getRight = numberq
Else
getRight = Right(numberq, Len(numberq) - middle)
End If
End Function
Private Sub CmdOpenCont_Click() 'read contacts file and display contents
cmdAddCT.Enabled = True 'enable addcontact and savecontact buttons
cmdSave.Enabled = True 'can now save to file without purge of existing entries
CmdRemItem.Enabled = True
Dim line As String
Open filename For Input As #1
Line Input #1, line
listContacts.AddItem (line)
While Not EOF(1)
Line Input #1, line
listContacts.AddItem (line)
Wend
Close #1
End Sub
Dim selectedIndex As Integer
Private Sub CmdAddCT_Click() 'add contact to contact text box
addnumber = txtNumber2.Text 'adding name
addname = txtName.Text 'adding number
listContacts.AddItem (addname + "-" + addnumber) 'placing information together in line
txtNumber2 = ""
txtName = ""
End Sub
Private Sub CmdRemItem_Click()
listContacts.RemoveItem removetxt
End Sub
Private Sub cmdselecItem_Click()
selectedIndex = listContacts.ListIndex
removtxt = listContacts
End Sub