I need to calculate weighted price index between 2 retailers (weighted on retailer 1 turnover). I need to calculate this index using different Item compositions (different top n).
Table1 contains data on area-week-item level.
The basic approach of weighting is:
SUM(price1/price2 * turnover)/SUM(turnover)
I calculate it with the following Query statement:
SELECT week, SUM(price1/price2 * turnover)/SUM(turnover) AS [PriceIndex]
FROM Table1
GROUP BY week;
According to the business needs I usually need to calculate this index with different set of products - only private label, only vegetables or - the most simple example - for only TOP n products (based on turnover in Q1'16).
I want to make a simple form in Access using VBA with several parameters for my query.
Form example
I have Dictionary tables with item-code descriptions (to select only beer category) and TOP 500 ranking table. I've made a query, that works - it JOINS top 500 table (it consists just code column with needed codes. I can manually write number of TOP codes (hilighted the string with ***).
SELECT week, SUM(price1/price2 * turnover)/SUM(turnover) AS [PriceIndex]
FROM Table1 AS F INNER JOIN TopTable AS T ON F.code=T.code
WHERE F.code IN (
***SELECT TOP 500 code
FROM TopTable)
GROUP BY week;
Now the main question. How to make the illustrated form?
I've made the beginning:
Private Sub Command_Click()
Dim top_num As Long
Dim SQL As String
top_num = Top_number.Value
sSQL = _
"SELECT week, SUM(price1/price2 * turnover)/SUM(turnover) AS [PriceIndex]" & _
"FROM Table1 AS F INNER JOIN TopTable AS T ON F.code=T.code" & _
"WHERE F.code IN ( " & _
"***SELECT TOP top_num code" & _
"FROM TopTable)" & _
"GROUP BY week;"
' ????
End Sub
I need just to execute SQL statement with form variable top_num.
So if you want to display the results of you query in your form, you have to do it in a subform.
create the Subform
create a new form, name it Subform1 or anything else you like. click on the square which is top left of the form and bring the property window. Under "Format", "default display", select "Datasheet".
Your query returns 2 columns, so you need to add 2 textboxes on your form
You have to bound your textboxes to the column names returned by your query: select a textbox and bring the properties window, under "data" tab the first field is "control source", there you specify your query column names : week for one textbox, PriceIndex for the other
Your Subform is ready, save and close it.
Add the subform to the form
Open the main form in design mode and add a SubForm control to it. When you create the control the wizard ask you which will be the data source for it, select SubForm1
Adapt the code
Private Sub Command_Click()
Dim top_num As Long
Dim SQL As String
top_num = Top_number.Value
sSQL = _
"SELECT week, SUM(price1/price2 * turnover)/SUM(turnover) AS [PriceIndex]" & _
"FROM Table1 AS F INNER JOIN TopTable AS T ON F.code=T.code" & _
"WHERE F.code IN ( " & _
"***SELECT TOP " & top_num & " code" & _
"FROM TopTable)" & _
"GROUP BY week;"
' Add This :
Me!subform1.Form.RecordSource = sSQL
End Sub
You're done
Related
Using MS Access, I have a table called "Locations" and a table called "Routes". I want to create a form that will basically present the routes table to the user (i.e select "from" and select "to") and will filter the "to" locations based on what the user has selected for the "from" location, i.e. a cascading combo box. The corresponding "Route.ID" will be stored in a resultant table on completion of the form.
Location has the following columns -
ID
Name
Notes
1
London
Great Britain
2
Manchester
Great Britain
3
Alabama
USA
Routes has the following columns -
ID
From_LID
To_LID
KM
Notes
1
1
2
450
Using the M1 route
2
2
1
450
Using the M1 route
3
1
2
485
Using the inside routes
4
2
1
485
Using the inside routes
5
1
3
5450
Too far to consider
6
3
1
5450
Too far to consider
I want to create a form with a cascading combo box - "ComboFrom", "ComboTo". Where "ComboFrom" will search the table for a list of distinct IDs, and using a JOIN with the Locations table, I can display the location names. "ComboTo" is dependant on the ID value in "ComboFrom" and will therefore only return a list of locations where it matches the From_LocationID of the routes table.
I have the first part more or less done. ComboFrom has the below query:
SELECT DISTINCT Location.Location_ID, Location.[Location Name], Location.Description
FROM Location INNER JOIN Route ON Location.Location_ID = Route.From_LID
ORDER BY Location.[Location Name];
ComboTo has the below query:
SELECT Location.Location_ID, Location.[Location Name], Location.Description, Route.From_LID
FROM Location INNER JOIN Route ON Location.Location_ID = Route.To_LID
WHERE (((Route.From_LID)=[Forms]![fmrRoute1]![From_LID]))
ORDER BY Location.[Location Name];
The piece of code in the "Where" clause in the ComboTo field basically gets the input of the ID from ComboFrom and outputs the correct IDs from the corresponding To_LID list. I then add vba against the update events for ComboFrom to requery ComboTo field.
What I am having trouble figuring out is how I can get the corresponding route ID to display correctly. i.e if I choose "1" for ComboFrom, and "3" for ComboTo, the Route ID should show "5" and not "7" (where "7" is a new record in the Routes table). Do I have to have a separate query to search for ComboFrom and ComboTo and return the resultant Routes ID? If so, how do I do this and attach it to the field in the form that will automatically update every time the ComboFrom and ComboTo fields change?
Is there a better way to do what I am trying to do?
Thanks in advance for any assistance!
This looks like cascading combo boxes into filtering a form from those two unbound combo boxes.
You can google those two ideas.
The self join made this tricky. The key was ignoring the relationship between Location.ID and From_LID. That relationship is in the database but ignored (deleted) in the query.
this gives us the sql we will need for the second combo box record source.
SELECT Routes.From_LID, Routes.To_LID, First(Locations.LocationName) AS FirstOfLocationName
FROM Locations INNER JOIN Routes ON Locations.ID = Routes.To_LID
GROUP BY Routes.From_LID, Routes.To_LID
HAVING (((Routes.From_LID)=2))
ORDER BY First(Locations.LocationName)
'just need to replace the 2
Private Sub cmbFrom_AfterUpdate()
'cascade combobox
Dim strRowSource As String
strRowSource = "SELECT Routes.From_LID, Routes.To_LID, First(Locations.LocationName) AS FirstOfLocationName" & _
" FROM Locations INNER JOIN Routes ON Locations.ID = Routes.To_LID " & _
"GROUP BY Routes.From_LID, Routes.To_LID " & _
"HAVING (((Routes.From_LID) = " & Me.cmbFrom & "))" & _
"ORDER BY First(Locations.LocationName)"
Debug.Print strRowSource
Me.cmbTo.RowSource = strRowSource
Me.cmbTo.Visible = True
'a look and feel choice
End Sub
[![enter image description here][2]][2]
Private Sub cmbTo_AfterUpdate()
'filter form 'most of the time you want to create a search form where you filter to the records you want
Me.Filter = "From_LID = " & Me.cmbFrom & " AND To_LID = " & Me.cmbTo
Me.FilterOn = True
Me.Detail.Visible = True
End Sub
I have an access database that I will be using to track orders and to track inventory levels. When I attempt to add the parts on my order form (sbfrmOrderDetails) to my inventory table (tblInventory) my VBA code does not execute as planned.
Please note that I have stripped down the code and the tables to just the relevant information/values. The code posted below does work, just not as intended. I explain in much more detail below.
Form Structure
I created my Order form (frmOrder) as a Single Form. This form is referred to in my later code to determine the order number using the txtOrderID control. When I link my subform, the linked master field is OrderID.
Within this form is my Order Details subform (sbfrmOrderDetails) as a continuous form. Every control is bound, and it is linked to the parent form. The linked child field is OrderID.
Photo 1: This photo may better illustrate my form:
Table Structure
The relevant tables I have are structured like so:
TableName: tblOrders
TableColumns: OrderID
TableName: tblOrderDetails
TableColumns: ID|InvID|Qty|OrderID|DeliveryStatus
TableName: tblInventory
TableColumns: ID|InvID|Qty|OrderID
Intended Action
The action I am trying to take occurs in the subform and is supposed to be isolated to the current record. When the user changes the ComboBox (Combo1 bound control to tblOrderDetails.DeliveryStatus), my VBA code will execute an 'INSERT INTO' SQL string that adds the InvID and the Qty from the current record into the inventory table (tblInventory).
VBA Code for Combo1 AfterUpdate Event (On sbfrmOrderDetails)
Private Sub Combo1_AfterUpdate()
Dim db As DAO.Database
Dim strSQL As String
Set db = CurrentDb
If Me.Combo1.Value = "Delivered" Then
strSQL = "INSERT INTO [tblInventory] ([InvID],[Qty])" _
& "SELECT " & Forms![frmOrder].Form![sbfrmOrderDetails].Form![txtInvID] & " AS InvID," & Forms![frmOrder].Form![sbfrmOrderDetails].Form![txtQty] & " AS Qty " _
& "FROM tblOrderDetails WHERE ((tblOrderDetails.OrderID)=(" & Forms![frmOrder]![txtOrderID] & "));"
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
Else
'Other event
End If
End Sub
Intended Results
When Combo1 (bound control) is changed from null to “Delivered” on record ID #11 only, it is supposed to add a single new record.
Photo 2: Intended Results:
Actual Results
When Combo1 (bound control) is changed from null to “Delivered” on record ID #11 only, it is adding a new record for every record populated in the subform.
Please refer to Photo 2 above to compare the Intended Results to the Actual Results.
You can see that the quantity from records 12 and 13 are transferred over under the InvID from record 11.
Please refer to Photo 1 to view the sample data and also to Photo 2 above to see the Actual Result of the code.
I suspect that since this is a continuous form that has Parent/Child linking, the form is running the VBA code once for every record (instead of one time for the current record).
Can I alter my VBA code to only run this code once on the current record as is intended? I am hoping this is the best approach to complete this task.
The output of Debug.Print strSQL would have been helpful (see How to debug dynamic SQL in VBA ), but it would be something like this:
INSERT INTO tblInventory (InvID, Qty)
SELECT 14 AS InvID, 2 AS Qty
FROM tblOrderDetails
WHERE tblOrderDetails.OrderID = 5
You are inserting two constant values, so you may as well use the INSERT ... VALUES (...) syntax, which by definition only inserts one record:
INSERT INTO tblInventory (InvID, Qty)
VALUES (14, 2)
The reason your statement inserts multiple records is because of WHERE tblOrderDetails.OrderID = 5. Multiple records (all on the subform) satisfy this clause.
You would have to specify the OrderDetails ID instead, to get only one record:
INSERT INTO tblInventory (InvID, Qty)
SELECT 14 AS InvID, 2 AS Qty
FROM tblOrderDetails
WHERE tblOrderDetails.ID = <Forms![frmOrder]![sbfrmOrderDetails].Form![txtID]>
tblOrders.OrderID --> this table has not been referenced in the statement and vba should throw an error
strSQL = "INSERT INTO [tblInventory] ([InvID],[Qty])" _
& "SELECT " & Forms![frmOrder].Form![sbfrmOrderDetails].Form![txtInvID] & " AS InvID," & Forms![frmOrder].Form![sbfrmOrderDetails].Form![txtQty] & " AS Qty " _
& "FROM tblOrderDetails WHERE ((tblOrders.OrderID)=
(" & Forms![frmOrder]![txtOrderID] & "));"
Debug.Print strSQL
Hello I am trying to use a drop down box on my form that will display two different record sources based on an if statement and can't get it to work. Is this possible??
Basically I want to show the codes and descriptions for the DX_Codes table If the date is less than 10/1/2015 and show the DX_Codes_ICD_10 if it is greater than or equal to 10/1/2015. The date is also a field on the same form.
IIf(Me.from_date < #10/1/2015#,
SELECT DX_Codes.dx_code, DX_Codes.dx_code_desc FROM DX_Codes ORDER BY DX_Codes.dx_code,
SELECT DX_Codes_ICD10.dx_code, DX_Codes_ICD10.dx_code_desc FROM DX_Codes_ICD10 ORDER BY DX_Codes_ICD10.dx_code);
You have to use VBA to manage a RowSource of a control like a combobox. Place the below behind some event trigger, possibly related with the from_date control.
If Me.from_date < #10/1/2015# Then
Me.ComboBoxName.RowSource = "SELECT DX_Codes.dx_code, DX_Codes.dx_code_desc" _
& " FROM DX_Codes ORDER BY DX_Codes.dx_code;"
Me.ComboBoxName.RowSourceType = "Table/Query"
Else
Me.ComboBoxName.RowSource = "SELECT DX_Codes_ICD10.dx_code, DX_Codes_ICD10.dx_code_desc" _
& " FROM DX_Codes_ICD10 ORDER BY DX_Codes_ICD10.dx_code);"
Me.ComboBoxName.RowSourceType = "Table/Query"
End If
I need to display the customers that have purchased a product (based on a user search) in a list box. I have five different tables in Access which store different information and that relate to each other with IDs (using combox boxes in vb). I need to be able to search for a product, for example "White Bread", the program then should display the customer's full name and address as stored in the database.
Table: TransactionDetails
Fields: ID, stockID, custTransID
Table: CustomerTransaction
Fields: ID, custID, dateOfTransaction
Table: CustomerAccountDetails
Fields: ID, custFullName, custAddress, custLandline,
custMobile, custDOB, custCreditDetails
Table: StockDescription
Fields: ID, stockName, stockDesc, stockPrice
Table: SupplierDetails
Fields: ID, supplierName, supplier Address
I think I need to use INNER JOIN to query multiple tables at once but I am unsure of the syntax (I'm new to SQL). So far I have this:
Dim productSearch As String
productSearch = productSrchInput.Text
Dim databaseConnection As New OleDb.OleDbConnection
Dim counter As Integer
Dim array(10) As String
databaseConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=assignment5database.accdb"
databaseConnection.Open()
Dim searchDatabase As OleDbCommand = New OleDbCommand("SELECT CustomerAccountDetails.custFullName, CustomerAccountDetails.custAddress " & _
"FROM CustomerAccountDetails " & _
"INNER JOIN StockDescription ON TransactionDetails.stockID = TransactionDetails.custTransID " & _
"WHERE StockDescription.stockName = '" & productSearch & "'", databaseConnection)
Dim searchResults As OleDbDataReader = searchDatabase.ExecuteReader
counter = 1
Do While searchResults.Read
srchResultsList.Items.Add(searchResults.Item(0))
counter += 1
Loop
databaseConnection.Close()
You are missing some of the joins that connect the customer to the stock details. Here is the SQL that Access will expect in order to pull the data based on your description. The parentheses may seem extraneous, if you are used to SQL server or MySQL, but Access will throw a fit if you leave them out.
SELECT CustomerAccountDetails.custFullName, CustomerAccountDetails.custAddress, StockDescription.stockName
FROM StockDescription
INNER JOIN ((CustomerAccountDetails
INNER JOIN CustomerTransaction ON CustomerAccountDetails.ID = CustomerTransaction.custID)
INNER JOIN TransactionDetails ON CustomerTransaction.ID = TransactionDetails.custTransID) ON StockDescription.ID = TransactionDetails.StockID
WHERE StockDescription.stockName="something"
As noted by Fionnuala, I will almost always build a query that has multiple joins in it using the Access query designer before putting it in code. I almost always leave out a set of parentheses or try to write the query in a structure that SQL Server would expect and get rejected by Access.
I think you could be able to use an inner join but perhaps a "union" may be more efficient.
http://www.w3schools.com/sql/sql_union.asp is good for improving sql knowledge, it helped me a lot.
I'm using SQL to code a combobox which will display names from one table (students), minus any students in my other table (Bookings) who have made a booking on a specific day.
In my "students" table I have one field, "Name". In my "Bookings" table I have two fields, "Name" and "Day".
Here is the code I am using to tell what is going in my combobox;
Combobox.RowSource = "SELECT Name FROM Students" & _
"LEFT OUTER JOIN Bookings " & _
"ON Students.Name = Bookings.Name" & _
"WHERE Bookings.Day = 'Monday';"
To my knowledge this should display all of the names in Students.Name minus any names in Bookings which are on the day Monday. But it's just opening an error message "Syntax error in FROM clause."
Thanks in advance for any advice.
Udate:
I found out how to make it work. Instead of a "Left Outer Join" you have to use a "Not In" clause.
Here' s the code I used.
combobox.RowSource = "SELECT Name FROM Students " & _
"WHERE Name " & _
"NOT IN (SELECT Bookings.Name FROM Bookings WHERE Bookings.Day = ""Monday"") "