473,654 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find first unused record number, above control number

62 New Member
Hi all,

I have a table called tblRecords that has "DashNum" [type: number] as its primary key. The lowest value of this table is 116 and the highest value is 269, though there are some missing values. It is expected that records will be removed in the future, leaving more missing values. I would like to have a text box have as its default value the first unused entry in this table.

I found a way to do this, but its not very efficient and requires an otherwise useless secondary table. I created a table called "DashNumBas is" with two columns: "DashNum" and "Used". "DashNum" has values starting at 116 and going to 9999, and used is a yes/no field. In order to find the first unused entry I have to run an update query which sets DashNumBasis!Us ed to "yes" for any record found in tblRecords. Then I have a select query find all DashNum from tblRecords where "Used" is "no", then finally I have to use a VBA expression DMIN("DashNum", "qryUnused" ) to find the first unused value.

While this works it means the user has to run two queries, plus a third query to reset all DashNum values to "no" after its done so the next update will grab values that are removed from the table.

I know I can have VBA run this code, but it seems like a complicated answer to a simple problem. Please help!
Oct 11 '07 #1
6 6969
ADezii
8,834 Recognized Expert Expert
Hi all,

I have a table called tblRecords that has "DashNum" [type: number] as its primary key. The lowest value of this table is 116 and the highest value is 269, though there are some missing values. It is expected that records will be removed in the future, leaving more missing values. I would like to have a text box have as its default value the first unused entry in this table.

I found a way to do this, but its not very efficient and requires an otherwise useless secondary table. I created a table called "DashNumBas is" with two columns: "DashNum" and "Used". "DashNum" has values starting at 116 and going to 9999, and used is a yes/no field. In order to find the first unused entry I have to run an update query which sets DashNumBasis!Us ed to "yes" for any record found in tblRecords. Then I have a select query find all DashNum from tblRecords where "Used" is "no", then finally I have to use a VBA expression DMIN("DashNum", "qryUnused" ) to find the first unused value.

While this works it means the user has to run two queries, plus a third query to reset all DashNum values to "no" after its done so the next update will grab values that are removed from the table.

I know I can have VBA run this code, but it seems like a complicated answer to a simple problem. Please help!
I have a table called tblRecords that has "DashNum" [type: number] as its primary key. The lowest value of this table is 116 and the highest value is 269, though there are some missing values
Hello nickvans, maybe I am misinterpreting what you are saying, but if DashNum is the Primary Key with values ranging from 116 thru 169, how can there be missing values?
Oct 11 '07 #2
nickvans
62 New Member
Hello nickvans, maybe I am misinterpreting what you are saying, but if DashNum is the Primary Key with values ranging from 116 thru 169, how can there be missing values?

Hi ADezii, Thanks for your response.

I have DashNum set as the primary key to prevent multiple entries using the same DashNum. The way the system is set up, we quote dash numbers to other departments for future work. Sometimes we get a notification that we won't actually use that dash number, and go back and delete it from our database. For instance, we might quote that we'll use dash number 270 for one customer, then the next day quote that we'll use 271 for another customer. The next day the customer who wanted 270 comes back saying they'll use a previously existing module, and no future work will be done. Dash number 270 should then be available for us to assign to another customer.

Hope this helps. Thanks.
Oct 11 '07 #3
ADezii
8,834 Recognized Expert Expert
Hi ADezii, Thanks for your response.

I have DashNum set as the primary key to prevent multiple entries using the same DashNum. The way the system is set up, we quote dash numbers to other departments for future work. Sometimes we get a notification that we won't actually use that dash number, and go back and delete it from our database. For instance, we might quote that we'll use dash number 270 for one customer, then the next day quote that we'll use 271 for another customer. The next day the customer who wanted 270 comes back saying they'll use a previously existing module, and no future work will be done. Dash number 270 should then be available for us to assign to another customer.

Hope this helps. Thanks.
This code should work nicely for you. It assumes the Table containing the [DashNum] Field is named tblTest, and the Field on your Form to hold the 1st unused sequential Dash Number is txtDashNum. Make your necessary substitutions, and let me klnow how you make out:
Expand|Select|Wrap|Line Numbers
  1. Dim MySQL As String, MyDB As DAO.Database, rst1 As DAO.Recordset
  2. Dim rst2 As DAO.Recordset
  3.  
  4. MySQL = "Select [DashNum] From tblTest Order By DashNum;"
  5.  
  6. Set MyDB = CurrentDb()
  7. Set rst1 = MyDB.OpenRecordset(MySQL, dbOpenSnapshot)
  8. Set rst2 = rst1.Clone
  9.  
  10. rst1.MoveFirst: rst2.Move 1     'move to the 2nd Record in rst2 
  11.  
  12. Do While Not rst2.EOF
  13.   'If the difference between 2 consecutive Dash Numbers is not 1, since
  14.   'they are ordered, this would indicate a gap in sequence
  15.   If rst2![DashNum] <> rst1![DashNum] + 1 Then
  16.     Me![txtDashNum] = rst1![DashNum] + 1
  17.       rst2.Close
  18.       rst1.Close
  19.       Set rst2 = Nothing
  20.       Set rst1 = Nothing
  21.         Exit Sub
  22.   End If
  23.   rst1.MoveNext
  24.   rst2.MoveNext
  25. Loop
  26.  
  27. rst2.Close
  28. rst1.Close
  29. Set rst2 = Nothing
  30. Set rst1 = Nothing
  31.  
