Results 1 to 6 of 6

Thread: [RESOLVED] flat files, reading writing and amending records

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    3

    Resolved [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.

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: flat files, reading writing and amending records

    Here is a sample code:
    VB Code:
    1. 'How to write text in a given text file
    2.  
    3. Sub WriteTextFileContents(Text As String, filename As String, Optional AppendMode As Boolean)
    4.     Dim fnum As Integer, isOpen As Boolean
    5.     On Error GoTo Error_Handler
    6.     ' Get the next free file number.
    7.     fnum = FreeFile()
    8.     If AppendMode Then
    9.         Open filename For Append As #fnum
    10.     Else
    11.         Open filename For Output As #fnum
    12.     End If
    13.     ' If execution flow gets here, the file has been opened correctly.
    14.     isOpen = True
    15.     ' Print to the file in one single operation.
    16.     Print #fnum, Text
    17.     ' Intentionally flow into the error handler to close the file.
    18. Error_Handler:
    19.     ' Raise the error (if any), but first close the file.
    20.     If isOpen Then Close #fnum
    21.     If Err Then Err.Raise Err.Number, , Err.Description
    22. End Sub
    23.  
    24. Private Sub Command1_Click()
    25. Call WriteTextFileContents(Text1.Text, "C:\Temp\Testing.txt", True)
    26. End Sub
    CS

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

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

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    3

    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?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: flat files, reading writing and amending records

    Is your question how to save the contents of a Listbox to a text file?

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    3

    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.

    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
    Last edited by hitman_leon; May 8th, 2006 at 08:50 AM.

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