Solved

Create Text File from Lables or text objects

Posted on 2002-04-04
4
151 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
  • 2
4 Comments
 
LVL 50

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 75 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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