Join 137,264 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,508 people online right now. Registration is fast and FREE... Join Now!
Hi folks, 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: SERIAL,PWD1,PWD2,PWD3 352512000000000,14102953,23595170,19112674 356856000000000,15336351,19740172,29235020
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!
CODE
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 Dec, 2006 - 06:49 PM
Avoid using multidimentional arrays, if you can. Well i did't get the point whay 2 dimentional array is needed. But if you want you can implemant it.
I would use following code:
CODE
Function sisu(nimi$) As String Dim arv As Integer arv = FreeFile Open nimi$ For Input As #arv sisu = Input$(LOF(arv), arv) Close #arv End Function Function vaja(m$(), mida%, Optional nr% = 1) As String Select Case mida Case 0 vaja = m(3 + nr) & "," & m(4 + nr) & "," & m(5 + nr) & "," & "," & m(6 + nr) Case 1 vaja = m(3 + nr*4) Case 2 vaja = m(4 + nr*4) Case 3 vaja = m(5 + nr*4) Case 4 vaja = m(6 + nr*4) End Select End Function Sub main() Const KÕIK = 0, _ SERIAL = 1, _ PWD1 = 2, _ PWD2 = 3, _ PWD3 = 4
Dim tekst As String Dim pikk As Integer
tekst = sisu("D:\test.csv") kogu = Split(tekst)
vaja = tagasta(KÕIK, 2) End Sub
This post has been edited by m2s87: 22 Dec, 2006 - 08:02 AM
I wonder why you are so derogitory about people who are just learning. MAYBE the reason for a TWO DIMENTIONAL ARRAY was so that the student LEARNS how to use a multidimentional array.
I believe, a little explanation is needed here. See my tutorial Arrays Explained in Visual Basic Tutorials
I am not derogatory upon people that are learning. Being able to learn, that is something to be envied. I myself make mistakes, and some of them i do on purpose. I do not believe unproven theories or speculations, until i am proven that it is right and even then i like to leave myself freedom of doubt..
Sometimes i show or share my perspectives of the matter, and that only because i which to be tutored - understand more, learn. Being able to think, not just act, is one thing i share common with many people, being able to correct myself, if i should make a mistake, i share common with few.
What could agitate to learn? If i leave out comments, someone would need to understand the code to judge, but that makes harder to be "võhik" (someone who thinks he knows, but he does not). It might seem frustrating, but if one really wants to know (to learn), he will ask, look up, look for and he will find, if not, it proves that he really did not wished to know. And in my case i like to solve tasks, that other can not or have problems with - it's like doing crosswords (i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).
You missed my point. When i told i do not see the reason to use 2 dimensional array, i did not mean it literally. I do not like to repeat myself, nor like when others do it, but i will make an exception so simplify what i sead by that sentence: First do you know answers to following questions: 1) How are arrays stored in memory? 2) Can one array = many arrays? 3) What does simple code mean?
Now what did i do: 1) I made a string 2) String got a value from the file (I did not use line by line method) 3) i made an array from the string values (now i had a list of data, question is how to read it) 4) i made a function that would return values from array like it would have more dimensions Is this HARD? No. Is this FASTER? Yes Is this understandable without misunderstandings? No
I am not derogatory upon people that are learning. Being able to learn, that is something to be envied. I myself make mistakes, and some of them i do on purpose. I do not believe unproven theories or speculations, until i am proven that it is right and even then i like to leave myself freedom of doubt..
Sometimes i show or share my perspectives of the matter, and that only because i which to be tutored - understand more, learn. Being able to think, not just act, is one thing i share common with many people, being able to correct myself, if i should make a mistake, i share common with few.
What could agitate to learn? If i leave out comments, someone would need to understand the code to judge, but that makes harder to be "võhik" (someone who thinks he knows, but he does not). It might seem frustrating, but if one really wants to know (to learn), he will ask, look up, look for and he will find, if not, it proves that he really did not wished to know. And in my case i like to solve tasks, that other can not or have problems with - it's like doing crosswords (i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).
You missed my point. When i told i do not see the reason to use 2 dimensional array, i did not mean it literally. I do not like to repeat myself, nor like when others do it, but i will make an exception so simplify what i sead by that sentence: First do you know answers to following questions: 1) How are arrays stored in memory? 2) Can one array = many arrays? 3) What does simple code mean?
Now what did i do: 1) I made a string 2) String got a value from the file (I did not use line by line method) 3) i made an array from the string values (now i had a list of data, question is how to read it) 4) i made a function that would return values from array like it would have more dimensions Is this HARD? No. Is this FASTER? Yes Is this understandable without misunderstandings? No
Frankly you simply come across as very arrogant and condesending. (nice stab at my spelling!) As for "can I answer your questions?", I dirrect you to my tutorial on said subject.
Simple code?
When demonstrating to someone who dosen't know, applying single opperations per LINE when possible to make clear exactly which function is being accomplished at each step along the way. Simple programming often means more lines of code.
Some of us had to learn to crawl before we learned to run.
hi KeyWiz & m2s87 .. tks for the replies. I'm sorry tat my posting caused some conflicts b.w the 2 of you.
I know you 2 meant well to share your abilities/skills of VB to members.
The reason i asked for 2D array as it was a suggestion given to me. Was told tat only need to open the file once and store all the data in the file to array. Some also told me storing in array will take up alot of memory. I was also given suggestion like using OLE, which i am not familiar so i decided to not to look up OLE /VBA until i make my program working. can fine tune using OLE/VBA if i have enough time to research and implement.
(i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).
I believe that says it all. The purpose of this site is exactly the opposite of this statement.
Nope, opposite would be (i do not care if others understand how to do it). Synonym to your behavior is "Your looking, but your not seeing." or "Your can hear, but your not lessening.". And do not be affronted, I merely saying you are not paying attention what i am saying.
(i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).
I believe that says it all. The purpose of this site is exactly the opposite of this statement.
Nope, opposite would be (i do not care if others understand how to do it). Synonym to your behavior is "Your looking, but your not seeing." or "Your can hear, but your not lessening.". And do not be affronted, I merely saying you are not paying attention what i am saying.
Do you realize how moronic all this is? I'm done, goodbye.