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!
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?
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.
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: -
Dim MySQL As String, MyDB As DAO.Database, rst1 As DAO.Recordset
-
Dim rst2 As DAO.Recordset
-
-
MySQL = "Select [DashNum] From tblTest Order By DashNum;"
-
-
Set MyDB = CurrentDb()
-
Set rst1 = MyDB.OpenRecordset(MySQL, dbOpenSnapshot)
-
Set rst2 = rst1.Clone
-
-
rst1.MoveFirst: rst2.Move 1 'move to the 2nd Record in rst2
-
-
Do While Not rst2.EOF
-
'If the difference between 2 consecutive Dash Numbers is not 1, since
-
'they are ordered, this would indicate a gap in sequence
-
If rst2![DashNum] <> rst1![DashNum] + 1 Then
-
Me![txtDashNum] = rst1![DashNum] + 1
-
rst2.Close
-
rst1.Close
-
Set rst2 = Nothing
-
Set rst1 = Nothing
-
Exit Sub
-
End If
-
rst1.MoveNext
-
rst2.MoveNext
-
Loop
-
-
rst2.Close
-
rst1.Close
-
Set rst2 = Nothing
-
Set rst1 = Nothing
-
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!
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.
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: - If rst1.EOF Then
-
[DashNum] = 1
-
GoTo LastLine ' I have a tag at the bottom before the close and destroy statements
-
-
Else
-
rst1.MoveLast
-
rst1.MoveFirst
-
rst2.MoveLast
-
rst2.MoveFirst
-
End If
-
-
If Not rst1.EOF And rst1![JID] <> 1 Then
-
[DashNum] = 1
-
GoTo LastLine
-
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: - [DashNum] = rst1![JID] + 1
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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 ***
|
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.
|
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.
|
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
| |
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
|
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
|
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......
|
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)
-...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| | |