Replace txt line

Reply

Join Date: Sep 2007
Posts: 85
Reputation: Dell XPS is an unknown quantity at this point 
Solved Threads: 0
Dell XPS Dell XPS is offline Offline
Junior Poster in Training

Replace txt line

 
0
  #1
Jan 3rd, 2008
I have made a program that you can add your contacts but i need to get a way to edit the persons details....
The details are kept in a txt file(because i don't know how to make this using a db)
I have a list box showing the name and surname of the contact and when you select anyone their details are shown in textboxes ready to be changed but I need the code for replacing or deleting a specific line from txt...


I have searched for this but not found any help... if another thread already exists for this topic sorry, and please post link... thank you...
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 332
Reputation: DenisOxon is an unknown quantity at this point 
Solved Threads: 17
DenisOxon's Avatar
DenisOxon DenisOxon is offline Offline
Posting Whiz

Re: Replace txt line

 
0
  #2
Jan 3rd, 2008
Hi,

To solve this you need to go back to the early days of programming where data was held in flat files or text files.

For example if your text file has 21 records in, one for each contact, and you are editing/deleting record 12. When you have finished editing or wish to delete record 12 then these are steps.

1) open data file in read mode
2) open new temp file in write mode
3) copy 11 record form data file to temp file.
4) if editing write the edited record, if deleting go to step 5)
5) read record 12 and do nothing with it
6) copy records 13 to 21 form data file to temp file.
7) close files
8) make a backup copy of data file.
9) copy temp file back over data file.

Problem sorted.

Any questions please come back. Also please let us know how you get on.

Denis
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 85
Reputation: Dell XPS is an unknown quantity at this point 
Solved Threads: 0
Dell XPS Dell XPS is offline Offline
Junior Poster in Training

Re: Replace txt line

 
0
  #3
Jan 3rd, 2008
thank you for the quick response... I have understood well the step but because it is the first time i am doing this i need some help with copying entries to remain same... edit the entry and the copy the remaining entries and the replace the original txt....

Can you please explain the steps you gave me in vb coding...
Thank you for the help...
as soon as i finish it i will let you know the results...
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 332
Reputation: DenisOxon is an unknown quantity at this point 
Solved Threads: 17
DenisOxon's Avatar
DenisOxon DenisOxon is offline Offline
Posting Whiz

Re: Replace txt line

 
0
  #4
Jan 4th, 2008
Here is some code to copy 12 lines across, hopefully will give the idea.

Denis



Dim linne As String
Dim j As Integer

Open "C:\TEST\Infile.txt" For Input As #1
Open "C:\TEST\outfile.txt" For Output As #2

For j = 1 To 12

Line Input #1, linne
Print #2, linne


Next j

Close #1
Close #2
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 541
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Replace txt line

 
0
  #5
