|
-
Aug 11th, 2011, 09:37 AM
#1
Thread Starter
Member
[RESOLVED] Write / Read to a specific line in text
Hello Friends,
I have a list with 40 lines which each line contains 3 data.
I want to read a specific line and get said 3 data and assign them to 3 text boxes on the form.
(LINE1)Data1,Data2,Data3
(LINE2)Data1,Data2,Data3
(LINE3)Data1,Data2,Data3
(LINE4)Data1,Data2,Data3
(LINE5)Data1,Data2,Data3
.
.
.
etc.
Case 1
Let's say. I wan to get Line4 's Data and assign them to text boxes as
Text1 = Data1, Text2 = Data2, Text3=Data3
Case2
Let's say I want to change LINE2's data in text file with text boxes value as
Data1 = Text1, Data2 = Text2, Data3 = Text3 and write it to the file.
Can someone help me ?
-
Aug 11th, 2011, 10:25 AM
#2
Re: Write / Read to a specific line in text
This may not really be an area where text files are of great use.
Here's a crude way: Create an array of strings and read up the text file and store the lines in the array. Now manipulate the contents of the array as you want. Once you are done, write the array back to the text file, overwriting any existing contents.
.
-
Aug 12th, 2011, 01:55 AM
#3
Thread Starter
Member
Re: Write / Read to a specific line in text
Dear honeybee, thanks for your suggestion. To be honest I am not so good at VB and I don't know how to do your suggestion. Before I write this message to you I searched this forum for "Data from File to Array" and I found some codes but I couldn't adopt them to my file. So, it would be really great, if you could write sample codes for me. Many thanks in advance.
-
Aug 12th, 2011, 02:44 AM
#4
Re: Write / Read to a specific line in text
-
Aug 12th, 2011, 02:52 AM
#5
Re: Write / Read to a specific line in text
Here's a sample that you can play with. Hopefully the comments will help you understand the code and logic involved.
Code:
Option Explicit
'
' Assumes the following are drawn on the Form:
' Three TextBoxes, named: Text1, Text2, Text3
'
' Four Command Buttons, named:
' cmdSelectData - when clicked will prompt user for
' a Line Number of the file to be changed
' The data from that line will be displayed
' int the TextBoxes
' cmdChange - when clicked will save the data in the TextBoxes
' back to the array of lines
' cmdSaveData - when clicked will save all the data back to
' the original File
' cmdRefresh - when clicked will re-read the file from disk
'
Private strFile As String
Private strLines() As String
Private strElements() As String
Private intLine As Integer
Private Sub GetData()
Dim intFile As Integer
'
' Obtain a free File Number
' and open the input file
'
intFile = FreeFile
Open strFile For Input As intFile
'
' Read the entire contents of the file
' close it and split into records
'
strData = Input(LOF(intFile), intFile)
Close intFile
strLines = Split(strData, vbNewLine)
'
' strLines(0) contains Line 1 of the file
' strLines(1) contains Line 2 of the file
' etc
' strLines(UBound(strLines)) contains last line of the file
'
End Sub
Private Sub cmdRefresh_Click()
Call GetData
End Sub
Private Sub cmdSaveData_Click()
'
' Save the data back to the original file
'
Dim strData As String
Dim intFile As Integer
intFile = FreeFile
Open strFile For Output As intFile
'
' Create the output from the strLines array
' and write it to the file
'
strData = Join(strLines, vbNewLine)
Print #intFile, strData;
Close intFile
End Sub
Private Sub cmdSelectData_Click()
intLine = CInt(InputBox("Which Line do you want to change?"))
strElements = Split(strLines(intLine - 1), ",")
'
' strElements(0) = data1 from the selected line
' strelements(1) = data2 from the selected line
' strElements(2) = data3 from the selected line
' Display the data in the TextBoxes
'
Text1.Text = strElements(0)
Text2.Text = strElements(1)
Text3.Text = strElements(2)
End Sub
Private Sub cmdChange_Click()
'
' Put the new data back into the strLines array
'
strLines(intLine) = Text1.Text & "," & Text2.Text & "," & Text3.Text
End Sub
Private Sub Form_Load()
'
' Get the data from the file
'
strFile = "C:\MyDir\MyFile.dat" ' The full path to, and name of your file
Call GetData
End Sub
Run the code and click on cmdSelectData; enter the line number (first line is 1) you want to change. The data from that line will appear in the TextBoxes; make any changes you want and click on cmdChange; you can then click on cmdSelectData and change another record etc.
When you've finished making all the changes, click on cmdSaveData to write the data back to the file.
If you want to see the changed data after saving it, click on cmdRefresh and go through the process again. If you click on cmdRefresh before clicking on cmdSaveData you'll loose any changes you've made.
(If you don't understand the Input, Split and Join functions then look in Help.)
-
Aug 12th, 2011, 03:16 AM
#6
Thread Starter
Member
Re: Write / Read to a specific line in text
Dear Doogle, thanks for the code. As you requested, I created 3 textboxes and 4 command buttons and renamed them as defined.
Then I changed the file path in code as
Code:
strFile = "C:\test.txt"
but when I run program it gives "Compile Error : Variable not Defined" under GetData() and showing below line.
Code:
strData = Input(LOF(intFile), intFile)
-
Aug 12th, 2011, 03:39 AM
#7
Thread Starter
Member
Re: Write / Read to a specific line in text
Ok, I found the problem.
there was miss coding and when I changed them as herebelow, program window appeared.
Code:
Open strFile For Input As intFile
strFile = Input(LOF(intFile), intFile)
Select button works. I clicked Change button then Save button but now it gives another error as "Run-time error "52": Bad file name or number" in herebelow line
Code:
Open strFile For Output As intFile
-
Aug 12th, 2011, 04:59 AM
#8
Thread Starter
Member
Re: Write / Read to a specific line in text
Annddd, I fixed said problem as well Just I replaced file path with StrFile.
Code:
strFile = "C:\test.txt"
but then I notized that program is saving changed data in text boxes to wrong line. I mean if you change Line3, it saves data to line4. And I fixed this problem with putting -1 as herebelow.
Code:
strLines((intLine - 1)) = Text1.Text & "," & Text2.Text & "," & Text3.Text
Now program is working as I wish. Many many thanks Doogle.
I am writing whole code here again, maybe someone else may need this codes in the future.
Code:
Option Explicit
'
' Assumes the following are drawn on the Form:
' Three TextBoxes, named: Text1, Text2, Text3
'
' Four Command Buttons, named:
' cmdSelectData - when clicked will prompt user for
' a Line Number of the file to be changed
' The data from that line will be displayed
' int the TextBoxes
' cmdChange - when clicked will save the data in the TextBoxes
' back to the array of lines
' cmdSaveData - when clicked will save all the data back to
' the original File
' cmdRefresh - when clicked will re-read the file from disk
'
Private strFile As String
Private strLines() As String
Private strElements() As String
Private intLine As Integer
Private Sub GetData()
Dim intFile As Integer
'
' Obtain a free File Number
' and open the input file
'
intFile = FreeFile
Open strFile For Input As intFile
'
' Read the entire contents of the file
' close it and split into records
'
strFile = Input(LOF(intFile), intFile)
Close intFile
strLines = Split(strFile, vbNewLine)
'
' strLines(0) contains Line 1 of the file
' strLines(1) contains Line 2 of the file
' etc
' strLines(UBound(strLines)) contains last line of the file
'
End Sub
Private Sub cmdRefresh_Click()
Call GetData
End Sub
Private Sub cmdSaveData_Click()
'
' Save the data back to the original file
'
Dim strData As String
Dim intFile As Integer
intFile = FreeFile
Open "c:\test.txt" For Output As intFile
'
' Create the output from the strLines array
' and write it to the file
'
strData = Join(strLines, vbNewLine)
Print #intFile, strData;
Close intFile
End Sub
Private Sub cmdSelectData_Click()
intLine = CInt(InputBox("Which Line do you want to change?"))
strElements = Split(strLines(intLine - 1), ",")
'
' strElements(0) = data1 from the selected line
' strelements(1) = data2 from the selected line
' strElements(2) = data3 from the selected line
' Display the data in the TextBoxes
'
Text1.Text = strElements(0)
Text2.Text = strElements(1)
Text3.Text = strElements(2)
End Sub
Private Sub cmdChange_Click()
'
' Put the new data back into the strLines array
'
strLines((intLine - 1)) = Text1.Text & "," & Text2.Text & "," & Text3.Text
End Sub
Private Sub Form_Load()
'
' Get the data from the file
'
strFile = "C:\test.txt" ' The full path to, and name of your file
Call GetData
End Sub
-
Aug 13th, 2011, 03:46 AM
#9
Re: Write / Read to a specific line in text
I guess I should have said that it wasn't tested - sorry 
-
Aug 13th, 2011, 04:01 AM
#10
Thread Starter
Member
Re: Write / Read to a specific line in text
Don't be sorry my friend. I already adopted your code to my program and it works perfectly. You also helped me to improve my VB6. Many thanks again.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|