Right guys, I'm really new to this but I'm getting along ok, but I've hit a block.
I need to be able to know how to search a text file in VB for a particular line and then display it.
the files I'm trying to use are set up as: Name, number1, number3, number4, number5, number6, number7
methane 50.8 65.3 77.2 0.236 12.0 258.5
So I need to know how to search the file for the Name and then display the rest of that line and save it in array
I can read a file from beginning to end but this I am clueless about, thanks
It's for a system to add, display, update and remove workers
and many thanks in advance
Use the FileSystemObject that is part of the Microsoft Scripting Runtime (scrrun.dll)
You can use something like -
Public Function ReadTextFileAsString(IN_sFilePath As String) As String
Dim myFSO As Scripting.FileSystemObject
Dim myTextStream As Scripting.TextStream
Dim myString As String
'Create a new FileSystemObject
Set myFSO = New Scripting.FileSystemObject
'Make sure file exists:
If myFSO.FileExists(IN_sFilePath) Then
Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
myString = myTextStream.ReadAll()
'Assign Return Value
ReadTextFileAsString = myString
'Make sure to clean up when done.
Set myTextStream = Nothing
Set myFSO = Nothing
Also have a look at this link with an attached sample...
but i used the code you wrote lit of error like Type 'Scripting.FileSystemObject' is not defined.
i wrote a code and works fine but always show nothing found!!
so may you please see this code and correct it
Dim A As String = "C:\Users\text.txt"
Dim objReader As New System.IO.StreamReader(A)
Dim strStudentName As String
Dim dteBirthData As String
Dim strAddress As String
Dim strSearch As String
Dim blnFound As Boolean
Dim strMessage As String
Dim intStyle As MsgBoxStyle
strSearch = InputBox("Enter a name to find", "find", , , )
blnFound = False
While blnFound = False And objReader.Peek() <> -1
strStudentName = strStudentName & objReader.Read() & vbNewLine
If strStudentName = strSearch Then
dteBirthData = dteBirthData & objReader.Read()
strAddress = strAddress & objReader.Read()
blnFound = True
If blnFound Then
txtStudentName.Text = strStudentName
txtBirthData.Text = CStr(dteBirthData)
txtAddress.Text = strAddress
strMessage = "Not Found"
intStyle = vbOKOnly + vbApplicationModal + vbCritical
MsgBox(strMessage, intStyle, "Not Found")