Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Friday, July 29, 2011

Dynamic Dlookup in Query Column

Introduction.

If we need only a single column value from a related table to incorporate into a Microsoft Access Query then we can use the Dlookup() function to bring that value into the Query Column. Normally we place that Table also into the Query's design surface, establish the link between the Primary Key of the main table and the Foreign Key of the other table and place the required field in the query column. Here, we will examine an interesting solution to a small problem with a Dlookup() function in a query column. The above method that normally we follow has some issues when implemented in the following situations, which we are going to explore.

The DLookup(“TaxRate2005”,”tblTaxRates”,”[Product Code] = ‘” & [ProductCode] & “’”) Function is used in a Query Column to pick up Sale Tax Rate from the tblTaxRates Table, for each item sold, for calculating the Sale Tax Value for the actual Sale value.  The Tax Rate will change every year during the annual budgeting of the government.  The Company maintains the history of these changes and adds a new tax rate field with Year as Suffix to the tblTaxRates Table like TaxRate2005, TaxRate2006, TaxRate2007, etc.  Take a look at the sample image given below:

The Query Column name is TaxRate where the Dlookup() Function is inserted, irrespective of TaxRate2005 or TaxRate2006 or TaxRate2007 is selected in the function parameter like: TaxRate:Dlookup(“TaxRate2005”,”tblName”,”Criteria”).  When we design a report we can use the report control name as TaxRate and no need to modify it every time.

Check the sample Query result image given below:


Applying Different Tax Rates

Now, assume that there are Orders placed during December with the applicable Tax Rate at that time. There are Orders placed for materials immediately after the budget implementation with a revised sales tax rate.  We have to prepare some invoices with the previous year's sales tax rates and others with current-year rates.  That means the Dlookup() function will look like Dlookup(“TaxRate2005”,” “,” “) for a previous year and Dlookup(“TaxRate2006”,” “, “ “) for the current year.  The table name and criteria parameters are omitted for clarity. As you can see the first parameter of the function must be changed every time for printing previous year and current year invoices.

We cannot ask the User to go in and change the Query design.  There must be a way to define that particular parameter of the function dynamically to refer to different tax rate columns on the tblTaxRates.  The solution is very simple, design a small Form with a TextBox and a Command button and let the user type the tax rate field name in the TextBox (or create a Combo Box if required) and click the Command Button to refresh the Query and open the Report that uses this query as the record source.  A sample image of the Query parameter Form is given below:

The Textbox name Tax on the above form.  With that in mind,;lkjh3 our Dlookup() function in the Order Details Query column will look like as given below:

TaxRate:DLookup([Forms]![InvoiceParam]![Tax],”tblTaxRates”,”[ProdCode]= '” & [Product Code] & “'”)

In the criteria part, the ProdCode field of the tblTaxRate table should match with the (Product Code) field of the Products Table linked to the Order Details Table to return the tax rate value for each item on the Order Details table.

The sample SQL of the Query.

SELECT [Order Details].[Order ID],
 Products.[Product Code],
 Products.[Product Name],
 [Order Details].Quantity,
 [Order Details].[Unit Price],
 [Quantity]*[Unit Price] AS SaleValue,
 Val(DLookUp([Forms]![InvoiceParam]![Tax],"TaxRates","[ProdCode]='" & [Product Code] & "'")) AS TaxRate,
 [SaleValue]*[TaxRate] AS SaleTax,
 [SaleValue]+[SaleTax] AS TotalSaleValue
FROM Products INNER JOIN [Order Details] ON Products.ID = [Order Details].[Product ID];

The Command Button Click Event Procedure can run the following code to open the Sales Invoice Report after refreshing the change on the Form:

Private Sub cmdRun_Click()
Me.Refresh
DoCmd.OpenReport "SalesInvoice", acViewPreview
End Sub
Technorati Tags:

No comments:

Post a Comment

Comments subject to moderation before publishing.

Powered by Blogger.