?
Solved

Create Text File from Lables or text objects

Posted on 2002-04-04
4
Medium Priority
?
155 Views
Last Modified: 2010-05-02
Hello,

I am just beginning to learn VB6 so in your response, please keep it simple.

I have created a few text boxes. Each box contains a command that needs to be put into a text file. This text file becomes a dat file and tells an executable what to do. So, I want to try to create these text (dat) files on the fly using the contents of my text boxes in VB. For example:

Label1: ShowDates=
Label2: Current
Label3: FromUser=
Label4: John
Label5: Start

The resulting .DAT (text) file would look something like this:

ShowDates=Current
FromUser=John
Start

So, my question is, what is the easiest way to add contents to a text file, and then create the text file?

Is this enough information?

Thanks,

John
0
Comment
Question by:jhieb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 6919715
Hi jhieb,

Example:

Private Sub Command1_Click() 'Append Content
    Open "C:\abc.dat" For Append As #1
        Print #1, Label1.Caption & "=" & Text1.Text
        Print #1, Label2.Caption & "=" & Text2.Text
        Print #1, Label3.Caption & "=" & Text3.Text
        Print #1, Label4.Caption & "=" & Text4.Text
        Print #1, Label5.Caption & "=" & Text5.Text
    Close #1
End Sub

Private Sub Command2_Click() 'Overwrite Content
    Open "C:\abc.dat" For Output As #1
        Print #1, Label1.Caption & "=" & Text1.Text
        Print #1, Label2.Caption & "=" & Text2.Text
        Print #1, Label3.Caption & "=" & Text3.Text
        Print #1, Label4.Caption & "=" & Text4.Text
        Print #1, Label5.Caption & "=" & Text5.Text
    Close #1
End Sub

Hope this help
0
 
LVL 5

Accepted Solution

by:
kcm76 earned 300 total points
ID: 6920922
The Concept of the following code is based on the Control Array.

Create Five Textboxes and Assign Same name to Text1 and Text2. This will create the Control Array. Similarly do the same for Text3 and Text4 and so on. The index of the First Textbox value is 0 and the Next one is 1.

For adding the "Start", u dont need a text box. u can hard code it.

u want to assing Name=John with First textbox having "Name" and second Textbox having "John".

Dim strCommand
strCommand = Trim(Text1.Item(0).Text) & "=" & Trim(Text1.Item(1).Text)

************Full Source Code **********
Private Sub Command1_Click()
Dim strCommand As String
Dim strCtrlName As String
strCommand = ""
FileNumber = FreeFile
Open "C:\CommandInfo.dat" For Output As #FileNumber
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is TextBox Then
            MsgBox ctrl.Index
            If ctrl.Name = strCtrlName Then
                If ctrl.Index = 0 Then
                    strCommand = ctrl.Text & strCommand
                Else
                    strCommand = strCommand & ctrl.Text
                End If
                Print #FileNumber, strCommand
            Else
                strCtrlName = ctrl.Name
                If ctrl.Index = 0 Then
                    strCommand = ctrl.Text & "="
                Else
                    strCommand = "=" & ctrl.Text
                End If
            End If
        End If
    Next ctrl
Close #FileNumber   ' Close file.
End Sub



Private Sub Form_Load()
Text1(0).Text = "Program"
Text1(1).Text = "Calc.Exe"
Text3(0).Text = "time"
Text3(1).Text = "18:00"
End Sub






0
 
LVL 5

Expert Comment

by:kcm76
ID: 6920927
*******CORRECTION********8

Before Closing the File Add
Print #FileNumber, "Start"

ALL THE BEST
KCM
0
 
LVL 1

Author Comment

by:jhieb
ID: 6921199

Thank you for the comment. This looks like it will work and it will give me enough information to work with. This question received two good responses but it appears that I can only give points to one person. So, I will give it to you since you were the first one to respond.

Thanks again.

John
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question