473,756 Members | 5,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update query that adds 1 to a previous value

I have asked this question before, but I could not get the suggested
solution work. So I will give more details this time.

I have an append query that adds several hundred records to a table into a
text field. Next to this text field is a separate Number (Single) field
with a unique ID number for the entry. I want each number to be 1 more than
the previous entry's number to keep it unique (I don't want autonumbering
for this, as I want to be able to change that number in some circumstances)

So, either as a part of the append query, or as a separate update query, I
need to be able to go through each entry that has a value of 0 in that field
(the default value) and find the maximum number in that field, then add 1 to
it. Is this even possible in a simple update query?

If it is not, is there some code I can put into a module and run. The table
I am updating is called tblNew, the text field I am appending is called
Category and the Integer field I want to increment by 1 is called
CategoryID.

Any solutions?

dixie
Aug 27 '06 #1
6 5017
There are several issues to be discussed before I could offer the best
solution.
First, you say you want a unique number for each record and that you want
them to be sequentially numbered (add 1 to the previous number) however you
want the flexibility to change the number. What would you change the number
to be and how would you avoid eventually running into that number in the
future?
(By the way, single integer limits you to 32,000 numbers. why not use
Long?)
Since you want the flexibility to change the number suggests to me that the
number is meaningful in some way. What are you using the number for?
There are many ways to come up with a unique ID. Tell us a little more
about what you are doing.

"Dixie" <di***@dogmail. comwrote in message
news:12******** *****@corp.supe rnews.com...
>I have asked this question before, but I could not get the suggested
solution work. So I will give more details this time.

I have an append query that adds several hundred records to a table into a
text field. Next to this text field is a separate Number (Single) field
with a unique ID number for the entry. I want each number to be 1 more
than the previous entry's number to keep it unique (I don't want
autonumbering for this, as I want to be able to change that number in some
circumstances)

So, either as a part of the append query, or as a separate update query, I
need to be able to go through each entry that has a value of 0 in that
field (the default value) and find the maximum number in that field, then
add 1 to it. Is this even possible in a simple update query?

If it is not, is there some code I can put into a module and run. The
table I am updating is called tblNew, the text field I am appending is
called Category and the Integer field I want to increment by 1 is called
CategoryID.

Any solutions?

dixie

Aug 27 '06 #2
Firstly let me answer the question about the Long - the most entries this
table is ever likely to see is about 3000 - 4000, it will certainly never
approach 32,000, but I take your point.

Basically, the table is full of class names for a K-6 school. Each year,
the classes change name, according to the grade the students are in and the
teacher's initials, so like 2KW, 3AV, 4-5ST. Each class has an accompanying
ID number for uniqueness. It is possible a class name may repeat in another
calendar year. I don't want to delete previous classes, as those classes
are tied to educational records.

Why do I not want to use Autonumber. Firstly, in my experience, autonumbers
will often jump huge amounts of numbers and in some of my databases, they
get so large, they start getting reported as exponential numbers.

I'll admit that right now, I cannot think of a reason to change the numbers
once they are there, but I don't like Autonumbers for the reasons given
above. I just thought it would be easy to add one to the highest ID number
in the table for each new entry. These entries are usually dumped in as one
job lot from the append query at the start of the school year and there is
usually not more than about 60, although it can be more, depending on the
structure of the school.

I have tried to do this and failed. I am now assuming I have to do some
kind of walking through the records routine.

dixie

"Len Robichaud" <le**********@c omcast.netwrote in message
news:6u******** *************** *******@comcast .com...
There are several issues to be discussed before I could offer the best
solution.
First, you say you want a unique number for each record and that you want
them to be sequentially numbered (add 1 to the previous number) however
you want the flexibility to change the number. What would you change the
number to be and how would you avoid eventually running into that number
in the future?
(By the way, single integer limits you to 32,000 numbers. why not use
Long?)
Since you want the flexibility to change the number suggests to me that
the number is meaningful in some way. What are you using the number for?
There are many ways to come up with a unique ID. Tell us a little more
about what you are doing.

