473,387 Members | 3,684 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,387 software developers and data experts.

Help with custom use of Split()

Hello all Experts,

I've inherited a .mdb with hundreds of thousands of records the problem is that there is no structure to the tables.
The issue I'm dealing with now is the following:
The table includes 5 columns, ID, Month1, Amount1, Month2, Amount2.
The problem is that each one of the Month an Amount fields have multiple records separated by the + sign.

Note: Not all the ID's have the same number of records in each column (in fact they very rarely do).

The question is how can I split each record to a separate field?

I've tried using this query
Expand|Select|Wrap|Line Numbers
  1. SELECT MyTable.ID, MyTable.Amount1, Testsplit([Amount1],0) AS Part1, Testsplit([Amount1],1) AS Part2, Testsplit([Amount1],2) AS Part3, Testsplit([Amount1],3) AS Part4, Testsplit([Amount1],4) AS Part5, Testsplit([Amount1],5) AS Part6, Testsplit([Amount1],6) AS Part7, Testsplit([Amount1],7) AS Part8, Testsplit([Amount1],8) AS Part9, Testsplit([Amount1],9) AS Part10
  2. FROM MyTable
The Testsplit function is:
Expand|Select|Wrap|Line Numbers
  1. Public Function TestSplit(strInput As String, intNr As Integer) As String
  2.  
  3.     Dim myArray() As String
  4.  
  5.     myArray = Split(strInput, "+")
  6.     If intNr <= UBound(myArray) Then
  7.         TestSplit = myArray(intNr)
  8.     Else
  9.         TestSplit = ""
  10.     End If
  11.  
  12. End Function
  13.  
Now this usually works well, but if any records are empty (no amount for the ID) that whole line is full of #error instead of not showing anything (or "")

Your assistance is most appreciated.
Feb 7 '12 #1

✓ answered by NeoPa

Wow. You know how to impress. This is a much better question (from Custom Split Help) and a pleasure to work with :-) Normally we don't allow the same question to be asked twice, but in view of the difference between the two, and that you were following instructions to the best of your understanding, instead of merging them and ending up with a confused thread, I'll close that one and leave it just for reference and allow this one to stand.

On to the problem, which is that strInput is defined as a String. This is fine if MyTable.Amount1 is a string, but for empty records it's likely to be Null (Blanks in fields are generally stored as Null rather than empty strings, though this can be controlled in the field setup.). As such, it's better to declare the parameter as a Variant rather than a String, and then handle the possibility in line #4 as follows :
Expand|Select|Wrap|Line Numbers
  1. Public Function TestSplit(varInput As Variant, intNr As Integer) As String
  2.     Dim myArray() As String
  3.  
  4.     myArray = Split(Nz(varInput, ""), "+")
  5.     TestSplit = ""
  6.     If intNr <= UBound(myArray) Then TestSplit = myArray(intNr)
  7. End Function
Line #5 is optional as TestSplit, as a string value, is automatically initialised to an empty string ("") anyway.

Let us know if this resolves your problem :-)

3 2091
NeoPa
32,556 Expert Mod 16PB
Wow. You know how to impress. This is a much better question (from Custom Split Help) and a pleasure to work with :-) Normally we don't allow the same question to be asked twice, but in view of the difference between the two, and that you were following instructions to the best of your understanding, instead of merging them and ending up with a confused thread, I'll close that one and leave it just for reference and allow this one to stand.

On to the problem, which is that strInput is defined as a String. This is fine if MyTable.Amount1 is a string, but for empty records it's likely to be Null (Blanks in fields are generally stored as Null rather than empty strings, though this can be controlled in the field setup.). As such, it's better to declare the parameter as a Variant rather than a String, and then handle the possibility in line #4 as follows :
Expand|Select|Wrap|Line Numbers
  1. Public Function TestSplit(varInput As Variant, intNr As Integer) As String
  2.     Dim myArray() As String
  3.  
  4.     myArray = Split(Nz(varInput, ""), "+")
  5.     TestSplit = ""
  6.     If intNr <= UBound(myArray) Then TestSplit = myArray(intNr)
  7. End Function
Line #5 is optional as TestSplit, as a string value, is automatically initialised to an empty string ("") anyway.

Let us know if this resolves your problem :-)
Feb 7 '12 #2
It most definitely solves the problem. Thank you!!! :-)
Just to verify: what data type in stored in the array, and will that have any effect on writing the data to a table?
Feb 7 '12 #3
NeoPa
32,556 Expert Mod 16PB
My pleasure :-)

The array is defined on line #2 and is of type String. As such "Yes".

Depending on the setup of your table, it may (most do) require empty values to be stored as Nulls. This can be handled by not setting the value at all if the string value is empty (""), or alternatively, using an array of Variants and maintaining the Null value as passed.
Feb 7 '12 #4

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

Similar topics

5
by: Arjen | last post by:
Hi All, What I want to is using a string as PATTERN in a split function. This makes it possible for me to change the PATTERN on one place in my script... For example: $separator = ";"; $line...
2
by: Hai Xu | last post by:
Hi, I ahve a string string="ThisAAaAAteAAAAst" now, I want to split it using "AA" as separator, say @str = split(/AA/, $string); however, I want to leave any AA+ as it is, that is, AAAA and...
1
by: Jamie Bissett | last post by:
I have written an installer class that I invoke as a custom action. I have asses 2 screens in the installation process that pass data to this Installer Class. I have succesfully debugged the...
1
by: imembe | last post by:
But I am trying to split material from a text box to be entered into two different tables. Let's I enter the word "Makebelieve" in a text box. But I want to only enter the first three words...
2
by: CodeCowboy | last post by:
I'm sure some of you have done this before and I've been perusing through the forum trying to find some uncomplicated solution. I am trying to extend the existing user.identity object. I would...
1
by: Ben | last post by:
i'm having trouble getting a custom validator to fire on one of my webforms. i dragged a custom validator onto the form, left all the properties on default, double clicked it, and typed this in the...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
1
by: Nigel999 | last post by:
Hi All, strValue = "<01><ID>Ally</ID><Password>Apple3</Password></01>" i wanted to split the str above into substring. Each substring will contain only the child node. eg Substring(1) will...
17
by: Max347 | last post by:
This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers...
5
by: HY26 | last post by:
Hello - First off, I'm fairly new with using Access. I have a text field that contains a full address and I would like to split that field into 4 parts - street address, town, state, & zip. I...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.