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
Solved

Displaying a string in a different format

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 65
VBA: Select SQL query based on a config Sheet v2 11 49
Macro Excel - Multiple If conditions 2 81
Publisher:   Unknown     VB.exe Application 1 28
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

829 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