"Dixie" <di***@dogmail. comwrote in message
news:12******** *****@corp.supe rnews.com...
>>I have asked this question before, but I could not get the suggested
solution work. So I will give more details this time.

I have an append query that adds several hundred records to a table into
a text field. Next to this text field is a separate Number (Single)
field with a unique ID number for the entry. I want each number to be 1
more than the previous entry's number to keep it unique (I don't want
autonumberin g for this, as I want to be able to change that number in
some circumstances)

So, either as a part of the append query, or as a separate update query,
I need to be able to go through each entry that has a value of 0 in that
field (the default value) and find the maximum number in that field, then
add 1 to it. Is this even possible in a simple update query?

If it is not, is there some code I can put into a module and run. The
table I am updating is called tblNew, the text field I am appending is
called Category and the Integer field I want to increment by 1 is called
CategoryID.

Any solutions?

dixie


Aug 27 '06 #3
"Dixie" <di***@dogmail. comwrote in
news:12******** *****@corp.supe rnews.com:

[snip 4 lines]
>
Basically, the table is full of class names for a K-6 school.
Each year, the classes change name, according to the grade the
students are in and the teacher's initials, so like 2KW, 3AV,
4-5ST. Each class has an accompanying ID number for
uniqueness. It is possible a class name may repeat in another
calendar year. I don't want to delete previous classes, as
those classes are tied to educational records.
how about doind this: 2006_2KW,2006_3 AV,2006_4-5ST etc.

You could even just use a separate field for the year, and
create a composite primary key based on hte two fields.

Any simple query solution will not work, as all records in the
batch will get the same number.

So we are left with a recordset solution manipulated in VB.
Copy the code below and Paste this into the database, then save
and exit. Make a copy of the .mdb to test. Test in the copy. if
it looks ok, run it in the original .mdb.

public sub SetNewId()
Dim RS as dao.recordset
Dim strSQL as string
Dim iCounter as single 'you really want to change the id number
'to an integer or long int.

strSQL = "SELECT ClassID FROM Classes WHERE ClassID = 0"
icounter = Dmax("ClassID", "Classes")+ 1

set RS= Currentdb.OpenR ecordset(strsql ,dbOpenDynaset)
do until RS.EOF
RS.edit
RS.fields("clas sID") = iCounter
RS.Update
iCounter = iCounter + 1
loop
RS.close
set RS = nothing
End Sub

[snip]
--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Aug 27 '06 #4
I have done that Bob. Firstly, using a Single number just sends it off into
Oblivion and I have to do a control break to get it back. I then changed
both the field I am working on to Integer and your code to Dim iCounter As
Integer. I then get the error Run-time error '6': Overflow.
The line
iCounter = DMax("FacultyID ", "tblFaculti es") + 1 is highlighted when I take
the debug option.

When I then look in the table, only one value has changed and it has gone
from 0 to 32767

dixie

"Bob Quintal" <rq******@sPAmp atico.cawrote in message
news:Xn******** **************@ 66.150.105.47.. .
"Dixie" <di***@dogmail. comwrote in
news:12******** *****@corp.supe rnews.com:

[snip 4 lines]
>>
Basically, the table is full of class names for a K-6 school.
Each year, the classes change name, according to the grade the
students are in and the teacher's initials, so like 2KW, 3AV,
4-5ST. Each class has an accompanying ID number for
uniqueness. It is possible a class name may repeat in another
calendar year. I don't want to delete previous classes, as
those classes are tied to educational records.

how about doind this: 2006_2KW,2006_3 AV,2006_4-5ST etc.

You could even just use a separate field for the year, and
create a composite primary key based on hte two fields.

Any simple query solution will not work, as all records in the
batch will get the same number.

So we are left with a recordset solution manipulated in VB.
Copy the code below and Paste this into the database, then save
and exit. Make a copy of the .mdb to test. Test in the copy. if
it looks ok, run it in the original .mdb.

public sub SetNewId()
Dim RS as dao.recordset
Dim strSQL as string
Dim iCounter as single 'you really want to change the id number
'to an integer or long int.

