Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 Trim Program~ Pls help!

Status
Not open for further replies.

ttxxpp

Programmer
Aug 10, 2005
9
SG
I have a problem on this sentence:
Const LogFileName As String = "C:\demo.txt"
cos the file name should be selected through selection instead of hard code. How can i make it the selected directory instead of hard code the filename?


Sub RemoveLeadingTrailingSpaces()
Const LogFileName As String = "C:\demo.txt" 'Change the directory
Dim FileNum As Integer, tLine As String
Dim contents As String
Dim FileNumTemp As Integer, tempLine As String
contents = ""

FileNum = FreeFile ' next file number
FileNumTemp = FreeFile
Open LogFileName For Input Access Read Shared As FileNum
'Open LogFileName For Input As #FileNum ' open the file for reading

Do While Not EOF(FileNum)
Input #FileNum, tLine ' read a line from the text file
tLine = Trim(tLine)

'~~~~~~~~~~~~~~~~~~~~~comment on if else statement: ~~~~~~~~~~~~~~~~~~~~
'~~~~~~If the current data read is blank or is of zero length Then ~~~~~
'~~~~~~don 't append to the string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~Else ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~append to the string for output ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If IsEmpty(tLine) Or tLine = CStr(0) Or tLine = "" Or tLine = " " Or tLine = Chr(10) Then
Else
contents = contents & tLine & Chr(10)
End If

Loop ' until the last line is read
contents = Trim(contents)
Close #FileNum ' close the file

Open LogFileName For Output As FileNumTemp
Print #FileNumTemp, contents;
Close #FileNumTemp

End Sub
 
I usually say something like
Dim LogFileName As String
LogFileName ="C:\demo.txt
 
Look up the Microsoft Common Dialog Control. To use this in your project, you need to add a reference to Microsoft Common Dialog Control 6.0 from Project->Components menu.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
How can i make
LogFileName ="C:\demo.txt"
to the file directory that user selected instead of hard code it?
 


Actually, you have already been given the direction to the information which is needed for this task

Dim LogFileName As String
and
Microsoft Common Dialog Control

The next step is for you to check the mentioned control and the samples for it, located in the MSDN-VB help file, and then try to get it working.

 
I still don't quite understand it. Any sample to show me?? or is there any simple way to create a VB6 program on Textfile trimming
 


So, what is actually your question in this thread?

> cos the file name should be selected through selection instead of hard code. How can i make it the selected directory instead of hard code the filename?

Which was answered.

Or

>is there any simple way to create a VB6 program on Textfile trimming

Which was first asked in your last post.
 
I know I was confused at first with this one but it is very simple
First tick on the Microsoft commondialog control, selected from the project, components menu.
Drag this new control to anywhere your form like any other control. (it doesn't show, only the dialog it produces )

Have Dim LogFileName as string at the top of the form code.

Create a new "select file" command ButtonX
Sub CommandX_Click
CommandDialog1.fileopen' program pauses here and shows a dialog box so you can select or enter in the file
LogFileName=CommandDialog.filename
RemoveLeadingTrailingSpaces 'go and do your other sub
End Sub

Delete the Const LogFileName line in your sub

You can set default files and lots of other things with commondialog - see help
 
I encounter error on this line:

CommandDialog1.fileopen

The error is no fileopen method found. Do you know what is the problem? Thank you very much. :D
 
The method that you need would be the .ShowOpen method of the Common Dialog control.

Hope this helps



HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Ted was confused with this.

He meant to say

CommonDialog1.showopen' program pauses here and shows a dialog box so you can select or enter in the file

LogFileName=CommonDialog1.filename

[gray]Experience is something you don't get until just after you need it.[/gray]
 
I tried but is it my trimming code got problem? as i run the RemoveLeadingTrailingSpaces() however my textpad data is not trim...

What is the problem in my code??
Thanks alot!:D

Sub RemoveLeadingTrailingSpaces()

Dim FileNum As Integer, tLine As String
Dim contents As String
Dim FileNumTemp As Integer, tempLine As String
contents = ""

FileNum = FreeFile ' next file number
FileNumTemp = FreeFile
Open LogFileName For Input Access Read Shared As FileNum

Do While Not EOF(FileNum)
Input #FileNum, tLine ' read a line from the text file
tLine = Trim(tLine)

'~~~~~~~~~~~~~~~~~~~~~comment on if else statement: ~~~~~~~~~~~~~~~~~~~~
'~~~~~~If the current data read is blank or is of zero length Then ~~~~~
'~~~~~~don 't append to the string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~Else ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~append to the string for output ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If IsEmpty(tLine) Or tLine = CStr(0) Or tLine = "" Or tLine = " " Or tLine = Chr(10) Then
Else
contents = contents & tLine & Chr(10)
End If

Loop ' until the last line is read
contents = Trim(contents)
Close #FileNum ' close the file

Open LogFileName For Output As FileNumTemp
Print #FileNumTemp, contents;
Close #FileNumTemp

End Sub
 
Do you have an example of your textfile's contents?

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Here's my sample M1.log file open in textpad.

Line 59: Class MSComctlLib.ImageList of control ImageList1 was not a loaded control class.
Line 278: Class MSComctlLib.Toolbar of control Toolbar1 was not a loaded control class.


Line 363: The property name Filter in CommonDialog1 is invalid.
Line 371: The property name _ExtentX in RichTextBox1 is invalid.
Line 372: The property name _ExtentY in RichTextBox1 is invalid.


 
If this is a normal text file, use [green]Line Input[/green] instead of Input and you will get a full line w/ crlf stripped off. Then test for len(trim(tline)) = 0 or trim(tline) = "".

What are you using the final string result for? You should probably use vbNewLine instead of chr(10) to add the end of line characters.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
My File is extension .log which is not .txt

Therefore even i click trim, the data whitespaces remain the same.
Is there any solution to it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top