Solved

Displaying a string in a different format

Posted on 1998-11-23
4
154 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now