3 Replies - 3183 Views - Last Post: 21 February 2011 - 06:24 PM Rate Topic: -----

#1 tbroom93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-May 10

Loading data inputted into a Text file

Posted 18 February 2011 - 07:25 AM

Hi, I am trying to load data from a text file into a series of text boxes.

To save the data inputted into a file I used the code displayed below, I wanted it so when you save a file, the program will create a txt file named by whatever data was inputed into to 'TreeId' text box.

for example If I typed 'B101' into the 'TreeID' text box along with the other data (in the other fields) and clicked save, the program would create a new text file called 'B101'containing the data inside the textboxes. I can create individual files aslong as the key feild (TreeID) arnt the same, if they are it will overright any previously stored data (but that isnt the problem).

This Section of the program works perfectly for me, but I havent got a clue how to load the data back into the text boxes (probaly due to my lack of experience), I have tried but failed.
Any help would be much appreciated,
Thanks..

Private Sub Command1_Click()

Dim TreeID As String

        TreeID = txtTreeID & ".txt"
        
    Open TreeID For Output As #1
               
        Print #1, txtTreeID
        Print #1, txtType
        Print #1, txtCondition
        Print #1, txtDescription
        Print #1, txtLongitude
        Print #1, txtLatitude
        Print #1, txtTreeImage
        Close #1
        
        txtTreeID = ""
        txtType = ""
        txtCondition = ""
        txtDescription = ""
        txtLongitude = ""
        txtLatitude = ""
        txtImage1 = ""



Is This A Good Question/Topic? 0
  • +

Replies To: Loading data inputted into a Text file

#2 maj3091  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: Loading data inputted into a Text file

Posted 18 February 2011 - 07:31 AM

Try looking at THIS code snippet which might help you.

In regards to the files overwriting etc., have a look in the tutorials section and you might find something of use in there, or you could look at the filesystem object, or use something like the code below:

' Returns "1234.txt"  if it exists.
MyFile = Dir("C:\my folder\1234.txt")   


Was This Post Helpful? 0
  • +
  • -

#3 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Loading data inputted into a Text file

Posted 21 February 2011 - 06:24 PM

View Posttbroom93, on 18 February 2011 - 06:25 AM, said:

Hi, I am trying to load data from a text file into a series of text boxes.

To save the data inputted into a file I used the code displayed below, I wanted it so when you save a file, the program will create a txt file named by whatever data was inputed into to 'TreeId' text box.

for example If I typed 'B101' into the 'TreeID' text box along with the other data (in the other fields) and clicked save, the program would create a new text file called 'B101'containing the data inside the textboxes. I can create individual files aslong as the key feild (TreeID) arnt the same, if they are it will overright any previously stored data (but that isnt the problem).

This Section of the program works perfectly for me, but I havent got a clue how to load the data back into the text boxes (probaly due to my lack of experience), I have tried but failed.
Any help would be much appreciated,
Thanks..

Private Sub Command1_Click()

Dim TreeID As String

        TreeID = txtTreeID & ".txt"
        
    Open TreeID For Output As #1
               
        Print #1, txtTreeID
        Print #1, txtType
        Print #1, txtCondition
        Print #1, txtDescription
        Print #1, txtLongitude
        Print #1, txtLatitude
        Print #1, txtTreeImage
        Close #1
        
        txtTreeID = ""
        txtType = ""
        txtCondition = ""
        txtDescription = ""
        txtLongitude = ""
        txtLatitude = ""
        txtImage1 = ""




I would suggest that you use microsoft's FileSystemObject to do a check if a file exists and then if it doesnt then write, else do something else... To access the FileSystemObject you will need to create a referance to the object, Under the prject menu, then Referances. You are looking for: 'Microsoft Scripting Runtime' it should be the scrrun.dll.
check the box...

In your general Decl's declare a variable to represent teh FileSystemObject...
Private FS as New FileSystemObject


OR
what ever you want to use... I like FSO as it reminds me what I am doing....

The checking syntax should look like the following...
If FS.FileExisits(Path To File)[=True or =False] Then
     'Do something with the file... Leave blank to do nothing...'
Else
     'do something with the file... Leave blank to do nothing...'
end if


Depending on how you put information into the file, you can read the information back pretty easily... or not...
The way you have your code in your question is an easy way to do it, but will require some work....
You will have to open the file for input, read the information into variables, then assign the text boxes the value of the variable.
The code would look something like:
dim var1$, var2$, var3$, var4$, var5$, var6$, var7$
'since we are dealing with one piece of information per file and not  a compounded file this will be what you will want...'
If FS.FileExists(txtTreeID & ".txt")=True Then
     Open txtTreeID & ".txt" For Input as #FreeFile 'Change the freefile to your file number...'
     Line Input #FreeFile, var1$
     Line Input #FreeFile, var2$ 'and so on...'
     Close #FreeFile
Else
     'Code to process if the selected file isnt there'
Endif



There are some other ways to write the file... Which may save some room and be easier to code for...
Instead of each line getting a variable, put them all in the same line... in a delimited file... the code would look like:
Open filename for output as #FreeFile
     print #FreeFile, var1,var2,var3,var4,var5,var6,var7
close #FreeFile


And to recover the information is something like:
Open filename for input as #FreeFile
     input #FreeFile, var1, var2, var3, var4, var5, var6, var7
close #FreeFile



To assign a variable to the text boxes, is simply to swap the way that you got the variables to have the value of the text box, for example...
var1 = Text1.Text
is the way you populated the variable, To populate the text box... it would be:
Text1.Text = var1


That should get you somewhat further...

The only thing i would ask is why create a new file for every item? it seems like you could loose track of what is what... you may want to look into something like a Random Access File to store the information... Although you have to be a little more specific about what is in the file, it is basically a primitive database, and it is a whole lot cleaner than a whole bunch of sequential files... but it ie up to you...

Hope this helps... remember That although you could use this form to make you project work, you should look and see how to do it for future refinance...

Best Wishes,
Jesse Fender
Was This Post Helpful? 1
  • +
  • -

#4 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Loading data inputted into a Text file

Posted 21 February 2011 - 06:24 PM

Sorry... This was a duplicate entry...

Jesse Fender

This post has been edited by chuckjessup: 21 February 2011 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1