strSQL = "SELECT ClassID FROM Classes WHERE ClassID = 0"
icounter = Dmax("ClassID", "Classes")+ 1

set RS= Currentdb.OpenR ecordset(strsql ,dbOpenDynaset)
do until RS.EOF
RS.edit
RS.fields("clas sID") = iCounter
RS.Update
iCounter = iCounter + 1
loop
RS.close
set RS = nothing
End Sub

[snip]
--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Aug 27 '06 #5
I think I have found it. I just added an RS.MoveNext after RS.Update and it
is working.

Does that make sense?

Thanks again Bob

dixie

"Dixie" <di***@dogmail. comwrote in message
news:12******** *****@corp.supe rnews.com...
>I have done that Bob. Firstly, using a Single number just sends it off
into Oblivion and I have to do a control break to get it back. I then
changed both the field I am working on to Integer and your code to Dim
iCounter As Integer. I then get the error Run-time error '6': Overflow.
The line
iCounter = DMax("FacultyID ", "tblFaculti es") + 1 is highlighted when I
take the debug option.

When I then look in the table, only one value has changed and it has gone
from 0 to 32767

dixie

"Bob Quintal" <rq******@sPAmp atico.cawrote in message
news:Xn******** **************@ 66.150.105.47.. .
>"Dixie" <di***@dogmail. comwrote in
news:12******* ******@corp.sup ernews.com:

[snip 4 lines]
>>>
Basically, the table is full of class names for a K-6 school.
Each year, the classes change name, according to the grade the
students are in and the teacher's initials, so like 2KW, 3AV,
4-5ST. Each class has an accompanying ID number for
uniqueness. It is possible a class name may repeat in another
calendar year. I don't want to delete previous classes, as
those classes are tied to educational records.

how about doind this: 2006_2KW,2006_3 AV,2006_4-5ST etc.

You could even just use a separate field for the year, and
create a composite primary key based on hte two fields.

Any simple query solution will not work, as all records in the
batch will get the same number.

So we are left with a recordset solution manipulated in VB.
Copy the code below and Paste this into the database, then save
and exit. Make a copy of the .mdb to test. Test in the copy. if
it looks ok, run it in the original .mdb.

public sub SetNewId()
Dim RS as dao.recordset
Dim strSQL as string
Dim iCounter as single 'you really want to change the id number
'to an integer or long int.

strSQL = "SELECT ClassID FROM Classes WHERE ClassID = 0"
icounter = Dmax("ClassID", "Classes")+ 1

set RS= Currentdb.OpenR ecordset(strsql ,dbOpenDynaset)
do until RS.EOF
RS.edit
RS.fields("clas sID") = iCounter
RS.Update
iCounter = iCounter + 1
loop
RS.close
set RS = nothing
End Sub

[snip]
--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com


Aug 27 '06 #6
"Dixie" <di***@dogmail. comwrote in
news:12******** *****@corp.supe rnews.com:
I think I have found it. I just added an RS.MoveNext after
RS.Update and it is working.

Does that make sense?
make me stay after class for forgetting the movenext. Yes it
makes perfect sense. it finds the highest number, adds 1 and
puts it into the same place as the last record. since it never
se4es the EOF, it just keeps going until the number is too big
to fit into the field.
>
Thanks again Bob

dixie

"Dixie" <di***@dogmail. comwrote in message
news:12******** *****@corp.supe rnews.com...
>>I have done that Bob. Firstly, using a Single number just
sends it off into Oblivion and I have to do a control break to
get it back. I then changed both the field I am working on to
Integer and your code to Dim iCounter As Integer. I then get
the error Run-time error '6': Overflow.
The line
iCounter = DMax("FacultyID ", "tblFaculti es") + 1 is
highlighted when I take the debug option.

When I then look in the table, only one value has changed and
it has gone from 0 to 32767

dixie

"Bob Quintal" <rq******@sPAmp atico.cawrote in message
news:Xn******* *************** @66.150.105.47. ..
>>"Dixie" <di***@dogmail. comwrote in
news:12****** *******@corp.su pernews.com:

