Solved

ACCESS QUERY SPLIT DATA BASED ON CRITERIA

Posted on 2014-03-20
11
2,418 Views
Last Modified: 2014-03-21
Hi,

I have a table in access with address information which the column contains a special character "|" to show where I want to split the data.  There could be from 1 to 4 of these special characters "|" or none.  I want to put each instance in it's own column.  All the help is appreciated.  Thanks!

» If the first instance of | appears to take anything to the right of this character
» If the 2nd instance of | appears to take anything to the right but if the 3rd instance appears to take anything between the 2nd instance and 3rd instance.
» If the 3rd instance of | appears to take anything to the right as well but if the 4th instance appears to take anything between the 3rd instance and 4th instance.

Below is example 1:
MC Cap Adv, LLC|Financial Life Building|4234 Brownsville Rd, Suite 129

Below is example 2:
Investment Research & Advisory|Pacific Center Plaza|80 West Peach Street|Suite 200B
0
Comment
Question by:KANEDA 0149
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 20

Expert Comment

by:GrahamMandeno
Comment Utility
You can use the Split() function.

Split([FieldName],"|")(0) will give the first section
Split([FieldName],"|")(1) will give the second section
... and so on

Graham Mandeno [Access MVP 1996-2014]
0
 

Author Comment

by:KANEDA 0149
Comment Utility
Thanks for the quick response Graham but I get the following error when I entered in the following SQL query.

SELECT A.ID, A.BILLINGSTREET, Split(A.BILLINGSTREET,"|",0) AS EXPR1

Open in new window


Error Message: "Undefined function 'Split' in expression."

Undefined function <name> in expression. (Error 3085)

You entered an SQL expression that includes a Function procedure name that cannot be recognized. Make sure the function exists, that it can be used in SQL expressions, or check the expression to make sure you entered the name correctly.
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
Create a user defined function to return the results of the Split() function.
Public Function AddrSplitter(ByVal parmText, parmPart)
    AddrSplitter = Split([parmText],"|")(parmPart)
End Function

Open in new window


You can invoke it in your query like this:
SELECT A.ID, A.BILLINGSTREET, AddrSplitter(A.BILLINGSTREET) AS EXPR1

Open in new window

0
 
LVL 20

Expert Comment

by:GrahamMandeno
Comment Utility
I'm sorry - I could have sworn I have used Split() in a SQL query before.  However, I just tried it myself and it doesn't work, but you can easily write a wrapper function in VBA:
Public Function GetElement( _
    WholeString As Variant, _
    Delimiter As String, _
    Element As Integer _
  ) As String
On Error Resume Next
GetElement = Split(WholeString, Delimiter)(Element)
End Function

Open in new window

Then, in your SQL:
SELECT A.ID, A.BILLINGSTREET, GetElement(A.BILLINGSTREET,"|",0) AS EXPR1

Open in new window


-- Graham
0
 

Author Comment

by:KANEDA 0149
Comment Utility
Thanks Graham but I am still getting the same error message but this time with "Undefined function 'GetElement' in expression."
 
» I have Access 2007
» I don't see any missing reference
» I added the Public Function by inserting new Module (see below)
Option Compare Database
Public Function GetElement( _
    WholeString As Variant, _
    Delimiter As String, _
    Element As Integer _
  ) As String
On Error Resume Next
GetElement = Split(WholeString, Delimiter)(Element)
End Function

Open in new window


» I did the same SELECT query you had and it's still won't work

It must be my Access, anyway of fixing that?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
place this codes in a regular module
Function fnParseInfo(vString As Variant, idx As Integer, Optional Delimiter As String = "|") As String

   Dim myArray() As String
   myArray = Split(vString, Delimiter)

   If idx < 0 Or idx > UBound(myArray) + 1 Then
      fnParseInfo = ""
  Else
      fnParseInfo = myArray(idx - 1)
   End If
End Function

Open in new window




to use in a query

SELECT TableAddress.Address, fnParseInfo([Address],1,"|") AS Expr1, fnParseInfo([Address],2,"|") AS Expr2, fnParseInfo([Address],3,"|") AS Expr3, fnParseInfo([Address],4,"|") AS Expr4
FROM TableAddress;
0
 
LVL 20

Accepted Solution

by:
GrahamMandeno earned 500 total points
Comment Utility
Hmmm ... something is clearly wrong with your setup.

Does the code compile?  (From the VBA window, go to Debug > Compile)

If you have compilation errors ANYWHERE in your code, it can prevent a query from calling a user-defined function.

-- Graham
0
 

Author Comment

by:KANEDA 0149
Comment Utility
Thank you all... it was definitely the USER (i.e. me)!  I can confirm all solutions worked.  Again thank you so much :)
0
 

Author Closing Comment

by:KANEDA 0149
Comment Utility
Thank you for the quick turnaround!
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
@KANEDA 0149

Did you try my code?  It posted before any of the other user-defined function comments.
0
 

Author Comment

by:KANEDA 0149
Comment Utility
Yes, that worked too.  It was me, I did not contain my field with brackets.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now