473,396 Members | 1,871 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,396 software developers and data experts.

Expand Range of Numbers with SQL in Access

Hello.

So, I am a logistics engineer and I am trying to help my pricing manager build a pricing application tool that will help eliminate her time spent filling in huge excel files with information about pricing bids. I have successfully build an Access form that fills in the areas she wanted filled in but I come across a new problem now:

Every once in a while she will receive an RFP (Request for Proposal) which has a cluster of zipcodes. For example:

Origin City Dest St Destination Zip Range
Bloomington AZ 850-865
Bloomington CA-Central 929-948, 950-953, 956-958

Now to make her bids, she has to manually create rows for each of the numbers in the range. Say for the 850-865 range, she has to make rows for 850, 851, 852, ... 865.

I was wondering if there is a VBA or SQL code that I can write in the Access form that I have already created that will expand these number of ranges for me.

SIDE NOTE: For that second range of zip codes (929-948, 950-953, 956-958) how would you compile the code so that it expands all the ranges after the comma?

If you can help me with this you'd be an absolute life saver!!

The name of my table with this information is tblTemplate.

Thank you all!!
Sep 7 '18 #1
7 1561
NeoPa
32,556 Expert Mod 16PB
This question covers two distinct topics, so let's forget writing out the new records to a table for now. You can post that later once you're happy with the technique of producing the individual numbers in an array or string. FYI we only allow one topic per thread as they become close to useless otherwise, for those searching for solutions to their problems.

Let's start then, with a function procedure that returns a string of all the numbers from a string that can include multiple ranges of numbers where a range is indicated by a '-' and items are separated by a ','.
Expand|Select|Wrap|Line Numbers
  1. Public Function AllNums(strNums As String) As String
  2.     Dim lngX As Long
  3.     Dim astrRange() As String
  4.     Dim varString As Variant
  5.  
  6.     For Each varString In strNums
  7.         astrRange = Split(CStr(varString), "-")
  8.         If UBound(astrRange) > 0 Then
  9.             For lngX = CLng(astrRange(0)) To CLng(astrRange(1))
  10.                 AllNums = AllNums & "," & lngX
  11.             Next lngX
  12.         Else
  13.             AllNums = AllNums & "," & astrRange(0)
  14.         End If
  15.     Next varString
  16.     If Len(AllNums) > 0 Then AllNums = Replace(Mid(AllNums, 2), ' ', '')
  17. End Function
Sep 7 '18 #2
Thank you so much for your response!
However I am a bit confused, where am I entering this code and what exactly will it do for me? Sorry for the confusion!
Sep 7 '18 #3
zmbd
5,501 Expert Mod 4TB
nabusafe:
Neopa's code is supposed to take the string input
Finds the numbers
Splits them out
Find the lowest and highest
Creates the values between these two
Returns a comma delimited string.
AllNums("929-948")
929,930,931,...948

NeoPa,
Compile error:
Line6: strNums is not a collection object nor an array
Line16: From personal experience with the Replace() Compiler will not like the single quotes in this context- IDK why - it just doesn't like them in this syntax.

Modified code:
Expand|Select|Wrap|Line Numbers
  1. Public Function AllNums(strNums As String) As String
  2.      Dim lngX As Long
  3.      Dim astrRange() As String
  4.      Dim varString As Variant
  5.  astrRange = Split(CStr(strNums), "-")
  6.      For Each varString In astrRange
  7.          If UBound(astrRange) > 0 Then
  8.              For lngX = CLng(astrRange(0)) To CLng(astrRange(1))
  9.                  AllNums = AllNums & "," & lngX
  10.              Next lngX
  11.          Else
  12.              AllNums = AllNums & "," & astrRange(0)
  13.          End If
  14.      Next varString
  15.      If Len(AllNums) > 0 Then
  16.       AllNums = Replace(Mid(AllNums, 2), " ", "")
  17.     End If
  18.  End Function
Proof of concept usage in the Immediate pane:
Expand|Select|Wrap|Line Numbers
  1. <ctrl><g>
  2. ?AllNums("929-948")
  3. 929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948
I also have the compact number expansion query...
Table: Multipliers, single field [PK] as numeric(long) record entries 0 to 9
then create the following query
Expand|Select|Wrap|Line Numbers
  1. SELECT ([ones]![pk])+([tens]![pk]*10)+([Hundreds]![pk]*100) AS Beans
  2. FROM Multipliers AS ones, Multipliers AS tens, Multipliers AS Hundreds;
One could use this in several ways to achieve the same effect using parameters, vba on the fly recordset, etc...
Sep 7 '18 #4
NeoPa
32,556 Expert Mod 16PB
Hi Z.

Sorry about that, and thanks for catching them. Brain fart time (s two). I should really have checked it before submitting. I admit it was air-code.

