473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Removing a Carriage Return

I have a text file with x number of lines. When opening the file, I have an extra blank line which I presume is a carriage return,

How do I
1. ensure I don't have a blank line when I write to the file or
2. get rid of the extra line afterwards

Here's the code I've used for the data write: -

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.  
  3.     Dim rst As DAO.Recordset
  4.  
  5.     Set rst = CurrentDb.OpenRecordset("Table2", dbOpenDynaset)
  6.  
  7.     DoCmd.TransferText acExportDelim, "", "Table1", "D:\Table1.txt", False
  8.     DoCmd.TransferText acExportDelim, "", "Table2", "D:\Table2.txt", False
  9.  
  10.     Open "D:\Table1.txt" For Append As #1
  11.     Open "D:\Table2.txt" For Input As #2
  12.  
  13.     Do While Not rst.EOF
  14.         Print #1, rst!FirstName
  15.         rst.MoveNext
  16.     Loop
  17.  
  18.     Close #1
  19.     Close #2
  20.  
  21.     Kill ("D:\Table2.txt")
  22.  
  23. End Sub
Jun 25 '07 #1
6 3762
ADezii
8,834 Expert 8TB
I have a text file with x number of lines. When opening the file, I have an extra blank line which I presume is a carriage return,

How do I
1. ensure I don't have a blank line when I write to the file or
2. get rid of the extra line afterwards

Here's the code I've used for the data write: -

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.  
  3.     Dim rst As DAO.Recordset
  4.  
  5.     Set rst = CurrentDb.OpenRecordset("Table2", dbOpenDynaset)
  6.  
  7.     DoCmd.TransferText acExportDelim, "", "Table1", "D:\Table1.txt", False
  8.     DoCmd.TransferText acExportDelim, "", "Table2", "D:\Table2.txt", False
  9.  
  10.     Open "D:\Table1.txt" For Append As #1
  11.     Open "D:\Table2.txt" For Input As #2
  12.  
  13.     Do While Not rst.EOF
  14.         Print #1, rst!FirstName
  15.         rst.MoveNext
  16.     Loop
  17.  
  18.     Close #1
  19.     Close #2
  20.  
  21.     Kill ("D:\Table2.txt")
  22.  
  23. End Sub
Typically a line containing a Carriage Return only will have a 0 length. Test for this condition before you write to the file. Modify the code within your Loop as follows, and let me know how you make out:
Expand|Select|Wrap|Line Numbers
  1. Do While Not rst.EOF
  2.   If  Len(rst!FirstName) > 0 Then
  3.     Print #1, rst!FirstName
  4.       rst.MoveNext
  5.   End If
Loop
Jun 25 '07 #2
Tried that, but made no difference. I don't think the Len function includes the vbCrLf in it's length evaluation.
Jun 26 '07 #3
ADezii
8,834 Expert 8TB
Tried that, but made no difference. I don't think the Len function includes the vbCrLf in it's length evaluation.
Try this and let me know again:
Expand|Select|Wrap|Line Numbers
  1. Do While Not rst.EOF
  2.   If Instr(rst![FirstName], vbCrLf) = 0 Then      'If Carriage Return not in Line
  3.     Print #1, rst!FirstName
  4.   End If
  5.   rst.MoveNext
  6. Loop
Jun 26 '07 #4
I will try that tomorrow. Just a point here, in that there will be a carriage return for each record. It's just the LAST carriage return that I'm trying to get rid of. I'll try your suggestion and see how it goes.
Jun 26 '07 #5
It didn't work, unfortunately.
Jun 27 '07 #6
ADezii
8,834 Expert 8TB
It didn't work, unfortunately.
Give this a try. The Debug.Print statement will generate a Run Time Error when it comes to the Line with the last Carriage Return. It will go to the Handler which will close the File and the Recordset. The previous lines would have already been written to the Recordset. Let me know how you make out.
Expand|Select|Wrap|Line Numbers
  1. Dim strLine As String, MyDB As DAO.Database, MyRS As DAO.Recordset
  2.  
  3. On Error GoTo Err_Click
  4. Set MyDB = CurrentDb()
  5. Set MyRS = MyDB.OpenRecordset("tblName", dbOpenDynaset)
  6.  
  7.  
  8. Open "C:\Dell\Test.txt" For Input As #1
  9.  
  10. Do While Not EOF(1)
  11.   Line Input #1, strLine
  12.     Debug.Print Left$(strLine, Len(strLine) - 1)
  13.   MyRS.AddNew
  14.     MyRS![TheName] = strLine
  15.   MyRS.Update
  16. Loop
  17.  
  18. Close #1
  19. MyRS.Close
  20.  
  21. Err_Click_Exit:
  22.   Exit Sub
  23.  
  24. Err_Click:
  25.   Close #1
  26.   MyRS.Close
  27.    Resume Err_Click_Exit:
Jun 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Canes_Rock | last post by:
The information posted at: ...
4
by: Josh | last post by:
Hi, I'm using System.Data.DataSet.ReadXml to convert some xml from a webservice to a DataSet. The xml looks like: <?xml version="1.0"...
2
by: John Dalberg | last post by:
Hi What's the regex to remove the carriage return/line field from a string? These can occur multiple times in the string as in xxx\r\n\r\n. -- John Dalberg
4
by: Andyza | last post by:
I'm using FileSystemObject to open and write to a tab delimited text file. First, I connect to a database and select some data. Then I create the text file and insert each record in the text...
3
by: Smythe32 | last post by:
Hi All, I am trying to remove carriage returns from a text field via some code. Here is an example of what I am up against. The field streetname contains the little square box that I believe...
6
by: shajias | last post by:
Hi, I am having a xml code like this <name> I am having a carriage return here. Second line with carriage return. This is the last line. </name>
2
by: polohero | last post by:
I am using an FtpClient.cs that someone else created and when I'm uploading (it uses a Socket) a file a carriage return is getting deleted when the file appears on the ftp location. Normally this...
4
by: LOCAFO | last post by:
I am using vb.net and have a long string of text. When I use vbnewline or vbCrLf, for a carraige retun, I get a black square box in the SQL database table. I need the carriage return but I don't...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.