473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using Split Function in a Query

283 100+
Hi everyone,

Im trying to take one column (in my query) and split the data in four different columns. so far i have the first part of the information and the last part of the information to split fine. I just cant get the middle part to split right.

Here is what im trying to split.

IDDW - Livermore, CA 94550

Every record in the table is the same so i want it to split at the - then at the first , and then at the space so you would have four fields that would say

Column1.Column2....Column3....Column4
IDDW.... Livermore.......CA ..........94550

Here is what i have so far
Expand|Select|Wrap|Line Numbers
  1. Exp1: Left([Shipper Reference#],InStrRev([Shipper Reference#]," - ")-1)
  2. Exp2: Right([Shipper Reference#],Len([Shipper Reference#])-InStrRev([Shipper Reference#]," "))
I added it to to more columns and keep trying to change these in hopes to get something but im having no luck so far.

Thanks for the help.
Apr 27 '10 #1
5 21422
gershwyn
122 100+
In situations like this, I always find it helpful to write a quick function to do the work for me. That way you don't have to squeeze everything on to a single line.

Expand|Select|Wrap|Line Numbers
  1. Function SplitRef(SplitString As String, Section As Integer)
  2.   Select Case Section
  3.     Case 1:
  4.       StartPos = 1
  5.       EndPos = InStr(SplitString, "-") - 2
  6.     Case 2:
  7.       StartPos = InStr(SplitString, "-") + 2
  8.       EndPos = InStr(SplitString, ",") - 1
  9.     Case 3:
  10.       StartPos = InStr(SplitString, ",") + 2
  11.       EndPos = InStrRev(SplitString, " ") - 1
  12.     Case 4:
  13.       StartPos = InStrRev(SplitString, " ") + 1
  14.       EndPos = Len(SplitString)
  15.   End Select
  16.   SplitRef = Mid(SplitString, StartPos, EndPos - StartPos + 1)
  17. End Function
1st:
Expand|Select|Wrap|Line Numbers
  1. ?SplitRef("IDDW - Livermore, CA 94550", 1)
  2. IDDW
  3.  
2nd:
Expand|Select|Wrap|Line Numbers
  1. ?SplitRef("IDDW - Livermore, CA 94550", 2)
  2. Livermore
  3.  
3rd:
Expand|Select|Wrap|Line Numbers
  1. ?SplitRef("IDDW - Livermore, CA 94550", 3)
  2. CA
4th:
Expand|Select|Wrap|Line Numbers
  1. ?SplitRef("IDDW - Livermore, CA 94550", 4)
  2. 94550
This works with the example given, but could benefit from some added error checking.
Apr 27 '10 #2
Jim Doherty
897 Expert 512MB
In response to gershwyns excellent posting ... (if you are not too familiar with the mechanics of how to implement this function) once you have created the function in a VBA module you call it via the query grid and the SQL for the query will look like the below where the reference YourTableName= the actual name of your table.

Gershwyns post demonstrates how to create the function. You can then simply call it as you would any other inbuilt Access function.

Expand|Select|Wrap|Line Numbers
  1. SELECT SplitRef([Shipper Reference],1) AS Column1,
  2.        SplitRef([Shipper Reference],2) AS Column2,
  3.        SplitRef([Shipper Reference],3) AS Column3,
  4.        SplitRef([Shipper Reference],4) AS Column4
  5. FROM YourTableName;
Apr 28 '10 #3
slenish
283 100+
Wow really appreciate the help from both of you.

Well i got it to work but there was a debug error with this line

Expand|Select|Wrap|Line Numbers
  1.  SplitRef = Mid(SplitString, StartPos, EndPos - StartPos + 1)
  2.  
any idea why?

Thanks again
Apr 28 '10 #4
Jim Doherty
897 Expert 512MB
@slenish
Is your data consistent? my guess is that it is not. The function will not deal with all eventualities of inconsistent data appearing in your text string. The function itself is explicit for your given example pattern.

This is why error checking within the function for zero length strings nulls and other anomolies passed in is important where it does not follow the pattern you describe.

There are many purpose built string manipulation functions available LEFT,RIGHT, INSTR, MID, SPLIT and so on, but all rely on specifics and arguments in order to delimit and parse data into separate entities.

Have a look at your data first then understand the concept of the function as contributed particularly the startpos and endpos variables, These variables are examining the position of a single character within the string at a given point in your data string and adding or substracting numeric values in order to set the position. If the values do not exist in the data then the return value for the INSTR function will be 0 therefore subtracting -1 would not make sense.

'What if' scenarios that undermine your function have to be neutralised and can be done once any problems your data may pose has been considered.
Apr 28 '10 #5
NeoPa
32,556 Expert Mod 16PB
any idea why?
I suspect one of the values in that line reflects data that doesn't match the expected format.

Did you know that for most simple variables (objects can be more complicated but their default properties often show up ok) you can hover over them with the mouse when code execution is paused and the value will display in a tool-tip type bubble (See Debugging in VBA). If you examine the data you will understand exactly what's wrong (at least if not, you can tell us the values & we'll explain).
Apr 28 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: lion | last post by:
my Problem: a query string passed into a html page doesn't display correctly on a mac I am just using html and javascript (no ASP, PHP or server side scripting) This is the query string:...
3
by: Dr. Oz | last post by:
Hi, I am trying to read in a query string from one page and build a link to another page based on the query string. Here's the code I am using to read in the query string: <script...
5
by: Tom | last post by:
I've used heredocs for single SQL statements without a problem. Also, I've tried this using the SQL form on PhpMyAdmin and it works so I conclude it should work in PHP. Problem: getting syntax...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
3
by: Skip | last post by:
OK, I'm a novice in JS but have lots of coding experience. I am trying to accomplish something that would seem somewhat simple - BUT IT'S NOT. I have a basic window that calls another window...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
4
by: dougans | last post by:
Hey there, Hope someone can help me, completely stuck with immigrating from php4 to OOP based php5. == index.php -- include(database.inc.php);
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
2
by: I Hate My Computer | last post by:
I am using frames on a website. The title link on the title page adds a query string. The link goes to a page with two rows the second has two columns. I want the right column to be changed depending...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.