Solved

Quick Access/VBA string function needed

Posted on 2013-12-20
4
507 Views
Last Modified: 2013-12-20
Dear experts -
Hopefully a quick one - I need a function that will give me a maximum of x characters of a string fed to it, BUT not in the middle of a word (defined as ending in a space or hyphen) UNLESS there is only a single word there.
Can anyone solve this little riddle?
Thanks!
0
Comment
Question by:terpsichore
4 Comments
 
LVL 57
ID: 39731599
Your going to need to write this if I'm understanding correctly.

Some examples would be good before we continue just to make sure were on the same page.

Jim.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39731618
So, if X characters ends in the middle of a word, what do you want, everything to the left of that word, or everything including that word?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39731630
The riddle is in interpreting your question.
Does this do it?
Function GetString(strText As String, iCharCount As Integer) As String
    Dim p As Integer
    
    strText = Replace(strText, "-", " ")
    p = iCharCount
    
    Do While Mid(strText, p, 1) <> " "
        If p = 1 Then
            GetString = Left(strText, iCharCount)
            Exit Function
        Else
            p = p - 1
        End If
    Loop
    GetString = Left(strText, p)
End Function

Open in new window

0
 

Author Closing Comment

by:terpsichore
ID: 39731650
perfect. thanks so much.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

15 Experts available now in Live!

Get 1:1 Help Now