Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Capitals within a string

Status
Not open for further replies.

PaulSc

MIS
Aug 21, 2000
148
GB
Hi,
Can anybody help me out please.

I have a number of strings in the format CllllCllllll (C=Caps,l-Lower) and I want to split them into 2 fields (Cllll Cllllll etc).

I believe I can do this by using the INSTR function but I'm having problems identifying what is a capital letter. It works if I specify a specific letter but basically I want to be able to pick up on any letter with an Ascii code between 60 & 90 and use that as the split criteria.

Any ideas as to how I might go about coding this?
Thanks in advance for any help..
Regards
PaulSc
 
This example uses a textbox (txtData) on a form where I enter data. Then I click a command button to test for the results. It might start you in the proper direction.

Private Sub cndCheck_Click()
Dim i As Integer
Dim strI As String
For i = 2 To Len(txtData)
strI = Mid(txtData, i, 1)
Select Case Asc(strI)
Case 65 To 90
MsgBox "Capital Letter is " & strI & vbCrLf & _
"First part is " & Left(txtData, i - 1) & " and 2nd part is " _
& Mid(txtData, i)
Exit For
Case Else
'do nothing
End Select
Next i


End Sub
 
A minor variation, which 'hides' those nasty ASCII codes (and might work with the double byte characters we are going to be using "any time real soon now"):

Code:
Public Function basFindUCase(strIn As String) As Integer

    'Returns the position of the first Uppercase character
    'In the input

    Dim Idx As Integer
    Dim MyChr As String * 1

    For Idx = 1 To Len(strIn)
        MyChr = Mid(strIn, Idx, 1)
        If (Asc(UCase(MyChr)) = Asc(MyChr)) Then
            basFindUCase = Idx
            Exit For
        End If
    Next Idx

End Function

of course, this only returns the first occurance of an Upper case character, but you could either adapt the concept or call it repetitively with the "remainder" of the string.

Then, the following will (if used properly) insert a "delimiter" into the string, so you could parse on the inserted "character".

Code:
Public Function basInsertChar(strIn As String, ChrIn As String) As String

    'To insert "ChrIn" Preceeding each Uppercase character found
    'NOTE: ChrIn may be any string.  This routine does NOT
    'restrict the insertion to a single character!!!

    Dim Idx As Integer
    Dim MyChr As String * 1
    Dim tmpStr As String

    For Idx = 1 To Len(strIn)
        MyChr = Mid(strIn, Idx, 1)
        If (Asc(UCase(MyChr)) = Asc(MyChr)) Then
            tmpStr = tmpStr & ChrIn
        End If
        tmpStr = tmpStr & MyChr
    Next Idx

    basInsertChar = tmpStr

End Function

I have previously posted a routine "basSplit" in the FAQ for Ms. Access General discussion. Use the preceeding to insert a space (" ") in your original string, then send that result to basSplilt, and you would have the seperated elements in an array which would be available for placing in your "fields".



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Cheers Guys, this is exactly what I needed.
Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top