Copying only headers in access - ms-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!

Related

MS Access - Table where all deleted items go to

I need a table where all deleted items from my main table go. I found the easiest way to do this is to copy my main table, paste the table and change the name. In my main table I went to the design tab, Create Data Macros, after delete. In the set field I put the name of the field, and for the value I put [Old].[ID] etc. My problem is this works great until I try to add in my "comments field" to the list. My comments field data type is "Long Text".
Somethings I have tried: deleting all calculated fields and adding them back in as short text (worked for some fields), Deleted all default values, deleted all validation rules.
Expected result would be for my deleted record from my main table to show up into the deleted records table. I receive no error message when deleting a record. It simply doesn't add to the to the deleted table. Thanks for looking.
Why copy tables? Create a yes/no field On your table called "Deleted." Then you can run a query for or against that field depending on if you want to see the deleted data or not. Add the field to existing queries that shouldn't show these, and set its criteria to false.

Lock table order when pasting

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.

use access form to edit data from a query result

I'm brand new to Access 2010, well to Access in general. I have a query that returns 10 columns (the query is called pending_review, the source is a table called escalations) in the results, some of those fields are blank since they have to be reviewed. One of the resulting columns is the primary key.
So this is my problem: I need a to create a form where I can type the primary key and the fields on the form will populate with the info from that row so I can fill the blank spaces with information that I now have and then update the table.Once the table is updated, that row should no longer show on the query results.
What I have done is that I created the form, but I don't know how to do the part of typing the primary key and everything gets populated. A solution would be to make a combo box that shows me the primary keys of the rows in the query results, so that every time i edit something in one of those fields for the query to be run again and reduce the number of options in the combo box.
Question is, how do i do that? or is there an easier and better way to do it?
Thanks in advance!!
Can you not just create a new form using the pending_review query as a data source? Access makes it very easy--you can just use the form wizard. This gives you a form in which you can edit one row at a time. Presumably the pending_review query has a filter condition that checks for certain fields being blank (null) or not. If you finish editing a result row in the form, and refresh the form, the row should then disappear from the form because it will no longer be returned by the pending_review source query.
Even if you need to do something more complex, this should at least be a good start.

How do I populate a field with static header row information on import?

As it stands, I am currently looking to import data from an Excel spreadsheet into a table on a monthly basis. The header row in the spreadsheet contains the date that the original query was run.
I have one master table in access consisiting of multiple files. I would like to set up an automated process to capture the date in the header upon import, and then record it in a field for every new record that was imported.
There are two caveats here:
Spreadsheet sizes will vary depending on where data exists.
I have no control over how the data is provided. Fields that contain no data for the month will not populate to the spreadsheet.
Less frequently fields will be added that do not exist.
So far I have been identifying these new additions manually and creating a new field for them at the end of the field list. I realize that this is very inefficient and I would like to automate it, if I can.
Does anyone have any insight? Any assistance would be greatly appreciated.
OK, here's the steps you'll want to take.
Create a link from Access to your Excel spreadsheet. Access will now see this as a table.
Create a make table query using the Excel table as the source and adding the date (derived from a sub-query) as an additional field.
Then run the query. This will automatically create all the fields.
If, however, you need to create new fields in an existing table, then you'll have to use VBA, read each header in the Excel table, compare it to the schema of the existing table, and execute an alter table query to add the field.
Good luck

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.