how to define a primary autonumbering field in a makequery

G

Guest

I need to use a makequery in Access 2000 to build a table of customer's
invoices. I want to use the query to generate the invoice numbers based
upon a starting number I give it. If I could define a primary key field with
an autonumber I could use this field to increment from my starting number.
The problem is the only fields I can define are the ones that come from
existing tables and queries. i don't see how to define a field as a primary
key auto numbering field.
 
G

Guest

Try doing a make table query one time, just to get the structure set up.
After you have done so, add a primary key autonumber field. The autonumber
field will auto-populate as the records are appended. This gets you an
autonumber field, correctly populated starting with 1.

If you need to run the process again, you could just append new records to
this table instead of re-creating it, or (if you need to restart the sequence
at 1), delete all the records in this table, compact/repair the database
(which resets the autonumber to 1 for the empty table), and append new
records into it.
 
G

Guest

Hi, Ruby.
upon a starting number I give it. If I could define a primary key field with
an autonumber I could use this field to increment from my starting number.
The problem is the only fields I can define are the ones that come from
existing tables and queries. i don't see how to define a field as a primary
key auto numbering field.

You'll need more than one query. In fact, you'll need eight queries, unless
you want to make manual changes to the table. Here are the steps:

1.) In your make-table query, alter the name of the table created to tblTemp.
2.) Run the make-table query.
3.) Create a new query and paste the following into the SQL View pane:

ALTER TABLE tblTemp
ADD COLUMN ID COUNTER (1001, 1)
CONSTRAINT PrimaryKey PRIMARY KEY;

Change the column name from ID to whatever name you want your primary key to
have, and change 1001 (the seed), to whatever number you want your invoices
to start at. Save and run the query. Open the tblTemp table and you'll
notice that Jet ignored your seed number. It doesn't matter what number you
use as the seed, because Jet will ignore it and start sequential numbering at
1. If this one thing worked correctly, then you wouldn't have to do any more
steps. But it doesn't.

You might be tempted to use an UPDATE query to add 1000 to all the numbers
in the primary key column to set them to the correct numbers, but you'll find
that this field is not updateable. But that doesn't mean that you can't
change these numbers.

4.) Create a new query and paste the following into the SQL View pane:

ALTER TABLE tblTemp
ALTER COLUMN ID LONG;

Change the column name from ID to the name of your primary key. Save and
run the query.

5.) Create a new query and paste the following into the SQL View pane:

UPDATE tblTemp
SET ID = ID + 1000;

Change the column name from ID to the name of your primary key and change
1000 to a number one less than the starting number for your invoices. Save
and run the query. Open the table and you'll find that the numbers are as
they should be, but the primary key column is of Long data type, not an
AutoNumber. You might be tempted to create and run a query that changes the
primary key column to an AutoNumber, but you can't change a field to an
AutoNumber when there are records in the table. But that doesn't mean that
you can't have an AutoNumber for this field.

5.) Create a new query and paste the following into the SQL View pane:

SELECT * INTO tblMyTable
FROM tblTemp
WHERE (1 = 0);

Change tblMyTable to the name of the table that your original make-table
query created. Save and run the query. You'll notice that this creates a
new table with no records, but the structure of the table is the same as
tblTemp. Because there are no records in this table, the primary key column
can be assigned an AutoNumber.

6.) Create a new query and paste the following into the SQL View pane:

ALTER TABLE tblMyTable
ALTER COLUMN ID COUNTER
CONSTRAINT PrimaryKey PRIMARY KEY;

.. . . where tblMyTable is the name of the table your original make-table
query creates, and ID is the name of the primary key column. Save and run
the query.

7.) Create a new query and paste the following into the SQL View pane:

INSERT INTO tblMyTable
SELECT *
FROM tblTemp
ORDER BY ID;

.. . . where tblMyTable is the name of the table your original make-table
query creates, and ID is the name of the primary key column. Save and run
the query. Voila! You have the table of records you should have had in the
first place. But there's some more administrative work to do.

8.) Create a new query and paste the following into the SQL View pane:

DROP TABLE tblTemp;

Save and run the query, then compact the database.

While these steps aren't the most efficient method of creating a new table
and assigning a primary key, this technique works. It also shows you the
technique to create a single, empty table of the correct structure, add a
primary key, append a record for the proper seed value, and delete that
record. Okay. It doesn't show how to append a record or delete that record,
but I suspect you could do that in your sleep. ;-) Then you could change
your original make-table query into an append query and append these records
into the new table. I'll leave this exercise up to you, but if you have
trouble with queries, then stick with the steps I've outlined above.

And remember that AutoNumbers aren't guaranteed to be sequential. While the
numbers for these records appended into the final table will be sequential,
usage of this table will result in gaps in the numbers, so if you need these
numbers to be sequential, then don't use an AutoNumber data type.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact info.
 
G

Guest

Thanks for your help.

Your method has the advantage of being simple but the disadvantage of
requireing a manual intervention by the user to click on the tool bar and
select the compact database acommand.

