By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

Data from random access file has unwanted trailing spaces

P: 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
Share this Question
Share on Google+
8 Replies


jamesd0142
100+
P: 469
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

Expert 5K+
P: 8,434
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

Expert 5K+
P: 8,434
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

Expert 5K+
P: 8,434
... 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
100+
P: 469
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

Expert 5K+
P: 8,434
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
100+
P: 469
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

Expert 5K+
P: 8,434
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

Post your reply

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