Jan 5th, 2008
Originally Posted by Dell XPS View Post
I have made a program that you can add your contacts but i need to get a way to edit the persons details....
The details are kept in a txt file(because i don't know how to make this using a db)
I have a list box showing the name and surname of the contact and when you select anyone their details are shown in textboxes ready to be changed but I need the code for replacing or deleting a specific line from txt...

I have searched for this but not found any help... if another thread already exists for this topic sorry, and please post link... thank you...
Hi Dell XPS.
Check out this sample code. just take two textboxes(text1,text2), three buttons(command1-3) and a listbox(list1) for testing :-

Option Explicit

Dim i As Integer

Private Sub Command1_Click()        ''for adding new record into the textfile
If Text1.Text <> "" And Text2.Text <> "" Then
    List1.AddItem Text1.Text & " " & Text2.Text
    Call UpdateRecords
    Call LoadRecords
Else
    MsgBox "Input details."
    Text1.SetFocus
End If
End Sub

Public Sub LoadRecords()    ''reads the file and fetches all existing records
Dim str As String

If Dir(App.Path & "\info.txt") <> "" Then
    List1.Clear
    Open App.Path & "\info.txt" For Input As #1
        Do While Not EOF(1)
            Input #1, str
            List1.AddItem str
        Loop
    Close #1
Else
    MsgBox "Record file is missing."
End If
End Sub

Private Sub Command2_Click()        ''for editing the selected record's info
If Text1.Text <> "" And Text2.Text <> "" Then
    List1.RemoveItem i
    List1.AddItem Text1.Text & " " & Text2.Text, i
    Call UpdateRecords
Else
    MsgBox "Input details."
    Text1.SetFocus
End If
End Sub

Private Sub Command3_Click()        ''for deleting the selected record from the file
Dim confirm As Integer

If List1.ListIndex <> -1 Then
    confirm = MsgBox("Delete this record?", vbYesNo)
    If confirm = vbNo Then
        Exit Sub
    End If
    List1.RemoveItem List1.ListIndex
    Text1.Text = ""
    Text2.Text = ""
    Call UpdateRecords
Else
    MsgBox "Select the record from the list you wish to delete."
End If
End Sub

Private Sub Form_Load()
Call LoadRecords
End Sub

Private Sub List1_Click()       ''for displaying information of the selected record
Text1.Text = Mid(List1.Text, 1, InStr(1, List1.Text, " ") - 1)
Text2.Text = Mid(List1.Text, InStr(1, List1.Text, " ") + 1)
i = List1.ListIndex
End Sub

Public Sub UpdateRecords()      ''for updating the text file after edit and delete operations
Dim j As Integer

Open App.Path & "\info.txt" For Output As #1
    For j = 0 To List1.ListCount - 1 Step 1
        Print #1, List1.List(j)
    Next j
Close #1
End Sub

hope you will get your answer completely from this coding.
know me if this helps you.

good luck

regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: varghaimani is an unknown quantity at this point 
Solved Threads: 0
varghaimani varghaimani is offline Offline
Newbie Poster

Re: Replace txt line

 
0
  #6
Aug 10th, 2008
Originally Posted by choudhuryshouvi View Post
Hi Dell XPS.
Check out this sample code. just take two textboxes(text1,text2), three buttons(command1-3) and a listbox(list1) for testing :-

Option Explicit

Dim i As Integer

Private Sub Command1_Click()        ''for adding new record into the textfile
If Text1.Text <> "" And Text2.Text <> "" Then
    List1.AddItem Text1.Text & " " & Text2.Text
    Call UpdateRecords
    Call LoadRecords
Else
    MsgBox "Input details."
    Text1.SetFocus
End If
End Sub

Public Sub LoadRecords()    ''reads the file and fetches all existing records
Dim str As String

If Dir(App.Path & "\info.txt") <> "" Then
    List1.Clear
    Open App.Path & "\info.txt" For Input As #1
        Do While Not EOF(1)
            Input #1, str
            List1.AddItem str
        Loop
    Close #1
Else
    MsgBox "Record file is missing."
End If
End Sub

Private Sub Command2_Click()        ''for editing the selected record's info
If Text1.Text <> "" And Text2.Text <> "" Then
    List1.RemoveItem i
    List1.AddItem Text1.Text & " " & Text2.Text, i
    Call UpdateRecords
Else
    MsgBox "Input details."
    Text1.SetFocus
End If
End Sub

Private Sub Command3_Click()        ''for deleting the selected record from the file
Dim confirm As Integer

If List1.ListIndex <> -1 Then
    confirm = MsgBox("Delete this record?", vbYesNo)
    If confirm = vbNo Then
        Exit Sub
    End If
    List1.RemoveItem List1.ListIndex
    Text1.Text = ""
    Text2.Text = ""
    Call UpdateRecords
Else
    MsgBox "Select the record from the list you wish to delete."
End If
End Sub

Private Sub Form_Load()
Call LoadRecords
End Sub

Private Sub List1_Click()       ''for displaying information of the selected record
Text1.Text = Mid(List1.Text, 1, InStr(1, List1.Text, " ") - 1)
Text2.Text = Mid(List1.Text, InStr(1, List1.Text, " ") + 1)
i = List1.ListIndex
End Sub

Public Sub UpdateRecords()      ''for updating the text file after edit and delete operations
Dim j As Integer

Open App.Path & "\info.txt" For Output As #1
    For j = 0 To List1.ListCount - 1 Step 1
        Print #1, List1.List(j)
    Next j
Close #1
End Sub

hope you will get your answer completely from this coding.
know me if this helps you.

good luck

regards
Shouvik
Thank you so much very good help with the examples
but i have a question
i have 3 textboxes so how can i fix this code to show 3 boxes of information

Private Sub List1_Click() ''for displaying information of the selected record
Text1.Text = Mid(List1.Text, 1, InStr(1, List1.Text, " ") - 1)
Text2.Text = Mid(List1.Text, InStr(1, List1.Text, " ") + 1)
i = List1.ListIndex
End Sub


thank you
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3200 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC