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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing text files into a vb app 1

Status
Not open for further replies.

g0ste

Technical User
Feb 16, 2003
95
0
0
AU
Is there a way i can import txt files that have been saved using an app i made in VB.

I have various text boxes, that are saved into a txt file. is there a way that i can open them up and have them show in the original boxes that they were originally input.

the txt file output is like

"Bloggs","Joe","85 Some St"

that has come from the text fields Surname, Forename, Address 1 etc.

Thanks alot!

Dan
 
Just to get you started, here is a basic way to open a file and get a line of values that you previously saved.

Code:
'local variables
Dim i As Integer
Dim intFile As Integer
Dim strFilePath As String
Dim strValues(1 To 3)      'assuming 3 fields

'get file number, path
intFile = FreeFile
strFilePath = "c:\testfile.txt"

'open file for input
Open strFilePath For Input As #intFile

'get line
Input #intFile, strValues(1), strValues(2), strValues(3)

'put in the field values
txtName.Text = strValues(1)
txtAddress.Text = strValues(2)
txtcity.Text = strValues(3)

'close file
Close #intFile

If there are more fields and textboxes, add more values. If there are more lines, just make sure you use EOF(intFileNumber) to determine if the file has ended or not while looping through each line. I would also recommend looking into FileSystemObject to save and to open files, but to each his own.

-Max
 
Dan,

Try this.

Code:
         FileNumber3 = FreeFile
         On Error Resume Next
         Open "C:\temp\list.txt" For Input As FileNumber3
         Input #FileNumber3, strNameLast, strNameFirst, strAddress
         Close FileNumber3
                 
         Me.txtNameFirst.Text = strNameFirst
         Me.txtNameLast.Text = strNameLast
         Me.txtAddress.Text = strAddress

I will assume that you already have the info in the file in this order: Last Name, First Name, Address.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Are you getting the data into vb or into a database?
what database are you you looking at?
How is your data in your text file delineated?

cheers
 
Max

Thanks for your input! I found a few problems when looking at your method, it didnt seem to like this line:

Open strFilePath For Input As #intFile

I am not sure what was going on, but if you could explain? I would like to know both methods (for future references).

Regarding HyperEngineer's method, that worked awesomely. And less code too :) Have a star!

I don't suppose you know a method to search through a directory full of text files? to search through just filenames? for example, 10 txt files named "jbloggs" "jdoe" "ajones" etc. Would make nice functionality in this program :)
 
I would look into using the FileSystemObject. A search on this forum should turn up several examples of the functionality you are after.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
for searching a directory?
 
i assume so, after searching for FSO stuff!
 
Yeah, my post was in response to the directory search portion of your question.

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
g0ste, the only reason that line did not work, is my stupidity in posting the code without checking it first. The line should not have # in it before the file number. My apologies.
-Max
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top