简体   繁体   中英

Expand Range of Numbers with SQL in Access

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:

在此处输入图片说明

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.

I want it to be able to give me this just by the press of a macro button:

在此处输入图片说明

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!!

You can write some code to do this. The amount of code is not long, but it is “tricky” code.

The following code would be “close” to what you need. The following code is “air code”. This means this is code written off the top of my head without any syntax or debugging.

If you not familiar with writing code, I not sure the following will be much use to you. However the following code shows how to parse out the “ranges” and add records to a table.

So you can do this, but you NEED the ability to write some VBA code. As noted, the following is the base outline how such code could be written:

Sub ParseOut()

   Dim rst          As DAO.Recordset    ' input talbe
   Dim rstOut       As DAO.Recordset    ' output (expanded rows)
   Dim strBase      As String
   Dim strOutPut    As String

   Dim rZip         As Variant
   Dim rZips        As Variant
   Dim rStart       As Integer
   Dim rEnd         As Integer

   Dim oneRange     As Variant
   Dim range        As Integer

   strBase = "tblRanges"

   strOutPut = "tblOutRange"

   With CurrentDb() ' added this to reach min chars for edit, but this saves one CurrentDb (for sure 0,005 secs)
     Set rst = .OpenRecordset(strBase)
     Set rstOut = .OpenRecordset(strOutPut)
   End With

   Do While rst.EOF = False

      rZips = Split(rst!ZipCodes, ",")
      For Each rZip In rZips
         oneRange = Split(rZip, "-")
         If LBound(oneRange, 1) = 0 Then
            ' no "-", so single value
            rStart = oneRange(0)
            rEnd = rStart
         Else
            ' start/end range
            rStart = oneRange(0)
            rEnd = oneRange(1)
        End If

        ' add the range to the table
        For range = rStart To rEnd
           rstOut.AddNew
           rstOut!City = rst!City
           rstOut!State = rst!State
           rstOut!Zip = range
           rst.Update
         Next range
      Next rZip

      rst.MoveNext 
   Loop

   rst.Close
   rstOut.Close

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM