Excel Spreadsheet to track and create joins - excel

I am writing a lot of SQL code at the moment - something I haven't done in a long time and getting frustrated at time spent working out relationships between tables.
I was thinking it might be a good idea to create in excel a list of the tables and how they join to other tables. Then you can simply use a drop down to select table1 and table2 and use a vlookup or similar to find how they link and generate the join.
I started but it occurred to me that someone may have already done this in an elegant way.
My process thus far has been to create a table in excel as per the table below. Then I can either have a dropdown or use a filter to find the appropriate relationship and get it to create the code for the required join:
Table1 |Table2 |Table1_Field |Table_ Field |Join Type
cnsmr |cnsmr_accnt |cnsmr_id |cnsmr_id |inner
cnsmr_accnt |UDEFGENERAL |cnsmr_accnt_id |cnsmr_accnt_id |inner
cnsmr_Accnt_Tag |cnsmr_accnt |cnsmr_accnt_id |cnsmr_accnt_id |inner
wrkgrp |cnsmr |wrkgrp_id |wrkgrp_id |inner
I can then use a formula like this (looking up on a list of tables to get the table abbreviation:
=" from " & A2 & " " & VLOOKUP(A2, 'List of Tables'!$A$2:$B$115, 2, FALSE)& " " & E2 & " join " & B2 & " " & VLOOKUP(B2, 'List of Tables'!$A$2:$B$115, 2, FALSE) & " on " &VLOOKUP(A2, 'List of Tables'!$A$2:$B$115, 2, FALSE) & "." &C2 & " = " & VLOOKUP(B2, 'List of Tables'!$A$2:$B$115, 2, FALSE) &"." &D2

Related

merge date text in power query

i have sheet including a start date & sf id and I need to combine them in one column as a reference but when I try to do this I found an error as shown in the P.S, So can I solve this error
Try using
= Text.From([Date]) & Text.From([Person ID])
or perhaps
= Text.From([Date]) & " " & Text.From([Person ID])
If the field is not text, you can not combine it using a & unless you wrap it with Text.From()

"Data type mismatch in criteria expression" in VBA ACCESS SQL

I am trying to fetch data from my Access table into Excel sheet. The below mentioned SQL query is throwing an error "Data type mismatch in criteria expression" while the execution reaches Open query.
I googled for all possible fixes. All are saying the number might be passed in quotes. i checked for it still clueless, The query is pretty simple where i am trying select data from a userform text box in DD-MMM-YYYY format (03-OCT-2018) and comapring with date portion of timestamp field and order by my customer id field
SELECT * FROM Master_Intrpay_Cash where DateValue(LAST_UPDATE_TIMESTAMP)>=#" & Trim(startdate.value) & "# and DateValue(LAST_UPDATE_TIMESTAMP)<=#" & Trim(enddate.value) & "# ORDER BY CUSTOMER_ID
Below Shown is the msgbox showing the query being passed. if it helps.
Also the crazy part is the above query was copy pasted from an existing working query, just changed the table name and timestamp field name. Rest everything is exactly the same.
Try without DateValue:
SELECT *
FROM Master_Intrpay_Cash
WHERE LAST_UPDATE_TIMESTAMP >= # " & Trim(startdate.value) & " #
AND LAST_UPDATE_TIMESTAMP <= # " & Trim(enddate.value) & " #
ORDER BY CUSTOMER_ID
DateValue expects a String as an argument, your column as Date/Time
Also, a preferable format for the date is #mm/dd/yyyy# (US date format), otherwise you may have problems with different locales.

updating multiple MS ACCESS tables with 1 spreadsheet

I have a MS Access database with about 1000 tables. The Tables are independent of each other, that is they are not linked to each other. Basically each table contains records of stock of a particular good. The Names of each table is the name of the item, for example 125 gm Soap, 200 ml Oil, etc. and contains the following columns : -
Date
Daily Opening Stock
Daily Sales
Daily Purchases
Daily Closing Stock
Each day, sales/purchases of all the goods are recorded in accounting software. The End of Day report summary is generated in Excel Format. This report is a consolidated report of daily Sales/Purchases of all the goods, row-wise. The Columns in Excel are :
Date
Item Name
Daily Opening Stock
Daily Sales
Daily Purchases
Daily Closing Stock
Presently I have to manually open each table in Access & copy-paste the respective daily sales/purchases data from the excel report. I want to automate this process so that the record is automatically appended to each of the respective table, basis the Item Name.
In summary, I want to update multiple tables in Ms Access database file, from data contained in 1 Ms Excel spreadsheet.
Is that possible ? Your help would be highly appreciated.
Thanks,
Thiru V. Klack
My first question to you is why can't the Access database match the data structure of the Excel spreadsheet? Updating 1 Access table from 1 spreadsheet is a lot simpler than what you're trying to do. Having 1,000 tables with the same data structure, where each table is named after a product, seems pretty fishy to me.
That being said, if you need to keep your 1,000 tables (I'm honestly surprised you're not hitting the DB maximum threshold for Access), there are ways you can make it work. I would suggest having a lookup table that matches the product (as it appears on the spreadsheet) to the table in Access it goes to. Then, you'd import the Excel data into a staging table, and run some VBA code to loop through the lookup table, running a query to append all the data from Excel into the appropriate Access table. Something like this:
private const STAGING_TABLE_NAME as string = "tDataFromExcel"
private Const LOOKUP_TABLE_NAME as String = "tGoodsToTableNames"
public sub AddData()
Dim db as DAO.Database
Dim rsItemLookup as DAO.Database
Dim strSQL as string
Dim GoodName as string, TableName as string
set db = CurrentDB
set rsItemLookup = db.OpenRecordset("select good_name, table_name from " & LOOKUP_TABLE_NAME)
do while not rsItemLookup.EOF
GoodName = rsItemLookup.fields("good_name").Value
TableName = rsItemLookup.fields("table_name").value
strSQL = _
"insert into " & TableName & " (" & _
"[date], " & _
"[Daily Opening Stock], " & _
"[Daily Sales], " & _
"[Daily Purchases], " & _
"[Daily Closing Stock]) "
"select " & _
"[date], " & _
"[Daily Opening Stock], " & _
"[Daily Sales], " & _
"[Daily Purchases], " & _
"[Daily Closing Stock]) " & _
"from " & STAGING_TABLE_NAME & " " & _
"where [Item Name] = """ & GoodName & """"
db.Execute strSQL
rsItemLookup.MoveNext
loop
end sub
Again, I'm certainly not advocating you take this approach: If you can, I would strongly implore you to refactor your database to not have 1,000 virtually identical tables.
Zack's answer provides a way to do what you're asking, but recommends that you do something else. I'm going to try to demonstrate that what he recommends would work for your situation, and show you how to achieve it.
You want to transfer data in an excel spreadsheet into a relational database that holds the same data - usually to give better access/manipulation. The data is in a big table of one day, with a separate line for each product, and you want to be able to view the entire history of a single product, so you have separated out the tables.
This is going to significantly slow down the automatic transfer process - that long loop that re-writes and processes the SQL over and over. You can look at the data in that way without this setup though. Lets call this tblProductDetails
One table has your product information - things like the name of the product, how much it costs... The other has the in/out records of what was bought and sold, keeping track of stock on hand at any point. This second table is going to have a LOT of records, as it's a new record for each product that was bought and/or sold each and every day. We might call this tblInOut.
With a Select query you can save in Access, you can look at the history of any one product at any time - you can even set this as a variable so that when you open the only query you save it prompts you for the name of the product you want to look up.
I've mocked up a 4 product sample schema for you with a query to show all the data, and another to show just the one product's history. You can use the second query exactly as is in Access' query builder (SQL view) where '125g Soap' (on the second last line) is mentioned in the query name so you know which query is which.
If you want to use just one query, do the same thing with one exception: on the second last line, where it says '125g Soap' replace the whole thing with Product_Name (without the inverted commas) and when you open the query, Access will give you a message box asking you to type in a product name.
This feature will only work if you type the name exactly right though, so you might prefer just to build a form so you can give the user a combo box with which to select the exact product name.

Extracting Data from SQL Server Using VBA-Excel with Dates Filter - Not Working

I am new to Excel VBA. I used VBA to extract data from SQL-Server and was able to extract data using text filters but when I started filtering by date, it didn't work anymore. I think my mistake was the formatting of date. I tried using all the samples on this site but I'm beginning to be hopeless. It would be great if you guys can assist me with this hurdle. Thanks a million.
Here's the code: (Please take note that I am skipping the connection and close codes to focus only on the problematic source code).
What I want to achieve here in this code is to extract all the records (using SQL query in a VBA environment) based on date criteria which is greater than or equal to January 1, 2018.
query = "SELECT (Worktype), count(*)" & _
"FROM dbo.InteractionSummary WHERE [Worktype] IN ('" & MAC & "')" & _
"GROUP BY [Worktype]"
With this query, I had no issue. It has generated the figures perfectly. What these codes do is to count all the records from "Worktype" column that has "MAC" value.
My issue is when I add the below date criteria.
Dim dTheDate As Date
dTheDate = #1/1/2018#
query = "SELECT (Worktype), count(*)" & _
"FROM dbo.InteractionSummary WHERE [Worktype] IN ('" & IMAC & "')" & _
"GROUP BY [Worktype] AND [StartDateTimeUTC] > " & dTheDate & " "
StartDateTimeUTC is a DateTime2 column (datetime2(3),notnull). I'm trying to extract only based on the dTheDate. I tried every format but it's not letting the code through.
Thanks,
Skywalker

Pass query as parameter into another query

I'm fetching data through an SQL statement in PowerQuery:
let
Source = Oracle.Database("sampleDB", [Query="SELECT * FROM mySampleTable WHERE CustomerID in (1,2,3,4,5)"])
in
Source
I need the SQL filter to be dynamic. So I have another query with a single cell containing a text string. For the example above, the single cell looks like => "1,2,3,4,5". This is why I want to build a dynamic SQL statement in PowerQuery that references that single cell from my other query.
I've tried the code below and other variants but none work!:
let
Source = Oracle.Database("sampleDB", [Query="SELECT * FROM mySampleTable WHERE CustomerID IN (" & MyReferenceQuery["SingleCell"] & ")"]
in
Source
How can I reference this other query?
If the text is in a single cell in a table, you need to access the value by specifying a column and a row-index, like below:
= Oracle.Database("sampleDB", [Query = "SELECT " & MyReferenceQuery[Column1]{0} & " FROM Table"])

Resources