Solved

File open not returning all the text

Posted on 2014-12-21
12
73 Views
Last Modified: 2014-12-22
I am saving the text typed into a textbox to a textfile, only 1 line
it saves ok but does not retun all the text
contents of text file
20 7,s bet 20 row 3

only returns
20 7
the open sub
Sub OpenSectionNameTxt()
        Dim txt As String
        Dim ff As Integer
        Dim fName As String

        ff = FreeFile

        fName = gMyPath & "\Section.txt"
         Open fName For Input As #ff
            Input #ff, txt
            Close #ff
        Debug.Print txt

                  
        End Sub

Open in new window


the save sub
Sub SaveSectionName()
    Dim sFileText As String
    Dim fName As String
     Dim fNu As Integer
     Dim txt As String
     fName = gMyPath & "\Section.txt"
     txt = txtDescription 'textbox contents
     fNu = FreeFile
        
       Open fName For Output As #fNu
            Print #fNu, txt
         Close #fNu
     
End Sub

Open in new window

How to correct this ?
0
Comment
Question by:isnoend2001
  • 7
  • 5
12 Comments
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40512393
The comma is probably being interpreted as a new line so for the input, instead of

Input #ff, txt

do this

txt  = Input$(LOF(FF), FF)
0
 

Author Closing Comment

by:isnoend2001
ID: 40512411
Excellent, fixed me right up, thanks
0
 

Author Comment

by:isnoend2001
ID: 40512466
When printed to debug
20 7,s bet 20 top row
but when added to textbox or listbox some weird symbols show.
weird symbolswhat makes this happen?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40513328
Most likely your textbox is not multiline and you have carriage-return line-feed characters in the text as a result of pressing return twice like I simulated here.
vbCrLfNote that each vbCrLf (what you get when you press 'return') is actually two characters, vbCr and vbLf, hence the 4 strange characters in the output.

If you want to get rid of them you can do

Text1.Text = Replace(YourString, vbCrLf, "")

Open in new window

0
 

Author Comment

by:isnoend2001
ID: 40513361
Thanks Marty, but i don't think that was causing the issue.
I made sure there were no vbcrlf's but the problem remained
I think the problem was in txt  = Input$(LOF(FF), FF)
Anyway i have since changed to an ini file and no more problems
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40513381
All Input$(LOF(FF), FF) says is to input the total Length Of the File (LOF), using the file number (FF) and so what you were seeing was definitely in the file, but perhaps not carriage returns. Can you reproduce and post the file that you were using previously?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:isnoend2001
ID: 40513941
My Project Too Huge
I made a small test project that shows the issue
AAA-Test-EE.zip
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40514083
This is really interesting! In 20 years of VB programming I never ran into it before, but it turns out that the Print # statement is inserting a carriage return/line feed at the end of the line. I saw it happening when I did this in the Immediate Window
?asc(mid(txt,20,1))
 13  ' Carriage Return character
?asc(mid(txt,21,1))
 10 ' Line Feed character

Open in new window


The following is code that corrects the problem (see line 43 and the comments in lines 35 to 41) along with some other changes that may help you in the future.
Option Explicit
Private gMyPath As String

Private Sub Command1_Click()
SaveSectionName
End Sub

Private Sub Command2_Click()
OpenSectionNameTxt
End Sub

Private Sub Form_Load()
' You never need to use Me and while using txtDescription = "blah" rather
' than txtDescription.Text = "blah" is OK because .Text is the default
' property, it's best to always include the .Text
'Me.txtDescription = "20 7,s bet 20 row 3"
txtDescription.Text = "20 7,s bet 20 row 3"

gMyPath = App.Path
End Sub
Sub SaveSectionName()
    Dim sFileText As String
    Dim fName As String
     Dim fNu As Integer
     ' You don't need a variable to hold the contents of
     ' the textbox. You can instead use the contents directly.
     'Dim txt As String
     
     fName = gMyPath & "\Section.txt"
     'txt = Me.txtDescription
     fNu = FreeFile
        
       Open fName For Output As #fNu
            'Print #fNu, txt
            ' NOTE: The semicolon fixes the problem!!!
            ' Please see this URL http://msdn.microsoft.com/en-us/library/office/gg264278(v=office.15).aspx
            ' which includes the following
            ' "charpos Specifies the insertion point for the next character.
            '          Use a semicolon to position the insertion point immediately after the last character
            '          displayed. ...If charpos is omitted, the next character is printed on the next line.
            '          [in other words a carriage return/ line feed is inserted]

            Print #fNu, txtDescription.Text, ;
            
         Close #fNu
     
End Sub

Sub OpenSectionNameTxt()
        'Dim txt As String
        Dim FF As Integer
        Dim fName As String
        FF = FreeFile
        fName = gMyPath & "\Section.txt"
         Open fName For Input As #FF
        ' http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_28585108.html
         'Input #ff, txt'only returns 20 7;
        ' You don't need a variable to hold the contents of
        ' the file. You can instead use the contents directly.
'         txt = Input$(LOF(FF), FF) 'fixes the issue
         txtDescription.Text = Input$(LOF(FF), FF) 'fixes the issue
           
            Close #FF
'        Debug.Print txt
        Debug.Print txtDescription.Text

     'txtDescription = txt
                  
        End Sub

Open in new window

0
 

Author Comment

by:isnoend2001
ID: 40514162
Thanks marty
The txtDescription holds the current section name of a ini file that has many sections each with its own keys and values
after running into this issue i just bailed and made another ini file to track txtDescription so far this has cured the problem
As for using the me keyword i know it is not needed, but it aids me in my poor memory
and spelling when referencing controls and variable names. also i have got into the habit of not leaving off default value names for controls eg: txtDescription = txt, sb txtDescription.text = txt,
if mbFormIsLoading then
sb
if mbFormIsLoading = true then
thanks for your help
0
 

Author Comment

by:isnoend2001
ID: 40514174
Seems i can always find unlikely problems
This had me stumped for a long time
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_28544047.html
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40514180
if mbFormIsLoading then is exactly the same as if mbFormIsLoading = true then because mbLoading is a Boolean.
0
 

Author Comment

by:isnoend2001
ID: 40514185
Yes, i know i have tried to stop writing
if mbFormIsLoading then
instead now i write:
if mbFormIsLoading = true then
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

746 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