Lock table order when pasting - ms-access

I'm a Windows Access newbie. However, I have to make some changes to our database at work.
I've created a new list of records with Excel, sorted as follows:
101A
102A
102.01A
102.02A
102.03A
103A
103.01A
...
When I copy & paste it in my access table, MS Access 2007 keeps my custom order until I restart the program. When I reopen it, the table gets sorted this way:
101A
102.01A
102.02A
102.03A
102A
103.01A
103A
...
How can I avoid this? Is there a way to "lock" my custom order when pasting from Excel to Access?
These records are being shown in a dropdown menu. but I'm not able to "code" in Access, I just have to change these table records by pasting them in the exact order I've established in my Excel table.

A database doesn't work like an Excel sheet.
If you select rows from a table (either opening the table directly, or using it as row source for a dropdown box), the rows are always returned in a certain order.
If you don't specify the order (ORDER BY ... clause), the primary key determines the order. In your case, this data column is probably the primary key.
And since . is sorted before letters (see Ascii table), you get the result you see.
To avoid that, you would need an additional column, e.g. "Sortnumber", where you can define the sorting you want, and use in a ORDER BY clause.

Yes, I solved creating an ID column and setting it as primary in the ORDER BY ... clause. Thanks.

Related

Access Records/Rows disappearing from lookup table

Access 2013
I have a simple lookup table that twice in the last month a record (different record on each occasion) has disappeared.
Since the auto id of the table is used as the foreign key I am forced to drop the table and recreate it.
I don't provide any delete functionality at all to users in the Access DB and especially not to the lookup table.
I have done a global search for all references to the table in my VBA code and found it was used several times but only in select statements.
At the moment I suspect a user is fiddling with something they don't fully understand and they are inadvertently deleting the record.
What else can I do determine the cause of this problem?
Can I make the lookup table read only?
Look at the relationships window. Look at the linked tables in question and see if there is a 'Cascade Deletes' checked. If so, deleting one record will delete related records.
It turned out to be a second lookup table that had a temporary relationship defined in the query that was not required. i.e: no columns were based upon this unnecessary table. This unnecessary lookup table was simply being used as a row source for a combobox on the form.
I discovered this after I noticed the form data type was set to Dynaset (Inconsistent Updates). Why? When I tested the form with normal Dynaset the form refused to perform any updates!
When I removed the unnecessary lookup table from the query I was able to set the data type back to Dynaset with updates continuing to work and deletions no longer cascading through to my first and required lookup table.
In addition the unnecessary lookup table was actually a query that was referencing a table in another database.

Excel 2013: table generated by connections switch original table rows

Hi I have a table that is simply a copy of another table on another sheet on my workbook.
The second table is generated using
DATA -> Existing Connections-> and then I select the Table tab.
Usually this work really well but in some cases (not always) the new table has the rows in a different order from the original one. How is it possible to avoid this?

Copying only headers in access

I am new to access and I was wondering how i can only copy the headers from access to excel. There is too much data in the access table and I only need the headers. Thanks!
Filter your table so it doesn't show any data (e.g. right-click in a text column and filter for "Equals: qwerqwerqwer". Then you can copy&paste the empty table.
As pointed out by Wayne (thanks!), this doesn't actually work - Access doesn't copy an empty table.
You need to have at least one record, so the best method is to right-click into the primary key column of any row, and choose Equals <the current value>. This will leave you with one record, now you can Ctrl+A to select all and Ctrl+C to copy.
Create a query for the table that selects all columns, but filters on a unique value for one row. Below is a sample query that will return one row of data with headers. If you dislike the one row, then I think you will be writing some VBA code to get the field names.
SELECT tblLog.eDate, tblLog.eTime, tblLog.Form,
tblLog.User, tblLog.Detail, tblLog.ID
FROM tblLog
WHERE (((tblLog.ID)=1));
You could possibly select all the fields one at a time from the Fields collection of a table, write that to an array and export the array to an Excel spreadsheet.
Or, you could create a new query, go into the SQL view and write:
SELECT Top 1 *
FROM MyTableName
It will give you only the first record of the table. Copy/Paste as text that to an Excel spreadsheet and delete the first row. Voila!

Why are my records not being inserted in the same order they are being executed?

