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

SQL/VBA add row number column

I have this table called: "ABC" and inside it are four fields: "SKU" (DataType: Short text), "VID" (DataType: Short text), "Cost" (DataType: Currency) and "Qty" (DataType: Number).
Expand|Select|Wrap|Line Numbers
  1. SKU    VID   Cost  Qty
  2. 123    545   $5    9
  3. 123    545   $5    25
  4. 145    441   $4    45
  5. 488    874   $7    4
  6. 474    457   $8    5
  7. 145    441   $4    47
  8.  
I want to add another field/column called "Line #" that shows the row number so the table will look like this:
Expand|Select|Wrap|Line Numbers
  1. Line #   SKU    VID   Cost  Qty
  2. 1        123    545   $5    9
  3. 2        123    545   $5    25
  4. 3        145    441   $4    45
  5. 4        488    874   $7    4
  6. 5        474    457   $8    5
  7. 6        145    441   $4    47
  8.  
I couldn't find any information on how to do this right because some rows have the same values.

Is there any way I can do this in either SQL or VBA?
Jul 8 '14 #1
8 2623
twinnyfo
3,653 Expert Mod 2GB
Is Line# (and PLEASE do not use "#" in the field name!) a unique identifier for each record? If it is, just add a Field to the Table, with AutoNumber Data type, and it will add numbers to all your records.

If this field is not a unique identifier, what is it used for and which rows would have a duplicate Line#?
Jul 8 '14 #2
It is a unique identifier but I don't like using AutoNumber because if I delete a row, than the number skips. Take this table for example:
Expand|Select|Wrap|Line Numbers
  1. LineNumber   B    C
  2. 1            cd   sd
  3. 2            cd   sd
  4. 3            ef   df
  5. 4            cd   gb
  6.  
Now if I want to delete line number 4, then add more rows here's what happens
Expand|Select|Wrap|Line Numbers
  1. LineNumber   B    C
  2. 1            cd   sd
  3. 2            cd   sd
  4. 3            ef   df
  5. DELETED
  6.  
Now that I deleted that row, let's say I want to add a row with values for B is "if" and for C it's "wq"
Expand|Select|Wrap|Line Numbers
  1. LineNumber   B    C
  2. 1            cd   sd
  3. 2            cd   sd
  4. 3            ef   df
  5. 5            if   wq
  6.  
It skips line 4 and goes to line 5.

If it's not possible to make a query or a module that creates [LineNumber] for me? Or should I just suck it up and enter each line in manually?
Jul 8 '14 #3
Seth Schrock
2,965 Expert 2GB
Are you wanting to add the field to the table or just in a query to get a row number in the query results?

**Edit** Sorry, I didn't refresh before posting, so I didn't see post #3.
Jul 8 '14 #4
twinnyfo
3,653 Expert Mod 2GB
Jrod,

There is nothing wrong with having missing row numbers. It happens all the time in databases. What happens if you were to change one of your items? Then you would delete it. Would you then renumber all of your other records, just because one of the items was deleted from the inventory?

I know, I am OCD myself, so having missing record numbers causes my head to screw into the ceiling, but I have learned to get over it. Don't over-complicate something that the DB will easily and happily uncomplicate for you.....
Jul 8 '14 #5
I'm not trying to sound rude or anything but I do need the line numbers to be in correct order. It has nothing to do with preference.

This table is connected to a report which I'd need to print out once this is complete. It needs to show the line number on the left side for each row.

Is there a way I can show the line numbers on a report without using a query?
Jul 8 '14 #6
Ok I just looked it up and I fixed it by creating an unbound textbox and in the property sheet under the "Data" tab I have:

Control Source =1

Running Sum Over All


Thanks for the advice and help
Jul 8 '14 #7
twinnyfo
3,653 Expert Mod 2GB
Jrod, no rudeness was taken. I hope you did not consider my comments rude, either. Sometimes they have to be in numerical order, and I can understand that necessaity from time to time.

I have done what you are asking before--since you are using it in a report, it would best be done in a query and not in a table, since there is no value in storing that value--if your inventory changes.

I have done this in the past, but my query is always sorting based on certain values, which is not what you have. I will have to do some lookinginto this one just a bit...
Jul 8 '14 #8
twinnyfo
3,653 Expert Mod 2GB
Ok. Glad you found an alternate solution.
Jul 8 '14 #9

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

Similar topics

14
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as...
0
by: chris | last post by:
What is the recommended way of storing 10 digit phone numbers? I like bigint(10), but it seems most are using varchar? Or I've also seen 2 int columns, one for area code and another for the rest...
8
by: Moondog | last post by:
I am referencing a datagrid item like this: strItemNo = grdItems.Item(grdItems.CurrentRowIndex(), 0) The Item Number is in the first column; column 0. It works fine except this is not a...
3
by: Ronald S. Cook | last post by:
I have tables in my database-based Web app like Player, Club, and Tournament. In each, I have an identity primary key column "ID" (1, 2, 3, 4.). I also have a column "Number" which is a...
1
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, I want to display a serial number before every row in the datagrid. How can I do that? Thanx for your help.
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
0
meLady
by: meLady | last post by:
Hello, I have a form called feedback which has three columns and each cell in a column has a combo box that holds (Yes/No/Empty), and I want to count each column's value for example let's supposed...
3
by: NewlytoSQL | last post by:
Hi all, im fairly new to SQL and im stuck half way through a query, im using DB2 here is what im tryng to do. i have a query that brings back an item number , shelf req, sum of all orders columns,...
3
dlite922
by: dlite922 | last post by:
This is again from the dynamic reporting module i'm building. Columns come in and are built based on XML doc and the HTML (smarty template) does not know weather this is a number to format it...
1
by: Tillu | last post by:
One of the column in a new table can be ename - varchar2(20) or hase_code_ename - number(11) – Hash Code generated by JAVA. We are going to create non-unique index on this column as one frequent...
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...
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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...
0
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...
0
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...

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.