[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Saving Multiple Textboxes as One File?

I'm working on a program takes an address and then there is an array of 99 textboxes. I would like to be able to save the file as my own filetype and be able to open it back up to have the address and the textboxes already filled out. I am searching for a way to do that, but all I end up with is a lame CD control example that just saves txtBox1.text and the opens the file into txtBox1.text. Would I have to create an ini file? I woud like it to have an extention just for my program, so if you double click the file, my program opens.
0
Greggory
Asked:
Greggory
  • 11
  • 4
  • 2
1 Solution
 
Stephen MandersonCommented:
Are the array of text boxes saved to one text box ?
0
 
Stephen MandersonCommented:
What you could do is save the all of the text in the array to one text box and save the text box to a file.

==== Join the Text Boxes =====
Dim i as integer
Dim str as String

str = ""
For i = 0 to 99
    str = str & Text(i).text & ", "
Next

str = Left(str, Len(str)-2)
TextJoined.text = str
0
 
Stephen MandersonCommented:
And to save the text box to a file. Add a rich text box to the form named textjoined

==== Save to a file =====
 path = "Data\"
 extention = ".ccd"
 FileName = "NameOfYourFile"

     TextJoined.SaveFile path & FileName & extention 'save the file

====Open the File =====
 path = "Data\" 'set the path
 extention = ".ccd"
 FileName = "NameOFYourFile"

      TextJoined.LoadFile path & FileName & extention    
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Stephen MandersonCommented:
And you can change the extension to suit yourself. Hope this helps.

Manderson
0
 
Stephen MandersonCommented:
And make sure you have a folder named Data in the directory you are running the program. Or change the Path to suit.
0
 
GreggoryAuthor Commented:
When I open the file, is each text box going to be filled out or after I join them, are they now 1 big box?
0
 
Stephen MandersonCommented:
Yes, they will be turned into one large Textbox. And stored Into the file.
0
 
Stephen MandersonCommented:
You can of corse alter that code to save each file as the number in its array. say,

Dim i as integer
Dim str as String
 path = "Data\"
 extention = ".ccd"
 FileName = "NameOfYourFile"


For i = 0 to 99
    Text(i).SaveFile path & FileName & extention 'save the file
Next

========== Load The data ==========

For i = 0 to 99
    Text(i).LoadFile path & FileName & extention 'Load the file
Next
0
 
Stephen MandersonCommented:
O what sorry, that will give the same name for each text box.

Dim i as integer
Dim str as String
 path = "Data\"
 extention = ".ccd"
 FileName = (i)


For i = 0 to 99
    Text(i).SaveFile path & FileName & extention 'save the file
Next

========== Load The data ==========
Dim i as integer
Dim str as String
 path = "Data\"
 extention = ".ccd"
 FileName = (i)

For i = 0 to 99
    Text(i).LoadFile path & FileName & extention 'Load the file
Next
0
 
Stephen MandersonCommented:
The only what to get them to .savefile is if they are Rich Text Boxes. hope this is ok for you ?
0
 
GreggoryAuthor Commented:
Does that save all 99 textboxes in one file, or each textbox to it's own file?
0
 
Stephen MandersonCommented:
The first way joins them all and saves as one file. The second way i showed will save a file for each text box.
0
 
GreggoryAuthor Commented:
Is there a way to create one file that will load all the info back into the right boxes?
0
 
Stephen MandersonCommented:
I dont think there is a way of doing it personaly, I may be wrong.
0
 
ShauliCommented:
'assuming all textboxes are in one array from 0 to 98 (I called the textbox array Text1, modify to your needs) then:

' here is how to call the subs. Locate these lines where you want to execute, save to file, and read from file respectively.
Private Sub Command1_Click()
'to save to file
Call Save_TextBoxes("your path & file name")   '<<<<<<<modify

'to read from file
Call Read_TotxtBoxes("your path & file name")   '<<<<<<<modify
End Sub

'sub to save to file
Public Sub Save_TextBoxes(ByVal sbPathFilename As String)
Dim tbLoop As Integer

'open file
Open sbPathFilename For Output As #1
    'loop the txtboxes
    For tbLoop = 0 To Text1.Count - 1
        Print #1, tbLoop & Chr(167) & Text1(tbLoop).Text
    Next tbLoop
Close #1
End Sub

'sub to read to text boxes:
Public Sub Read_TotxtBoxes(ByVal sbPathFilename As String)
Dim mySplit() As String, txtLoop As Integer, myLine As String
On Error GoTo errHandling
'open file
Open sbPathFilename For Input As #1
    'loop to read to txtboxes
    Do Until EOF(1)
        Line Input #1, myLine
            mySplit = Split(myLine, Chr(167), -1)
                If UBound(mySplit) > 0 Then
                    Text1(txtLoop).Text = mySplit(1)
                    txtLoop = txtLoop + 1
                End If
    Loop
Close #1
Exit Sub

errHandling:
If Err.Number = 340 Then
    MsgBox "More Lines Than TextBoxes"
    Err.Clear
    Close #1
Else
    MsgBox "An error has occured"
    Err.Clear
End If
End Sub

S
0
 
GreggoryAuthor Commented:
PERFECT! Thanks
0
 
ShauliCommented:
You are welcome. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 11
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now