Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read CSV File

Status
Not open for further replies.

bebbo

Programmer
Dec 5, 2000
621
GB
Being a complete novice to VB I'm trying to open a csv file a search for a number. The code is below but it fails at the file open. Could anyone help me to open this file and search it???

Thanks

Private Sub CFLaser1_BarCodeReceived(cstrBarCode As String, ByVal nCodeType As Integer)

Dim LnCount As Integer
Dim FoundLn As Integer
Dim flds As String
Dim sFle

txtBarCode.Text = cstrBarCode
MsgBox cstrBarCode, vbOKOnly, "R K B"
File.Open "test"
LineCount = 1
FoundLine = -1
Do While Not sFle.EOF
flds = Split(sFle.lineinputstring, ",")
If flds(LBound(flds)) = cstrBarCode Then
MsgBox "RKB"
FoundLine = LineCount
Exit Do
End If
LineCount = LineCount + 1
Loop

File.Close

End Sub
 
how about

Open "c:\temp\tempfile.txt" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
'could use just input if you have the same number of fields on each line - stringone, stringtwo etc
Line Input #1, MyString ' Read data into two variables.
Debug.Print MyString ' Print full line to the Immediate window.
Loop
Close #1 ' Close file.

I hope that helps

Grant
 
Hi Beppo
You seem to be using the filesystem object without creating a reference to it, the following is what you want(cut'n'pasted from some working code):

Public Sub printoutreports(strprintfilename As String)
'strprintfilename is the path and name of file to be opened
Dim fsoPrint As New FileSystemObject ' Used for file system interactions.
Dim flRpt As File ' Object representing report file.
Dim tstrRpt As TextStream ' Textstream to read from report file.
Dim fstrReport As Integer ' File stream handle for report.
On Error GoTo ErrorHandler

Set fsoPrint = CreateObject("Scripting.FileSystemObject")
Set flRpt = fsoPrint.GetFile(strprintfilename)
Set tstrRpt = flRpt.OpenAsTextStrea(ForReading) ' open report for read.

' read the lines of text in the report.
While Not EOF(fstrReport)
strreportline = tstrRpt.ReadLine
Wend
' Close everything down.
tstrRpt.Close
Set fsoPrint = Nothing
end sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top