Solved

Displaying a string in a different format

Posted on 1998-11-23
4
168 Views
Last Modified: 2010-05-03
I need to format a string and display the results in a list box.  A couple of string examples would be:

34030089 U110-115
100-0714-001 R113-114

I need it to be displayed in a list box, in this format:

34030089 U110
34030089 U111
34030089 U112
34030089 U113
34030089 U114
34030089 U115
100-0714-001 R113
100-0714-001 R114

I have multiple strings to work with and I can't seem to be able to get the desired information accurately.
0
Comment
Question by:rround
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Staplehead earned 50 total points
ID: 1446428
rround,

the following subroutine does what you want...

keep in mind, though, that there are some assumptions:
1) there's no error checking that the input string is
   well-formed (always an alpha before the number, always
   a dash for a range of numbers, etc)

2) i fill the listbox directly from the routine.

3) i don't check that the first number in the range is
   less than or equal to the second.

let me know if you need more comments; here it is...

Public Sub ExpandNumbers(lstrSerial As String, ctlList As ListBox)

Dim Finished As Boolean
Dim iDashPosition As Integer
Dim iNumberPosition As Integer
Dim sTemp As String

Dim i As Integer
Dim sPrefix As String

Dim iLowBound As Integer
Dim iHighBound As Integer

'initialize temp variables
Finished = False
iDashPosition = Len(lstrSerial)

'search for the right-most dash character
While Not Finished
    If Mid(lstrSerial, iDashPosition, 1) = "-" Then
        Finished = True
    Else
        iDashPosition = iDashPosition - 1
    End If
Wend

'find the position of the letter which precedes
'the number (e.g., "34030089 U110-115")
iNumberPosition = iDashPosition - 1
Finished = False
While Not Finished
    sTemp = Mid(lstrSerial, iNumberPosition, 1)
    If sTemp < "0" Or sTemp > "9" Then
        Finished = True
    Else
        iNumberPosition = iNumberPosition - 1
    End If
Wend

iLowBound = CInt(Mid(lstrSerial, iNumberPosition + 1, iDashPosition - iNumberPosition - 1))
iHighBound = CInt(Mid(lstrSerial, iDashPosition + 1, Len(lstrSerial) - iDashPosition))

'set up the strings' prefixes...
sPrefix = Left(lstrSerial, iNumberPosition)

For i = iLowBound To iHighBound
    sTemp = sPrefix & CStr(i)
    ctlList.AddItem (sTemp)
Next i

End Sub
0
 

Author Comment

by:rround
ID: 1446429
This works great, however, I also need the code to detect this type of string:

102-0618-001 R12-18,R126-131,R140

and have it display in the same format as the other strings.


0
 
LVL 1

Expert Comment

by:Staplehead
ID: 1446430
you can add one of two things to the code:
1) some parsing that looks for comma-delimited ranges, then passes each separately to
       the ExpandNumbers routine,
or
2) code in ExpandNumbers itself, which looks for dash *or* comma, treating each separately.
       the problem here is, how do you know when a dash is in the prefix or in a range?
       you'd be stuck assuming that the alpha that precedes will *always* be there...

Larry
0
 

Author Comment

by:rround
ID: 1446431
Sorry about the delay in responding.  The code and advice you gave worked great. Thank you.

Rob.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

688 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