Solved

Create Text File from Lables or text objects

Posted on 2002-04-04
4
154 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 52

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

689 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