473,387 Members | 1,844 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,387 software developers and data experts.

Data from random access file has unwanted trailing spaces

1
Hi there.

I am doing a school project and need to be able to retrieve data from a file and display it in a selectable list box. I originally used a random access file, because I need to select particular fields from a record, but the data is not displaying correctly. Because you must give a field length for each string, my data is displaying with a lot of spaces after it. For example if I display a firstname and surname, instead of "Joe Bloggs" my program is displaying "Joe____________________Bloggs" (_ means a space, of course).

Is there any way of cutting off the extra spaces when displaying a field? I had a go at using a text fle instead, which solved this problem, but then I couldn't target a specific field!

Any help is greatly appreciated!
Oct 16 '07 #1
8 2010
jamesd0142
469 256MB
To cut off the ending spaces is really easy, could you tell me what version of vb you are using.

Thanks James



Example:
This only Removes spaces from end of string!

Expand|Select|Wrap|Line Numbers
  1.       Function removeSpaces(ByVal Val)
  2.         Dim Val2 As String
  3.         Dim a As String = Val
  4.         Dim i As Integer
  5.         Dim c As String
  6.         Dim d As Integer = 0
  7.         Dim noSpaces As String
  8.         Dim b As Integer = Len(a)
  9.  
  10.         For i = 1 To b
  11.             c = Mid(a, i, 1)
  12.             If c <> " " Then
  13.                 d = d + 1
  14.             ElseIf c = " " Then
  15.                 noSpaces = Mid(a, 1, d)
  16.             End If
  17.         Next
  18.         If i = b + 1 Then
  19.             Val2 = a
  20.         End If
  21.         If Len(noSpaces) > 1 Then
  22.             Val2 = noSpaces
  23.         End If
  24.         Return Val2
  25.     End Function
  26.  
USAGE EXAMPLE:
Expand|Select|Wrap|Line Numbers
  1. TextBox2.Text = removeSpaces(TextBox1.Text)
  2.  
put a value in textbox1 and see the result in textbox2

-------------------------------------------------------------------------------------------------
If you putt "aa" in the above textbox1 then "aa" is output in textbox2.

If you putt "aa " in the above textbox1 then "aa" is output in textbox2.
spaces are removed from the end.

NOTE:
If you putt "aa aa" in the above textbox1 then "aa" is output in textbox2.
all characters after the spaces are ignored.
Oct 16 '07 #2
Killer42
8,435 Expert 8TB
To cut off the ending spaces is really easy ...
Possibly even easier than you think.

To cut spaces off the end of a string, use the Rtrim() function. To cut down multiple spaces inside a string, I'd recommend playing with the Replace() function. The problem there, of course, is that it will remove half the spaces, and leave the rest. Perhaps this function will be some help...
Expand|Select|Wrap|Line Numbers
  1. Public Function ZapDoubleSpacesIn(ByVal Src As String) As String
  2.   Dim OriginalString As String
  3.   If Src = "" Then Exit Function
  4.   Do
  5.     OriginalString = Src
  6.     Src = Replace(Src, "  ", " ")
  7.   Loop While Src <> OriginalString
  8.   ZapDoubleSpacesIn = Src
  9. End Function
Oct 17 '07 #3
Killer42
8,435 Expert 8TB
To cut off the ending spaces ...
I'm really curious about something, James. Why would you code ElseIf c = " " Then when you could simply write Else?
Oct 17 '07 #4
Killer42
8,435 Expert 8TB
... NOTE:
If you putt "aa aa" in the above textbox1 then "aa" is output in textbox2.
all characters after the spaces are ignored.
Hm... if all it does is chop the text at the first string, it seems to me we could simplify the process a lot. What about...
Expand|Select|Wrap|Line Numbers
  1. Function removeSpaces(ByVal Val As String) As String
  2.   Dim I As Long
  3.   I = Instr(Val, " ")
  4.   If I Then
  5.     Return Left$(Val, I - 1)
  6.   Else
  7.     Return Val
  8.   End If
  9. End Function
Note, I've tried to use VB.Net syntax, but since I'm a VB6 developer I may have it wrong. For one thing, we don't use Return.
Oct 17 '07 #5
jamesd0142
469 256MB
I'm really curious about something, James. Why would you code ElseIf c = " " Then when you could simply write Else?
Because im a noob, and need people like you to point out my errors :)

And...

Yes i guess this is easier than i first thought :)
Oct 17 '07 #6
Killer42
8,435 Expert 8TB
Because im a noob, and need people like you to point out my errors :)
Well, you've come to the right place! I love picking holes in everybody's code. Including my own. :)

(And don't forget, that was not an error. It was just a longer way than I would have coded it.)
Oct 17 '07 #7
jamesd0142
469 256MB
Well, you've come to the right place! I love picking holes in everybody's code. Including my own. :)

(And don't forget, that was not an error. It was just a longer way than I would have coded it.)
Yes i seem to write 50 lines of code when 10 lines will do the job, i do this often!

TUT! ;)
Oct 17 '07 #8
Killer42
8,435 Expert 8TB
Yes i seem to write 50 lines of code when 10 lines will do the job, i do this often!
I guess the important thing is to write code that works. Then comes codes that runs efficiently. Then you can worry about whether the code could be smaller. :)
Oct 17 '07 #9

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

Similar topics

1
by: eddiekwang | last post by:
Hello, when I export data from a table to a text file, I get trailing spaces if the data type in char. (This dosen't happen if the data type is varchar). I can get rid of the spaces by using the...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
7
by: ITM | last post by:
Can anyone tell me how I can prevent Access adding trailing spaces when I insert a value into a Text column? For example, if I execute the following statement: UPDATE Log SET Log.Title =...
6
by: Sam Lazarus | last post by:
I need to import data from a website, but the text is arranged badly: Name:John Doe Title:Grunt ID:314159 Name:Jane Doe Title:Queen-Bee ID:271828 etc...
5
by: Stan Shankman | last post by:
How do I add trailing spaces within a right-justified textBox? In order to add a trailing space to the text in a right-justified textBox, I use to just append a space to the end of the text...
17
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can...
13
by: WardC | last post by:
I'm a FileMaker user trying to help a client with an Access data entry problem: When text is copied from another application and pasted into an Access form, the pasted text sometimes includes...
9
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and...
5
by: brian.j.parker | last post by:
Hey all, I've noticed an obscure little quirk: it appears that if you use a login with trailing spaces on the name, SYSTEM_USER automatically trims those trailing spaces in SQL Server 2000, but not...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.