I tried to execute this command using a macro but the command cannot be
executed while the database is open from within a macro. Your method is the
method I am presently using but I would like to automate it a little more.

I can also generate my invoice numbers very easily from within a report but
I don't know of a way to save the report generated numbers back into a table.
In the report I do the math from within a text box. Do you know of a way to
send the results of my invoice where I generte the number from some a
calculation within the report form back to a field in a table?
 
G

Guest

Thanks for your help.

I am going to try your solution.
It is a little scarry to me because I have never used SQL but if all the
steps are there I think I can dowhat you suggest.

It amazes me that such a simple task is so complicated.
 
6

'69 Camaro

Hi, Ruby.
Thanks for your help.

You're welcome.
It is a little scarry to me because I have never used SQL

If you're going to do any serious work with relational databases, SQL is a
must-have skill. It's far more powerful and flexible than the QBE Design
Grid.
It amazes me that such a simple task is so complicated.

If Jet were able to create an AutoNumber column with a seed value other than
1, it would require only one query to add the AutoNumber primary key to the
new table.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
 
J

John Vinson

I need to use a makequery in Access 2000 to build a table of customer's
invoices. I want to use the query to generate the invoice numbers based
upon a starting number I give it. If I could define a primary key field with
an autonumber I could use this field to increment from my starting number.
The problem is the only fields I can define are the ones that come from
existing tables and queries. i don't see how to define a field as a primary
key auto numbering field.

I'd like to "unask" the question, Ruby - or at least to point out that
it may be the wrong question to be asking!

It is *very very rare* that you need to use MakeTable queries. If the
purpose of the query is just to get a numbered list of customer
invoices, you don't need a new table *at all* - you can use a Report
based on a query selecting the invoices; you can get sequential line
numbers by using a textbox on the report with a Control Source of 1
(just the number one) and its Running Sum set to Over All (or to Over
Group if you're producing multiple customers' reports on one Access
report).

John W. Vinson[MVP]
 
G

Guest

Yes John, I do use the report to generate the invoice number now but I want
to sumarize the customer's past payment history, and invoices in his
statement which is a separately generated document.

I think it would be good to use a description in the statment line that
includes the invoice number. By using the report to generate the invoive
number at one point in time and then sending a statement out latter asking
for payment, I run the risk of generating a different number from the
orriginal invoice number when I include it in the statement. If I can
actually save the number with a short invoice description and invoice total
amount in a table then I can guarantee correlation.
 
J

John Vinson

Yes John, I do use the report to generate the invoice number now but I want
to sumarize the customer's past payment history, and invoices in his
statement which is a separately generated document.

I think it would be good to use a description in the statment line that
includes the invoice number. By using the report to generate the invoive
number at one point in time and then sending a statement out latter asking
for payment, I run the risk of generating a different number from the
orriginal invoice number when I include it in the statement. If I can
actually save the number with a short invoice description and invoice total
amount in a table then I can guarantee correlation.

I still don't understand how a MakeTable query comes into the picture.
If you have an Invoice table, you have an Invoice table and can use a
Custom Counter (much better than an autonumber) to generate invoice
numbers as needed, right there in the table. The Invoice table would
have a description field, and you'ld link to it as needed.

Could you explain why repeated MakeTables are needed? You're surely
not generating a new Invoice table for each customer, are you?

John W. Vinson[MVP]
 
G

Guest

I'm sorry but I don't know what a "Customer Counter" is.

Some more information that might help:
I have many customers.
Some customers buy product each month but not all.
Every customer who buys product gets an invoice listing that purchase/s
Every customer that has an outstanding bill or who paid off a bill during a
billing cycle gets a statement. Statements list balance owed at the begining
of the period, itemize any payments during the period, add any service
charges and manual account adjustments and list the new invoices with a toal
account balance at the end of the period.

Therefore, when I generate invoices for customers I automatically assign the
next available invoice number. Sometimes I need to skip a block of invoice
numbers. Sometimes I need to reisue the same number with a correction.
There might be other reasons why I want to have control over where the
invoice numbers start incrementing from too. I can have this control by
either using the method you described in the report or I can use an auto
indexing table that I then add the index to my starting invoice number. Each
month different customers get invoices so if I assign every customer an index
then I will skip invoice numbers. If I only assign customers who get an
invoice a number for that particular billing period then I have to keep track
of the number some way with the customer's account.

If I could not only generate the number in the report but then save it to a
table then my problem would be solved. If the make table query initialized
the index to 0 or 1 each time then when I append the cutomers who get
invoices to the table I would automatically get the proper increment from my
initial starting invoice number by adding the two together. The problem is a
make query does not allow an index to be made and a simple delete query on an
existing table does not restart the index.
If I delete and then compact the data base then I have to ell my user to
manually go to the tool bar and carry out this process plus Access closes my
form so I have to have a paper instruction telling him/her to re-open the
form and proceed with the next step. It works but it isn't very professional.

Now back to the "Customer Counter". Please elaborate a little about this
idea. Maybe I could use it to count invoices someway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top