Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

gettting text file into a form

Posted on 1999-06-24
14
Medium Priority
?
172 Views
Last Modified: 2010-05-02
I would like to utilise a database file to insert values into a text file at predefined points and then save the file under a new filename. i have the code to run the values from the db. into the text and save the new file(s). however i need to load the text file onto a form.this has me stumped.
0
Comment
Question by:dataweaver
[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
  • 4
  • 4
  • 2
  • +4
14 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 1519517
open "file" for input as #1

while not eof(1)
 line input #1,sString
 text1.text = text1.text & sString & vbcrlf
wend

close #1
0
 
LVL 10

Expert Comment

by:smegghead
ID: 1519518
what do you mean 'load the text file onto a form' ??
0
 
LVL 18

Expert Comment

by:deighton
ID: 1519519
In my example text1 is a multiline text box.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dataweaver
ID: 1519520
i should be more explicit. previously i have used the word object to insert db.values into a text file in order to create a raft of edited documents. unfortunately microsoft inserts a whole load of its own rubbish into the files when i save them. i am now turning to VB to find a non word solution to this problem. i need to load the source file into a form text box possibly?? in order to perform the edits on it. how do i load a file onto a form and what control should i place it in??
0
 
LVL 18

Expert Comment

by:deighton
ID: 1519521
Use a RichText box - it has a load and save file method.

RTB.loadfile "text.txt",rtftext
0
 

Author Comment

by:dataweaver
ID: 1519522
i should be more explicit. previously i have used the word object to insert db.values into a text file in order to create a raft of edited documents. unfortunately microsoft inserts a whole load of its own rubbish into the files when i save them. i am now turning to VB to find a non word solution to this problem. i need to load the source file into a form text box possibly?? in order to perform the edits on it. how do i load a file onto a form and what control should i place it in??
0
 
LVL 18

Expert Comment

by:deighton
ID: 1519523
Did you get any where with the richtext box?
0
 

Author Comment

by:dataweaver
ID: 1519524
just looking for the control?
0
 

Author Comment

by:dataweaver
ID: 1519525
this is driving me slightly mad. i am runing both the vb5 learning edition and the vb6
working model. i have trawlled the progs. and the microsoft site looking for a RTF box control but no joy. am i barking up the wrong tree or what? help?

0
 
LVL 12

Expert Comment

by:mark2150
ID: 1519526
Umm, you have to *LOAD* the control in. Tools/Components (<Ctrl T>) and look for "Microsoft Rich Textbox Control".

There's an example on my web page that has a complete little editor based on a RichTextBox. It shows how to change fonts, centering, bold/ital/underline, etc.

Visit: www.cyberchute.com/rvbus/madmark

and grab the "WHAT2DO" program. Source code included no extra charge!

M

0
 
LVL 2

Expert Comment

by:jaywalk
ID: 1519527
Ok. If I was deailing with plain text files, I'd use the following:

Function GetTextFile(FileName As String) As String

    Dim msg As String
    Dim X As Long
    GetTextFile = ""
    On Error GoTo errGTFHandler

    msg = Space(FileLen(FileName))

    X = FreeFile
    Open FileName For Binary As #X
    Get #X, , msg
    Close #X
    GetTextFile = msg
Exit Function

errGTFHandler:
    If Err = 53 Then Exit Function
    MsgBox Err.Description & " [" & Err & "]"
    Exit Function
End Function

I'd have a simple TextBox (as supplied in a default exe ;), with multiline set to true, and When the time came to save any changes, I'd read the .Text property, and save that.

By the way:
To load up the trusty RichTextBox:

Look under 'Components' (right click on toolbar) for:
Microsoft Rich TextBox Control 6.0

And make sure it's got the proverbial tick next to it.
RichTextBox1.LoadFile "filename"

Note that this will NOT work with word documents.
You'll have to save them individually as RichText, or Plain Text.


0
 
LVL 18

Expert Comment

by:mdougan
ID: 1519528
Actually it sounds like you want to be working with a File opened for Random Access Write (or Read Write).  I'd assume that your "records" have a fixed length (if not, they would need to have a fixed length).  You would define a User Defined Type with all of your Fields.  

Then you would use the GET function to read a record from the file into a variable declared as your User Defined Type.  Then, using the syntax MyTypeVariable.MyFieldName you could take the data from the record and put it into a textbox on the screen.  

If you wanted to let the user update the file, then you can use the PUT function to update the record in place in the file.

The only part of your requirments that this does not work well with, is that you want to re-name the file.  In this case, you'd have to do a file-copy first, then open that for Random Access Read Write and so on...
0
 
LVL 18

Accepted Solution

by:
mdougan earned 40 total points
ID: 1519529
Type ErrorRecord
    RecID As String * 11
    ErrorID As String * 2
    ErrorMessage As String * 35
    Crlf    As String * 2
End Type

Dim Rec As ErrorRecord
Dim fErrorHandle as Long
Dim CurrRec as Long

Private Sub Form_Load()
    OpenErrorFile
End Sub

Public Sub OpenErrorFile()
Dim szFileLocal As String
Dim lString As String

    CurrRec = 1
    fErrorHandle = FreeFile
    szFileLocal = App.Path & "\MyFile.err"
    Open szFileLocal For Random Access Read Shared As fErrorHandle Len = Len(Rec)
    RecordCount = LOF(fErrorHandle) / Len(Rec)
    ClearErrorRecord Rec
    Get fErrorHandle, CurrRec, Rec
    DisplayRec Rec
End Sub

Public Sub ClearErrorRecord(Rec As ErrorRecord)
    Rec.RecID = Space(11)
    Rec.ErrorID = Space(2)
    Rec.ErrorMessage = Space(35)
    Rec.Crlf = Space(2)
End Sub

Private Sub DisplayRec(Rec As ErrorRecord)
    txtRecID.Text = Trim$(Rec.RecID)
    txtErrorID.Text = Trim$(Rec.ErrorID)
    txtErrorMessage.Text = Trim$(Rec.ErrorMessage)
End Sub

Private Sub cmdFirst_Click()
    CurrRec = 1
    ClearErrorRecord Rec
    Get fErrorHandle, CurrRec, Rec
    DisplayRec Rec
End Sub

Private Sub cmdLast_Click()
    CurrRec = RecordCount
    ClearErrorRecord Rec
    Get fErrorHandle, CurrRec, Rec
    DisplayRec Rec
End Sub

Private Sub cmdNext_Click()
    If CurrRec < RecordCount Then
        CurrRec = CurrRec + 1
        ClearErrorRecord Rec
        Get fErrorHandle, CurrRec, Rec
        DisplayRec Rec
    End If
End Sub

Private Sub cmdPrev_Click()
    If CurrRec > 1 Then
        CurrRec = CurrRec - 1
        ClearErrorRecord Rec
        Get fErrorHandle, CurrRec, Rec
        DisplayRec Rec
    End If
End Sub

0
 
LVL 4

Expert Comment

by:RichW
ID: 1519530
Try this:

Create a Textbox and place it on a form.  Create a Command Button and place it on a form.  Set the Textbox as MultiLine = True.

Private Sub Command1_Click()

Dim NewLine As String
Dim strFile
strFile = "C:\Test\test.txt"  'Path to text file

Open strFile For Input As #1

Do Until EOF(1)
    Line Input #1, NewLine
    Text1.Text = Text1.Text + NewLine + vbCrLf
Loop

Exit Sub

End Sub

You can change the variations of how the user gets the path to the text file, but this should populate the text box in VB from a text file.

Good luck.

Rich

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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

715 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