Import Excel Spreadsheet into Existing MS Access Tables - excel

I have an Access database. Here is the setup for a few tables.
id - companyID (autonumber) PK, company (text), firstName (text), lastName (text)
category - companyID (number) combined PK with category, category (text)
salesmen - companyID (number) combined PK with code, code (text)
There is a 1-many relationship between id and category and between id and salesmen.
If I have a spreadsheet with columns of company, firstName, lastName, category1, category2, category3, salesman1, salesman2, how could I import the columns into the appropriate tables?
My first idea was to import the spreadsheet and then append company, firstName and lastName to the id table. Then I would join the imported spreadsheet with the id table to create a new table with all of the spreadsheet columns plus the auto generated companyID. Then I could append companyID and category1 to the category table. Then do the same for category2 and 3 and so on.
This seems really complicated if I have a lot of spreadsheets to import. Also, the person who will be importing the spreadsheets isn't a programmer, so she wants it to be as user-friendly as possible.
Is there a better way to import these spreadsheets?
Thanks!

What I would do is create another table to import the raw data into, then INSERT the data from there into the relevant tables.
DoCmd.RunSQL ("DELETE * FROM ImportDataTable;")
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "ImportDataTable", "C:\exceldata.xls"
The second line in Access VBA will import the data into the table called ImportDataTable (the ImportDataTable column names should be F1, F2, F3, etc.
Then use an append query (INSERT INTO) for each table that some of the ImportDataTable data needs to go in to. All this code can be put behind a button on a form so that the user(s) only need to press a button when new data is available.

Related

Add unique values to a column retrieved from multiple tables in PowerBI

I currently have two tables (Excel sheets) related to each other in PowerBI:
Inventory; columns (Article number, description, quantity, sumOfQuantityReceived)
MaterialsReceived; columns (Article number, quantityReceived, DateReceived)
The tables are related to each other with an one (Inventory) to many (materialsReceived) relationship, as shown below.
However, the Inventory table currently only shows the Article numbers that are present in the Inventory table and will not automatically add a new row with article number if there is a new one present in the MaterialsReceived table.
For example: The inventory list currently contains the following information
While there is a new article number present in the MaterialsReceived table (article number: 969686)
So my question is now: How can I create a new table in PowerBI that retrieves the unique article numbers from both tables and adds them to a new column.
In this situation, the new table would contain one column with 4 rows (456982, 456987, 556987 & 969686)
You can try below code
Uniq_Article_Number_Table =
FILTER (
DISTINCT (
UNION (
VALUES ( inventory[article number] ),
VALUES ( 'Material Received'[article number] )
)
),
[article number] <> BLANK ()
)

How to show last two rows only in tableview from sqlite using with QSqlQueryModel?

Below is my example code:
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('book.db')
db.open()
model = QSqlQueryModel()
model.setQuery("SELECT * FROM card")
self.tableView.setModel(model)
I am using QSqlQueryModel, Qtablevie, Sqlite3, and able to view all rows in my table. But i want to view only last two rows of my table which are newly inserted rows in to the table. The table has no "id" field and it has numaric and text fields. How is it possible?
Below is the table image:
If you want to get the last 2 elements ordered by any field that indicates the insertion order, in your case "rowid", then you have to use a filter in the SQL command like this:
model.setQuery("SELECT * FROM card ORDER BY rowid DESC LIMIT 2")
Another possible option is to filter the table using QSortFilterProxyModel but it is more inefficient.

How to import excel spreadsheet into access and cross reference two tables

I have the following two Access tables
Employees
id Name
1 bob smith
2 james bird
3 jane big
Events
id emp_id Notes
1 1 fell down the stairs
2 3 paper cut in the break room
I also have the following Excel file that I would like to 'suck' (import) into the Events table. The problem is the data needs to be correlated on the name/emp_id field and I'm not sure the best way to do this.
Excel_sheet
Employee Name Notes
bob smith feel asleep while driving
The access table uses references to the Employees table, whereas the Excel sheet is using names. What are some options for me to bring this Excel sheet into Events table and convert the names (bob smith) into their associated id's from the Employees table?
Assuming names are consistently spelled in both datasets and only one person exists for each name, try:
INSERT INTO Events(emp_ID, Notes) SELECT ID, Notes FROM excel_sheet INNER JOIN Employees ON Employees.Name=excel_sheet.[Employee Name];
Build that SQL in a query object or run in VBA:
CurrentDb.Execute "INSERT INTO Events(emp_ID, Notes) " & _
"SELECT ID, Notes FROM excel_sheet " & _
"INNER JOIN Employees ON Employees.Name=excel_sheet.[Employee Name];"
Suggest you test with a copy of database.
Name is a reserved word and really should not use reserved words as names for anything.

How do I stop Access from auto sorting my data on import?

I am importing an Excel spreadsheet into Access 2010. I created a Saved Import that will import a column of data intentionally out of order. The query I created needs to take the exact order of this data, return a value associated with it from a master table in our company's DB, then allow me to export both of these fields to Excel. I need to do this because I need to copy and paste the export on top of the values of another spreadsheet.
The problem is that when I import into Access the FG column is sorted by A-Z. This can be seen in both the table import and the results of the query. How do I keep my data in the mixed up order throughout the whole process?
Prepared Import sheet
FG
D
B
E
A
C
After importing
FG
A
B
C
D
E
Query
FG Description
A descript of A
B descript of B
C descript of C
D descript of D
E descript of E
To solve this problem, I had to use the option "Let Access create a primary key for me" when importing. This allowed the table to be populated with the data in the same order as was on the import spreadsheet. To make sure the query also keeps this same order, I had to include the new "ID" field as part of the results.
Final SQL Code
SELECT FGImport.ID, FGImport.FG, dbo_Active_Part_Number_List_Syteline.description
FROM dbo_Active_Part_Number_List_Syteline RIGHT JOIN FGImport ON dbo_Active_Part_Number_List_Syteline.item = FGImport.FG
GROUP BY FGImport.ID, FGImport.FG, dbo_Active_Part_Number_List_Syteline.description;
This may be an OLD post, but I would suggest opening Excel and creating a simple macro to enumerate the records before importing to Access; once you've imported the file, this enumartion (we'll call it "MacroField") will be part of the data set you import (We'll call this table "Table1"). You'll still retain the records original formatting by querying the record and ordering by Table1.MacroField asc/desc. There has to be a better way of doing it, but if you're in a pinch this does the trick.
Sub liminal()
For i = 1 To ThisWorkbook.Sheets.Count
If Sheets(i).Name <> "PrimaryTab" Then
ct = 0
Sheets(i).Activate
Range("B1000000").Select
Selection.End(xlUp).Select
en = ActiveCell.Row
For p = 1 To en
ct = ct + 1
Sheets(i).Range("A" & p).Value = ct
Next p
End If
Next i
Sheets("PrimaryTab").Range("A1").Select
MsgBox "Data Set, please open and load to MS ACCDB"
End Sub
If you want to import an Excel table (let's say with three columns) and you want the first row of the Access table (you are importing the data into) to have the Columns Title, then you create and Access table "myTable" with these fields:
F1 (Data Type: whatever)
F2 (Data Type: whatever)
F3 (Data Type: whatever)
Order (Data Type: AutoNumber)
In Access VBA you will use the following command:
ImportFile = "C:\myExcelFile"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "myTable", ImporteFile, False
When the Excel file is imported, the data will be alphabetical sorted by the first column; but the Field "Order" will have the sequential order of the data as in the Excel File. You can use a query on the table to sort the data by the field "Order", thus getting back the same data arrangement as in the Excel Table.

Reconciliation Ms Access records VBA and generate error message

I have got a requirement to reconciliation the records between excel sheet & MS access 2010 table with same data in different columns.. Need to validate the excel sheet based on the values from MS access with the help of unique id and to generate the error message for that unique id like below format in MS access reports.
Currently I have created one table based on below schema and mapped it to reports.. Now I need to compare the records and populate the error message in below table..
I'm new bie to VBA , Please help me with the best approach with no performance tolerance like avoiding much looping..
Table Schema: Employee
Employee ID, Employee Name, Employee Salary
1,Tester,5000
Excel Sheet Schema: Employee
ID, Name, Salary
1,Tester,4000
Required Output Report: Ms ACCESS 2010
File Name, Unique ID, Column Name, Difference
Employee, 1, Salary, access salary <5000> is different from excel sheet salary <4000>
You can achieve your desired outcome without using any VBA at all. Start by creating a Linked Table in Access that points to your Excel data. Detailed instructions can be found here:
Import or link to data in an Excel workbook
I named my Linked Table [ExcelData], so when I open it in Access it looks like this:
To perform the reconciliation all we need to do is create a query like this:
Note that the Field: value for the fourth column is
Difference: "Access salary <" & [Employee].[Employee Salary] & "> is different from Excel sheet salary <" & [ExcelData].[Salary] & ">"
For reference, the entire SQL statement for that query is
SELECT
"Employee" AS [File Name],
Employee.[Employee ID] AS [Unique ID],
"Salary" AS [Column Name],
"Access salary <" & [Employee].[Employee Salary] & "> is different from Excel sheet salary <" & [ExcelData].[Salary] & ">" AS Difference
FROM
Employee
INNER JOIN
ExcelData
ON Employee.[Employee ID] = ExcelData.ID
WHERE (((Employee.[Employee Salary])<>[ExcelData].[Salary]));
That query returns the following:

Resources