Solved

Create Text File from Lables or text objects

Posted on 2002-04-04
4
149 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 49

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now