Solved

ACCESS QUERY SPLIT DATA BASED ON CRITERIA

Posted on 2014-03-20
11
2,721 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
ID: 39944255
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
ID: 39944279
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
ID: 39944303
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 20

Expert Comment

by:GrahamMandeno
ID: 39944304
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
ID: 39944314
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39944319
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
ID: 39944322
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
ID: 39944351
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
ID: 39944352
Thank you for the quick turnaround!
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39944511
@KANEDA 0149

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

Author Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

856 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