Results 1 to 3 of 3

Thread: Importing Text from csv file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    71

    Exclamation

    Does anyone have a block of code that they would share that includes the parsing and storing of information in a CSV file? The csv file will contain a row of comma del data. Each row indicates a new record. Any advice ideas or suggestions would be great!

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    If you are using VB 6.0, you can use the Split Function. It returns a 1-dimensional array and lets you specify the delimiter for the string.

    Code:
    Private Sub Command1_Click()
        Dim strTest As String
        Dim arrSplit() As String
        
        strTest = "one,two,three,four"
        arrSplit = Split(strTest, ",")
        MsgBox arrSplit(0) & vbCrLf & arrSplit(1) & vbCrLf & _
               arrSplit(2) & vbCrLf & arrSplit(3)
    End Sub
    Then you can pull the individual items out of the array by subscript.

  3. #3
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    If you check out the help on the INPUT file function, that should help.

    This example assumes that TESTFILE is a file with a few lines of data, each line contains a string in quotations and a number separated by a comma, for example, ("Hello, World", 234).

    Code:
    Dim MyString, MyNumber
    Open "TESTFILE" For Input As #1	' Open file for input.
    Do While Not EOF(1)	' Loop until end of file.
    	Input #1, MyString, MyNumber	' Read data into two variables.
    	Debug.Print MyString, MyNumber	' Print data to Debug window.
    Loop
    Close #1	' Close file.
    Note that strings only need enclosing in quotes if there is a comma somewhere in the string - the code would see this as a field separator otherwise!

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width