Solved

Create Text File from Lables or text objects

Posted on 2002-04-04
4
152 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 51

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

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

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 78
Excel - Save a copy of work book 13 89
TT Auto Dashboard 13 95
Automatic Email Reminder 4 58
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

828 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