Solved

Quick Access/VBA string function needed

Posted on 2013-12-20
4
512 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

751 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