I'm using Microsoft Office Access as my DBMS and I'm using VBA to write my code for this project.
I'm doing data scraping for items on a website and I encountered something that appeared odd to me after I had inserted my data into a table.
In my code I use a loop to iterate through and collect all the items that the website has to offer. Once I have all the data for one item I insert it into my table and then move on to the next. There's 14,724 items that I need to insert into my table. If I iterate over all of them, they will be added to the table but they are out of order once I look at them in the table, even though all the items are there, however if I adjust the loop to only collect...let's say only the first 10 items then they will appear in the same order in which they were collected which is the same order they appear in the source code for the website.
It is important to note that my table does not have an id field because it is not required as there's one other field that serves as a unique identifier for an item in the table.
This does not seem like a big issue but I'm curious as to why this happens. Is there some kind of limitation when using MS-Access as your DBMS?
Any insight is greatly appreciated.
Thank you.
A table is not a spreadsheet.
This is by design of any relational database engine. Records in a table have no order other than what you eventually assign or apply.
If you want to sort the data (ascending or descending) use a Query. The Table doesn't have any order. Even the Fields don't have any relevant order.

How to restart counting from 1 after erasing table in MS Access?

I have table in MS Access that has an AutoNumber type in field ID
After inserting some rows, the ID has become 200
Then, I have deleted the records in the table. However, when I tried to insert a new row, I see that the ID starts with 201
How can I force the ID to restart with 1, without having to drop the table and make new a new one?
In Access 2010 or newer, go to Database Tools and click Compact and Repair Database, and it will automatically reset the ID.
You can use:
CurrentDb.Execute "ALTER TABLE yourTable ALTER COLUMN myID COUNTER(1,1)"
I hope you have no relationships that use this table, I hope it is empty, and I hope you understand that all you can (mostly) rely on an autonumber to be is unique. You can get gaps, jumps, very large or even negative numbers, depending on the circumstances. If your autonumber means something, you have a major problem waiting to happen.
In addition to all the concerns expressed about why you give a rat's ass what the ID value is (all are correct that you shouldn't), let me add this to the mix:
If you've deleted all the records from the table, compacting the database will reset the seed value back to its original value.
For a table where there are still records, and you've inserted a value into the Autonumber field that is lower than the highest value, you have to use #Remou's method to reset the seed value. This also applies if you want to reset to the Max+1 in a table where records have been deleted, e.g., 300 records, last ID of 300, delete 201-300, compact won't reset the counter (you have to use #Remou's method -- this was not the case in earlier versions of Jet, and, indeed, in early versions of Jet 4, the first Jet version that allowed manipulating the seed value programatically).
I am going to Necro this topic.
Starting around ms-access-2016, you can execute Data Definition Queries (DDQ) through Macro's
Data Definition Query
ALTER TABLE <Table> ALTER COLUMN <ID_Field> COUNTER(1,1);
Save the DDQ, with your values
Create a Macro with the appropriate logic either before this or after.
To execute this DDQ:
Add an Open Query action
Define the name of the DDQ in the Query Name field; View and Data Mode settings are not relevant and can leave the default values
WARNINGS!!!!
This will reset the AutoNumber Counter to 1
Any Referential Integrity will be summarily destroyed
Advice
Use this for Staging tables
these are tables that are never intended to persist the data they temporarily contain.
The data contained is only there until additional cleaning actions have been performed and stored in the appropriate table(s).
Once cleaning operations have been performed and the data is no longer needed, these tables are summarily purged of any data contained.
Import Tables
These are very similar to Staging Tables but tend to only have two columns: ID and RowValue
Since these are typically used to import RAW data from a general file format (TXT, RTF, CSV, XML, etc.), the data contained does not persist past the processing lifecycle
I think the only ways to do this is outlined in this article.
The article explains several methods. Here is one example:
To do this in Microsoft Office Access 2007, follow these steps:
Delete the AutoNumber field from the main table.
Make note of the AutoNumber field name.
Click the Create tab, and then click Query Design in the Other group.
In the Show Table dialog box, select the main table. Click Add, and then click Close.
Double-click the required fields in the table view of the main table to select the fields.
Select the required Sort order.
On the Design tab, click Make Table in the Query Type group. Type the new table name in the Table Name box, and then click OK.
On the Design tab, click Run in the Results group.
The following message appears:
You are about to paste # row(s) into a new table.
Click Yes to insert the rows.
Close the query.
Right-click the new table, and then click Design View.
In the Design view for the table, add an AutoNumber field that has the same field name that you deleted in step 1. Add this AutoNumber
field to the new table, and then save the table.
Close the Design view window.
Rename the main table name. Rename the new table name to the main table name.
I always use below approach. I've created one table in database as Table1 with only one column i.e. Row_Id Number (Long Integer) and its value is 0
INSERT INTO <TABLE_NAME_TO_RESET>
SELECT Row_Id AS <COLUMN_NAME_TO_RESET>
FROM Table1;
This will insert one row with 0 value in AutoNumber column, later delete that row.