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
Edited 3 Years Ago by pritaeas: Moved.
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() Call myTextStream.Close End If 'Assign Return Value ReadTextFileAsString = myString 'Make sure to clean up when done. Set myTextStream = Nothing Set myFSO = Nothing End Function
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 End If End While If blnFound Then txtStudentName.Text = strStudentName txtBirthData.Text = CStr(dteBirthData) txtAddress.Text = strAddress Else strMessage = "Not Found" intStyle = vbOKOnly + vbApplicationModal + vbCritical MsgBox(strMessage, intStyle, "Not Found") End If
@moodylv: You are in the wrong forum. That is VB.Net code.
I will ask a moderator to move this. For future reference, this is the correct forum: http://www.daniweb.com/software-development/vbnet/58
Take a look at this article:
In the example change:
MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",")
MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(" ") ' your file is space delimited
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.
Private Sub btn_addline_Click(ByVal ...
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...