I am trying to open a csv file and load into array using VB.
The below is my code on how i search for a serial no, once found, copy PWD1 & PWD3 into 2 variables using open file, read line by line.
Format of the .csv file open up using Notepad:
I would like to make use of 2D array, open the test.csv and load data of the file to array. Then use that array for lookup.
Would like to seek help from you guys. Thank you very much!
Public Sub RetrievePWD(PWD1 As String, PWD3 As String) Dim items() As String Dim FName As String Dim Buf As String 'start with AA and BB as empty until correct serial number is found PWD1 = "" PWD3 = "" 'set path and file name FName = "D:\test.csv" 'open the file for input Open FName For Input As #1 'read lines from the file Do 'empty buffer before starting to read Buf = "" 'read in a line and if it is blank then read the next line Do Line Input #1, Buf 'trim off extra spaces Buf = Trim(Buf) 'loop should ignore empty lines but not end of file(EOF) Loop While Buf = "" And Not EOF(1) 'If we got data check to see if it is the right serial number If Buf <> "" Then 'split the buff into items array by the appropriate dilimeter items = Split(Buf, ",") If items(0) = txtUser.Text Then 'Found Serial Number PWD1 = items(1) PWD3 = items(3) 'Finished so exit Do Loop Exit Do End If 'reset array for next Split Erase items End If Loop While Not EOF(1) 'loop until end of file 'done with the file so close it Close #1 'done with items array so reset it Erase items End Sub
This post has been edited by Trazz: 21 December 2006 - 07:49 PM