[snip 4 lines]

Basically, the table is full of class names for a K-6
school. Each year, the classes change name, according to
the grade the students are in and the teacher's initials,
so like 2KW, 3AV, 4-5ST. Each class has an accompanying ID
number for uniqueness. It is possible a class name may
repeat in another calendar year. I don't want to delete
previous classes, as those classes are tied to educational
records.

how about doind this: 2006_2KW,2006_3 AV,2006_4-5ST etc.

You could even just use a separate field for the year, and
create a composite primary key based on hte two fields.

Any simple query solution will not work, as all records in
the batch will get the same number.

So we are left with a recordset solution manipulated in VB.
Copy the code below and Paste this into the database, then
save and exit. Make a copy of the .mdb to test. Test in the
copy. if it looks ok, run it in the original .mdb.

public sub SetNewId()
Dim RS as dao.recordset
Dim strSQL as string
Dim iCounter as single 'you really want to change the id
number 'to an integer or long int.

strSQL = "SELECT ClassID FROM Classes WHERE ClassID = 0"
icounter = Dmax("ClassID", "Classes")+ 1

set RS= Currentdb.OpenR ecordset(strsql ,dbOpenDynaset)
do until RS.EOF
RS.edit
RS.fields("clas sID") = iCounter
RS.Update
iCounter = iCounter + 1
loop
RS.close
set RS = nothing
End Sub

[snip]
--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from
http://www.teranews.com





--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Aug 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4747
by: Ken Bush | last post by:
How can I write an update query that removes part of a field? Like if I have a field with values such as 8/3/68 (a birthday obviously) and I need to put values in a new column but I need everything after and including the final / removed to end up with simply 8/3
3
7052
by: Bill Clark | last post by:
I have about 20,000 records pulled from Excel that I need to update. What I need to do is run an update query that bascially says: If a field is null, update it with the previous record value of that same field. In some instances, it will have to go back a few records before it finds a value that is not null. Can this be done? Thanks Bill
3
15257
by: -Michelle- | last post by:
Hi Using A2003 on XP I am wondering from the MVP's and others, what is the most efficient way (in terms of time to process) of updating data in a table, using the docmd.RunSQL or Recordset 'Edit' and 'Update'? eg: (if you need it)
2
2260
by: Dejan Pujic | last post by:
Hello, I'm having difficulty figuring out how to update values in a certain column, where the new information depends on the previous. Here's a further description: The whole database is for insurance purposes. tbl_Property ID Name Client Value ------------------------------ 1 HouseA Client1 300,000
3
1878
by: Larry Woods | last post by:
I have a datagrid that is carrying all fields of a record...except one. Now I want to update the underlying database via a dataadapter. The update is working but the field that is "left out" is not there, of course. How do I get that field back into the datatable for the database update? TIA, Larry Woods
13
2456
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever works best) to display a dropdown menu of fields populated from table tblInvoiceData. This control also includes a textbox which the user can input a value. These two columns are side by side and not in a vertical layout. The user then clicks on...
3
1607
by: DavidPr | last post by:
I've wrestled with this code all day and I just can't figure out what the problem is. I have this same code on the add_job.php page and the edit_job.php page and neither one is enter in the correct information into the database. But first here's a synopsis of what happens up to this point: The registration script adds users to the users table and a row in that table is named stateid. The information that goes into this stateid row is...
11
4282
by: stantron | last post by:
Setup: I only have one database with one table in it. The first page has a form that adds a record (w/ 6 fields in it) to the mySQL database's lone table via PHP. This works fine. I also have a PHP table that displays all records in my database on this same page. This too works fine. I have my table set up so that there is a 7th field, called "rid" to act as a "record id" or unique identifying field. This is primary and auto-incremented. There...
1
1870
beacon
by: beacon | last post by:
Hi everybody, I have an Employee table with the following: Table - Employee -------------------------- ID: AutoNum, PK FName: Text LName: Text Status: Yes/No
0
9454
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...
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8709
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...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5139
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
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
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
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2664
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.