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

How to ignore certain Customer Numbers and increase a certain kind incrementally?

269 256MB
Hi Bytes!

I am working in a database and we've gone through several big changes this year in our Order Numbers (ShopOrder is the field name).
These are the types of ShopOrders we have had this year:
039801, 039802, 039803… (this was a sequence, now going away)
(no sequence on these next three, just showing the variety of ShopOrders we currently have in the database)
810964A3180
809511A3744-1
TDCC22A0100
TF0001, TF0002, TF0003… (will still use this sequence sometimes, special case, and can input manually)

My desire is to begin a new sequence that looks like this:
20160001, 20160002, 20160003 for this year, and of course next year we will start 20170001...

I have some code from another database increases the number (default value) by 1. Here is the code:
Expand|Select|Wrap|Line Numbers
  1. Format$(Year(Date()),"0000") & IIf(Val(Left$(DMax("[ShopOrder]","tblShopOrders"),4))=Val(Format$(Year(Date()),"0000")),Format$(Val(Right$(DMax("[ShopOrder]","tblShopOrders"),4)+1),"0000"),"0001")
However, in this database it doesn't seem to be working and I suspect it is because I have these other varieties of OrderNumbers already in the database. (My other databases where I successfully use this code doesn't have all this clutter!)

Can someone tell me how I can ignore all the junk order numbers and just look for the max YEAR ShopOrder and increase it by 1, like I desire? I'm not a code writer, FYI, but I do great things by picking up code off the internet and manipulating it to fit my needs. Pretty please be specific with the code if possible. :-)
If it helps, this is the only sequence in my database that has 8 digits, so could we use that somehow to our advantage, like ignore everything that isn't 8 digits? How would I code this?

Thanks in advance!!
Danica
Sep 28 '16 #1
5 827
jforbes
1,107 Expert 1GB
Hey Danica,

Here is a similar piece of Code to yours:
Expand|Select|Wrap|Line Numbers
  1. Public Function getKey(ByRef sTable As String, ByRef sField As String, ByRef iKeyLen As Integer, ByRef sPrefix As String) As String
  2.     Dim sLastKey As String
  3.     sLastKey = Nz(DMax(sField, sTable, sField & " LIKE '" & sPrefix & "*'"), sPrefix & "0")
  4.     getKey = sPrefix & Right("00000" & Val((Right(sLastKey, Len(sLastKey) - Len(sPrefix))) + 1), iKeyLen - Len(sPrefix))
  5. End Function
If you called it like so:
Expand|Select|Wrap|Line Numbers
  1. ?getKey("tblShopOrders", "ShopOrder", 6, "TF")
you sould get the next key that you are expecting.

The main difference between this code and yours is this code uses the Criteria in the DMAX() function to limit what records to look at to determine the last key that was used.
Sep 29 '16 #2
DanicaDear
269 256MB
I am no expert here, but does this code increase the sequence of TF0001, TF0002...?
I want to increase the 20160001, 20160002... (not the TF sequence..we will have to do that one manually if I use the default value to increase the 2016xxxx sequence). I will see if I can apply your idea to my situation. Strings freak me out but I can at least try this!
Thank you very much for your reply. :-)
Sep 29 '16 #3
jforbes
1,107 Expert 1GB
Haha, Yeah, I provided a solution for the key starting with TF. This function call works for a Key that starts with the Year:
Expand|Select|Wrap|Line Numbers
  1. ?getKey("tblShopOrders", "ShopOrder", 8, year(now()))
Sorry about the mixup.
Sep 29 '16 #4
DanicaDear
269 256MB
Ok, I got this working, for the most part. I had never seen a Public Function before. :-)
I have the getKey set under the default value for ShopOrder in my properties, with the Public Function in an event procedure when you click the NEW button which I have located on the form.
When I open the form (fresh start) and go to new record, it increases the value perfectly. However, when I type in the field, it keeps that ShopOrder value in the field I am writing in, and ALSO gives the next field (it would then be the new record) the same value, I suppose because the write field isn't saved yet so the next new field adds one to the previously saved field.

Example:
20160005 is the last saved record.
Click new record. 20160006 defaults correctly. As soon as I begin to type in details, 20160006 also defaults into the next record too. Not only would this be the wrong number, but my primary key would duplicate. Any further ideas? Did I screw it up?

PS. Just in case someone follows this later.... I changed your "00000" to "0000" in your Public Function code. I think that's what you meant. :-)
Sep 30 '16 #5
jforbes
1,107 Expert 1GB
It sounds like you have everything setup correctly. Usually when using something like this as a Default for a field, this is how it works. When you save your new record, the key in the new record that is shown directly below the one you just saved to, should update and give display the correct next key value.

That point in time where they are both showing the same key value is correct. You as a human know that they should be different, but the defaulting and the Function are just returning the next key based on what it knows, which is the saved records.

Changing "00000" to "0000" shouldn't impact what happens much. The code that is being executed on that line is pretty dense and so it's a bit hard to follow:
Expand|Select|Wrap|Line Numbers
  1. getKey = sPrefix & Right("00000" & Val((Right(sLastKey, Len(sLastKey) - Len(sPrefix))) + 1), iKeyLen - Len(sPrefix))
  2.  
  3. If the Previous key was "20160123" and the function
  4. was run, sLastKey would be equal to "20160123" and 
  5. the sPrefix would be "2016".  
  6. Substituting these values, this would be the result:
  7.  
  8. getKey = "2016" & Right("00000" & Val((Right("20160123", Len("20160123") - Len("2016"))) + 1), 8 - Len("2016"))
  9.  
  10. Simplifying more would give 
  11. getKey = "2016" & Right("00000" & Val("0123") + 1, 8 - 4)
  12. ...
  13. getKey = "2016" & Right("00000" & 124, 4)
  14. ...
  15. getKey = "2016" & Right("00000124", 4)
  16. ...
  17. getKey = "2016" & "0124"
  18. ...
  19. getKey = "20160124"
Sep 30 '16 #6

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

Similar topics

2
by: Dave Bootsma | last post by:
Is it possible to programatically save a certain image from a certain web page? I want to automatically get a specific graphic from a specific web page programatically so I can automate the...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
4
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view...
1
by: alw0015 | last post by:
I'm working on a piece of code that reads in a time from the user. This time value consists of 3 separate inputs: 1 integer representing hours 1 char representing the ":" 1 integer...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
14
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems,...
30
by: DanicaDear | last post by:
I will search for on frmCustomers. When I find the customer, I click a button btnShowOrders and it opens frmOrders with subfrmOrderDetails2. (I'm having trouble grasping the previous sentence...
1
by: Moru | last post by:
Hi all i have text1.text change every 10 m/s like this 0.555kg0.566kg0.590kg0.00 and after 10m/s 55kg0.670kg0.590kg0.00 kg0.605kg0.6155kg0.00 How can i make text2.text = the forth characters...
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: 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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.