Oct 11 '07 #4
nickvans
62 New Member
This code should work nicely for you. It assumes the Table containing the [DashNum] Field is named tblTest, and the Field on your Form to hold the 1st unused sequential Dash Number is txtDashNum. Make your necessary substitutions, and let me klnow how you make out:
Thanks ADezii, your code works fantastically. It actually found a couple records which weren't in my database and should have been. Thanks a million!
Oct 15 '07 #5
ADezii
8,834 Recognized Expert Expert
Thanks ADezii, your code works fantastically. It actually found a couple records which weren't in my database and should have been. Thanks a million!
I'm happy it all worked out for you.
Oct 15 '07 #6
KingWm
1 New Member
Thanks for the information. As an Access Hack, I found this very useful. However, there are two scenarios not covered I thought I would mention for others: 1) the table is empty and so the first available number is 1, and 2) all the numbers are in sequence and so there are none skipped. I am using this routine for a table that will be empty upon first use. And, since we are always filling in skipped numbers, it will be typical that there are none skipped. The only time we will find a skipped number is when a record was deleted.

First, if there are no records in the recordset, then you need a test at the beginning. I put this at the beginning of mine:


Expand|Select|Wrap|Line Numbers
  1. If rst1.EOF Then
  2.     [DashNum] = 1
  3.     GoTo LastLine ' I have a tag at the bottom before the close and destroy statements
  4.  
  5. Else
  6.     rst1.MoveLast
  7.     rst1.MoveFirst
  8.     rst2.MoveLast
  9.     rst2.MoveFirst
  10. End If
  11.  
  12. If Not rst1.EOF And rst1![JID] <> 1 Then
  13.     [DashNum] = 1
  14.     GoTo LastLine
  15. End If
Then, in the event that there are no skipped numbers, you need a line at the end, after the loop, to set the variable, otherwise you will get "0", or no result:

Expand|Select|Wrap|Line Numbers
  1. [DashNum] = rst1![JID] + 1
  2.  
Thanks for the idea. I hope someone else finds this useful. Even though it has been 9 years since this was posted, it is still relavent.
Nov 22 '16 #7

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

Similar topics

13
1510
by: Chris Cioffi | last post by:
Hello all! I'm trying to subclass int such that once it reaches a certain value, it flips back to the starting count. This is for use as an X12 control number. Subclassing int and getting the display like I want it was trivial, however what's the best way to make it such that: 990 + 1 = 991 991 + 1 = 992 .... 998 + 1 = 999
26
30917
by: Martin R | last post by:
Hi, How to find first not null value in column whitout chacking whole table (if there is a not null value then show me it and stop searching, the table is quite big)? thx, Martin *** Sent via Developersdex http://www.developersdex.com ***
1
11118
by: | last post by:
How can I obtain the record number or row number for a record while in form view? Thank you in advance for any information. R. L.
14
16220
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to provide me with small sample code for that.
4
19398
by: jim | last post by:
I've been searching and trying out SQL statements for hours and I'm still having a problem. I'm querying 3 tables... and I only want the first unique record returned. Currently, the data returned looks like this.............. New York New York A New York New York B
2
1851
by: angelscorp | last post by:
How can we fetch the record with maximun seq number where seq number is a column in the table......the table contains nearly fifty columns so below query is not working. SELECT TOP 1 id, name, salary from table ORDER BY id DESC
2
8643
by: kai | last post by:
Hi, I use VB 2008 as front end, SQL Server 2005/2008 as back end, I want control number of cucurrent users log on to the database, I am trying to use a stored procedure, but it did not work. Is this possible? Please help. Thanks Kai
3
2346
by: pchaitanya | last post by:
I have selected some list of valid dates to a label. now i need to find first day among the given dates from label contrl i got dates from calender control by clicking for entire week.. that is i have dates from sunday to saturday and I have to find which date is sunday......
5
3047
by: paragpdoke | last post by:
Hello All. I am new to SQL ... trying to make a query work untimately in Perl (v5.10.0 built for MSWin32-x86-multi-thread) against MySQL hosted remotely (which currently I don't know the version for). I am currently trying with Quantum DB plugin for Eclipse to run the query. I am trying to work with 2 tables ... something equivalent to the following example: Table: Orders - OrderId (unique integer) - CustomerNumber (non-unique integer) -...
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.