Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / March 2006

Tip: Looking for answers? Try searching our database.

MS Access Query - Selecting Certain Entries In A Field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
deboraha.bishop@canadapost.ca - 17 Mar 2006 20:48 GMT
Using a MS Access query, I would like to write a formula that will give
me only the first 3 items in a field separated by semi-colons.  For
example, of the data in the field including:
horses;cats;dogs;parrots;mice;elephants,
I would like to extract:
horses;cats;dogs; only.

Any help would be appreciated.
John Vinson - 17 Mar 2006 22:54 GMT
>Using a MS Access query, I would like to write a formula that will give
>me only the first 3 items in a field separated by semi-colons.  For
[quoted text clipped - 4 lines]
>
>Any help would be appreciated.

Well, you're having problems because you're violating normal design
principles: fields should be atomic, having only one value. If you had
this information split out into a related table with one row per item
it would be easy!

That said... you'll need some moderately snarky string parsing code.
You might be able to get by with some nested InStr() and Left()
function calls, but they'll have problems with records which only have
TWO items, or just one. So try:

Public Function FirstThree(strIn As String) As String
Dim strWords() As String ' array of words
Dim iLen As Integer
Dim i As Integer
strWords = Split(strIn, ";") ' split out each word
iLen = uBound(strWords)
If iLen > 2 Then iLen = 2
For i = 0 to iLen
  FirstThree = strWords(i) & ";"
Next i
End Sub

                 John W. Vinson[MVP]    
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.