| | |
Replace txt line
![]() |
•
•
Join Date: Sep 2007
Posts: 85
Reputation:
Solved Threads: 0
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...
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,
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
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
•
•
Join Date: Sep 2007
Posts: 85
Reputation:
Solved Threads: 0
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...
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...
•
•
•
•
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...
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.
Have a problem? Don't worry just give me a call and I'll fix it for you.
•
•
Join Date: Aug 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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
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
![]() |
Similar Threads
- Sorting a File (Python)
- Problems with my bus ticket program. (Python)
- how to move to the second line in C++ .txt file reading? (C++)
- Class - Shuffle() (Python)
- python newbie help (Python)
- batch or wordlist search and replace in one file (Shell Scripting)
- Searching and Comparing strings from an XML Document (Python)
- need help with pickling please (Python)
- Passing txt files into a programand writeln info out (Pascal and Delphi)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Creating a DSN from within vb
- Next Thread: Bar code scanner
Views: 3200 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 access activex add ado append application array banned basic beginner blackjack calculator cd channel9 click code coffeehouse college component copy creative data database date desktop dissertations dissertationthesis edit error excel file filter flex form grid hardware icon ide image installer interaction key list listbox mail match measure microsoft movingranges nice noob number object open oracle outlook pause pdf pos prime print printer program programmer prompt query random range range-objects readfile reading report save search sites size sort spectateswamp sql struct sum table tags textbox time timer variable variables vb vb6 vb6.0 vba vista visual web window windows





