Solved

Displaying a string in a different format

Posted on 1998-11-23
4
151 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
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Staplehead earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry about the delay in responding.  The code and advice you gave worked great. Thank you.

Rob.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now