I did a last-minute reshuffle of the code to replace line #6 and got it wrong. I also know exactly why single quotes don't work in VBA. It's because VBA doesn't support them. Nothing to do with the Replace() function at all. The parameters that are supposed to be passed to it are unintelligible to VBA as VBA uses double-quotes (") exclusively. The SQL standard uses single-quotes (') exclusively but Jet & ACE both allow double-quotes (") in their SQL too. It's supposed to make it easier for those learning, but just causes confusion (TBF it does make it marginally easier for very new users but then continues to confuse everyone but the rare few that understand why they did it that way).
Expand|Select|Wrap|Line Numbers
  1. Public Function AllNums(strNums As String) As String
  2.     Dim lngX As Long
  3.     Dim astrRange() As String
  4.     Dim varString As Variant
  5.  
  6.     For Each varString In Split(strNums, ",")
  7.         astrRange = Split(CStr(varString), "-")
  8.         If UBound(astrRange) > 0 Then
  9.             For lngX = CLng(astrRange(0)) To CLng(astrRange(1))
  10.                 AllNums = AllNums & "," & lngX
  11.             Next lngX
  12.         Else
  13.             AllNums = AllNums & "," & astrRange(0)
  14.         End If
  15.     Next varString
  16.     If Len(AllNums) > 0 Then AllNums = Replace(Mid(AllNums, 2), " ", "")
  17. End Function
Sep 9 '18 #5
NeoPa
32,556 Expert Mod 16PB
Nabusafe:
However I am a bit confused, where am I entering this code ...
You need to insert it into a Standard Module then you can call it from the Immediate Pane (See Debugging in VBA.) to test it.
Nabusafe:
... and what exactly will it do for me?
I thought that was explained in my earlier post :
NeoPa:
... the technique of producing the individual numbers in an array or string.
That is a list of all the numbers that are covered by the string format you explained you use (List separator = "," & Range separator = "-").
I tried it with an example list and the results were as below :
Expand|Select|Wrap|Line Numbers
  1. ?AllNums("123-127,300,401-405");
  2. 123,124,125,126,127,300,401,402,403,404,405
Sep 9 '18 #6
Thank you guys both for your help. I really appreciate it.

I will be honest I'm not very proficient with VBA so all of this code is kind of confusing.

Is there anyway someone could attach a downloadable file that shows how all of this code is ran and executed? I would truly be grateful
Sep 10 '18 #7
NeoPa
32,556 Expert Mod 16PB
Nabusafe:
Is there anyway someone could attach a downloadable file that shows how all of this code is ran and executed? I would truly be grateful.
I won't go that far, I think that's too much of an ask, but I'll try to explain easily in simple steps. It will be easier to understand if you've read the previously linked article (Debugging in VBA).
  1. Open Access.
  2. Open up a database within Access that you're happy to run som testing in.
  3. Press Alt-F11 (Hold Alt key while quickly pressing F11 once, then release Alt key.) to open up the VBIDE (Visual Basic Integrated Development Environment).
  4. Navigate to the Project Explorer pane using Ctrl-R.
  5. Right-click on the name of the project at the top and select Insert & Module.
  6. You should see a new module has been added to the list with a name like "Module" plus an integer.
  7. If your Access has been correctly configured (See Require Variable Declaration.) you should see in the new module some basic code like :
    Expand|Select|Wrap|Line Numbers
    1. Option Compare Database
    2. Option Explicit
    If that isn't there then copy and paste it in anyway and fix your Access setup.
  8. After these two lines paste the code found in post #5. If you need to then remove enough spaces from the start of all lines so that the first and last lines are fully to the left within the module.
  9. Now navigate to the Immediate pane by typing Ctrl-G.
  10. Copy and paste my test (?AllNums("123-127,300,401-405");) across into there and hit enter.
  11. Feel free to try other data passed across to the function and see what comes back out.
There's no need to save this module in that particular database. If you close the database it should prompt you to save that module or not.
Sep 10 '18 #8

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

Similar topics

63
by: Jerome | last post by:
Hi, I'm a bit confused ... when would I rather write an database application using MS Access and Visual Basic and when (and why) would I rather write it using Visual Studio .Net? Is it as easy...
5
by: ad | last post by:
I want to restrict only a range of ip can access my web application. How can I do that ?
16
by: a | last post by:
We are writing an app that assigns people to teams based on their curent score. Teams are 8 people, there are 2 teams. (i would like it to be flexible, but this is a start). I need an algorithm...
2
by: geek491 | last post by:
I want an SQL that displays all the days within the range given. SELECT <days> from <dual> where days between '01/01/2006' and '12/12/2006' Dual table is precent in Oracle but what is its...
4
by: Bongard | last post by:
I have a dynamic range that I would like to use as a linked table into Access. The problem is that Access doesn't seem to want to to recognize the dynamic range when you click on "show named...
10
by: AsheboroAlli | last post by:
Hey... googled my problem and got this site... I am new to SQL and pretty much "winging it" as far as learning... I have set the format on my field to mm/dd/yy to match the data that is...
1
by: kreuzen | last post by:
Hi everybody, I'm trying to write a program that checks what the minimum and maximum number out of all the range numbers given. Example: 1-10, 2-20, 3-30 min = 1 max = 30 There are also...
3
by: bbjo827 | last post by:
I have a database that hold information that holds referral dates, this database hold more than one years worth of data, and will be used in the future. We need to create update queries that will up...
1
by: olugard | last post by:
Hello All! I am a relative new comer to VBA/Access/SQL Queries, I have been working for several weeks on a database and I am looking to determine the time period between 2 days are denoted in...
1
by: shawnnnnnnn | last post by:
Hi all, I am trying to create a form that will select records by date range. The form contains 2 textboxes, with pop-up calendars to select dates. I managed to come up with this code, 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:
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...
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.