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!

Strip First Character = SFC 3

Status
Not open for further replies.

sucoyant

IS-IT--Management
Sep 21, 2002
213
US
Is there a way to grab the first character in a string after each space from a textbox so I can do something else with it?

Example: Strip First Character = SFC
John Doe = JD
What Would Buddha Do = WWBD

Thanks in advance!

________________________________________
BUDDHA.gif
Buddha. Dharma. Sangha.
 
Try something like this:
myExample="Strip First Character"
mySFC=""
myArr=Split(myExample, " ")
For i=LBound(myArr) To UBound(myArr)
mySFC=mySFC & Left(myArr(i),1)
Next
MsgBox myExample & " : " & mySFC

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Ahh.. excellent! Thank you MUCH!

________________________________________
BUDDHA.gif
Buddha. Dharma. Sangha.
 
Others may find more efficient methods, but this seems to work for me:
Function SFC(strOrig As String) As String
Dim strSFCTemp As String
Dim intLoc As Integer

strSFCTemp = Left(strOrig, 1)
intLoc = InStr(1, strOrig, " ")
Do While intLoc > 0
strOrig = Mid(strOrig, intLoc + 1)
strSFCTemp = strSFCTemp & Left(strOrig, 1)
intLoc = InStr(1, strOrig, " ")
Loop
SFC = strSFCTemp

End Function

You can use this in a query:
SELECT SFC([YourField]) FROM YourTable

or in code
strYadda = SFC(rstWhatever!YourField)

Jeremy



==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
OKay... so apparently i'm completely stupid.

How do I put that into a function/sub so I can call it and get a return value.

i.e.
myString = Me.txtOne
myItem = stripChar(myString)
MsgBox myItem

Thanks for all of your help.

________________________________________
BUDDHA.gif
Buddha. Dharma. Sangha.
 
msgbox SFC(me!txtOne)

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Something like this ?
Function stripChar(strItem As String) As String
Dim myArr()
Dim myStr As String
Dim i As Integer
myArr = Split(strItem, " ")
For i = LBound(myArr) To UBound(myArr)
myStr = myStr & Left(myArr(i), 1)
Next
stripChar = myStr
End Function
myString = Me.txtOne
myItem = stripChar(myString)
MsgBox myItem

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
OKay... so maybe I'm not as dumb as I thought.

The function that I made before kept giving me an error, and now the function that was provided here is also giving me the same error:

byref.gif


It then highlights this portion of the code:
myItem = stripChar(myString)

________________________________________
BUDDHA.gif
Buddha. Dharma. Sangha.
 
Jeez.. i'm blind. I passed over JeremyNYC's post.

Works great. Much appreciated. Thanks!!

________________________________________
BUDDHA.gif
Buddha. Dharma. Sangha.
 
Alrighty... I've got a wrench to throw into the equation!

I'm using a function called "OnlyAlphaNumericChars" from in correlation with the SFC function that is provided above.

Here is my code so far:

Code:
Function SFC(strOrig As String) As String
Dim strSFCTemp As String
Dim intLoc As Integer

strSFCTemp = Left(strOrig, 1)
intLoc = InStr(1, strOrig, " ")
Do While intLoc > 0
    strOrig = Mid(strOrig, intLoc + 1)
    strSFCTemp = strSFCTemp & Left(strOrig, 1)
    intLoc = InStr(1, strOrig, " ")
Loop
SFC = UCase(strSFCTemp)

End Function
Public Function OnlyAlphaNumericChars(ByVal OrigString As String) As String
'Public Function OnlyAlphaNumericChars(OrigString As String) As String
'***********************************************************
'INPUT:  Any String
'OUTPUT: The Input String with all non-alphanumeric characters
'        removed
'EXAMPLE Debug.Print OnlyAlphaNumericChars("Hello World!")
         'output = "HelloWorld")
'***********************************************************
    Dim lLen As Long
    Dim sAns As String
    Dim lCtr As Long
    Dim sChar As String
    
    OrigString = Trim(OrigString)
    lLen = Len(OrigString)
    For lCtr = 1 To lLen
        sChar = Mid(OrigString, lCtr, 1)
        If IsAlphaNumeric(Mid(OrigString, lCtr, 1)) Then 'Sends single char to IsAlphaNumeric function for a check
            sAns = sAns & sChar
        End If
    Next
        
    OnlyAlphaNumericChars = sAns

End Function

Function IsAlphaNumeric(sChr As String) As Boolean
    IsAlphaNumeric = sChr Like "[0-9A-Za-z]"
End Function


Here is the wrench. If a user enters something in ( ), how would I go about omitting this?
i.e. This (is just a test) string! = TS

Any ideas?

You have all been so helpful! I appreciate it so much!

________________________________________
BUDDHA.gif
Buddha. Dharma. Sangha.
 
If you call SFC after OnlyAlphaNumericChars then you're in trouble.
The brackets are long gone.

You need a 'RemoveBracketedStuff' function also.

Code:
Private Function RemoveBracketedStuff(s) As String
Dim iOpenPos As Integer
Dim iClosePos As Integer
'removes bracketed text
'assumes only one open and one close bracket exist

'where is the opening bracket?
iOpenPos = InStr(s, "(")
If iOpenPos > 0 Then
    'find the closing bracket
    iClosePos = InStr(s, ")")
    If iClosePos > 0 Then
        'a close bracket exists
        'wipe the bit in the middle
        RemoveBracketedStuff = Left$(s, iOpenPos - 1) & Mid$(s, iClosePos + 1)
    Else
        'no close bracket - crop at open bracket
        RemoveBracketedStuff = Left$(s, iOpenPos - 1)
    End If
Else
    'no bracket - return all
    RemoveBracketedStuff = s
    
End If

End Function



call these in this order:

msgbox SFC(OnlyAlphaNumericChars(RemoveBracketedStuff ("hello (mad) world")))
 
JeffTullin - Thank you! You have completed my Frankenstein!

The actual syntax to make it work correctly is so:

Code:
strTest = "Hello (mad) world!"
 
strTest = RemoveBracket(strTest)
strTest = SFC(strTest)
strTest = OnlyAlphaNumericChars(strTest)


The output is "HW" which is EXACTLY what I wanted.

Thank you all SO much! I wish I could give more than just one star per person!

________________________________________
BUDDHA.gif
Buddha. Dharma. Sangha.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top