Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 All Powder Board and Ski SQL Server Workbook Chapter 6: Forms, Reports, and Applications Jerry Post Copyright © 2003.

Similar presentations


Presentation on theme: "1 All Powder Board and Ski SQL Server Workbook Chapter 6: Forms, Reports, and Applications Jerry Post Copyright © 2003."— Presentation transcript:

1 1 All Powder Board and Ski SQL Server Workbook Chapter 6: Forms, Reports, and Applications Jerry Post Copyright © 2003

2 2 Form Types StyleStyle DescriptionCategory Customer Last Name First Name Phone Address City Ski Board Style Sale CustomerSalesperson ItemIDDescriptionPriceQuantityValue Main Grid Main and Subform

3 3 Customer Main Form Record navigation Text box Label

4 4 Action Right-click the project, Add/Add New Item. Choose the Data Form Wizard. Enter the name Customer.vb Create a new connection if necessary. Move the Customer table to the right. Make sure all Columns are checked. Select option to display a single record. Fix the page layout. Add a button to the main form to open the Customer form with code. Test! If necessary, add password to the connection string.

5 5 Main Form Wizard Select table Add/Add New Item Data Form Wizard

6 6 Display Style Main form

7 7 Design View Properties Database connectionToolbox

8 8 Visual Studio Form Structure Form Dataset: Customer Order LastName FirstName OrderID OrderDate Database Data Adapter (Order) Fill/Load Update Data Adapter (Customer) Fill/Load Update

9 9 Action Double-click the Load button. Copy the code inside the subroutine. At the top of the code page, expand the region created by the designer. Find the New subroutine at the top. Find the comment ‘Add any initialization Paste the code after that comment. Save and test the form. Delete the Load button and its code.

10 10 Form Triggers Paste the code from the Load event Start button

11 11 Action Delete the text box for Gender. Drag a combo box from the ToolBox. Expand the Property window. Name it cboGender. Delete any entry in the Text property. In Items, enter Male, Female, Unidentified. Expand the DataBindings property. For Text and SelectedValue, expand and pick objdsCustomer – Gender. Save and test the form. Set the Tab Index for all controls.

12 12 Combo Box and Data Binding Combo Box Data Binding

13 13 Initial Grid Form

14 14 Action Add a new form with Add/Add New and the Data Form Wizard. Create a new dataset (dsSkiBoardStyle). Use the existing connection. Select the Grid layout. Add the password to the ConnectionString. Copy the code from the Load button to the New event. Add a new button to the Main form. Add the code to open the new form. Save everything and test the form.

15 15 Create the Grid Form Grid Form

16 16 Private Sub SetGridStyles() ' To control the grid layout and format you need a DataGridTableStyle Dim tableStyle As New DataGridTableStyle tableStyle.MappingName = "SkiBoardStyle" ' The table name ' Create a DataGridColumnStyle for each column Dim colStyle As New DataGridTextBoxColumn colStyle.MappingName = "Style" colStyle.HeaderText = "Style" colStyle.Width = 135 tableStyle.GridColumnStyles.Add(colStyle) ‘ Put the StyleDescription Column here Dim colStyleCategory As New DataGridTextBoxColumn colStyleCategory.MappingName = "Category" colStyleCategory.HeaderText = "Category" colStyleDescription.Width = 100 tableStyle.GridColumnStyles.Add(colStyleCategory) ' Add the tableStyle to the grid Me.grdSkiBoardStyle.TableStyles.Add(tableStyle) End Sub Grid Column Layouts Define the style for each column in the order you want displayed Create the style Add the style to the grid

17 17 Action Open the code behind the form and create a new subroutine. Copy the code from the example to create a table style and each column style. In the form’s New event under initialization, call the new subroutine. Test the code and debug it.

18 18 Add a Data Adapter for Lookup List SQL Data Adapter Create query Advanced options

19 19 Properties for Lookup Data Grid NamegrdCategory DataSourcedsProductCategory1 DataMemberProductCategory CaptionVisibleFalse ColumnHeadersVisibleFalse PreferredColumnWidth75 RowHeaderWidth0 ReadOnlyTrue LockedTrue VisibleFalse

20 20 Code for Lookup List: Startup #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call Try 'Attempt to load the dataset. Me.LoadDataSet() Me.SqlDataAdapter1.Fill(Me.DsProductCategory1) grdCategory.Visible = False Catch eLoad As System.Exception 'Add your error handling code here. 'Display error message, if any. System.Windows.Forms.MessageBox.Show(eLoad.Message) End Try SetGridStyles() End Sub

21 21 Lookup Code: Main Grid Const iColCategory = 2 ' Column for drop down list Private row As Integer Private col As Integer Private Sub grdSkiBoardStyle_CurrentCellChanged( _ ByVal sender As Object, ByVal e As System.EventArgs) _ Handles grdSkiBoardStyle.CurrentCellChanged Dim cell As DataGridCell = grdSkiBoardStyle.CurrentCell If (cell.ColumnNumber <> iColCategory) Then grdCategory.Visible = False Exit Sub ' Only care about last column End If row = grdSkiBoardStyle.CurrentRowIndex col = cell.ColumnNumber Dim rect As Rectangle = grdSkiBoardStyle.GetCellBounds(row, col) Dim tb As DataGridTextBox = grdSkiBoardStyle.Controls(2) tb.BackColor = System.Drawing.Color.Blue grdCategory.Left = rect.Left + grdSkiBoardStyle.Left grdCategory.Top = rect.Bottom + grdSkiBoardStyle.Top grdCategory.Visible = True End Sub

22 22 Lookup Code: Category Grid Private Sub grdCategory_Click( _ ByVal sender As Object, ByVal e As System.EventArgs) _ Handles grdCategory.Click Dim tb As DataGridTextBox = grdCategory.Controls(2) Me.objdsSkiBoardStyle.Tables("SkiBoardStyle"). _ Rows(row)(iColCategory) = tb.Text grdCategory.Visible = False End Sub

23 23 Action Add a SQLDataAdapter to the form. Retrieve the data from the ProductCategory table and uncheck the UPDATE option. Right-click the adapter to create a new dataset: dsProductCategory. Add a new data grid to the form and size it. Set the data grid properties. Add initialization code to load the dataset. Add subroutine to display the lookup list. Add subroutine to transfer data from the lookup list to the main dataset.

24 24 Grid with Lookup User clicks left of row to select.

25 25 Main/Subform by Sale The Data Form Wizard uses relationships to handle Parent and Child tables.

26 26 Action Create a new form with the Data Form Wizard. Add a new dataset with the existing connection. Choose the Sale and SaleItem tables. Create a parent child relationship with the SaleID column. Choose all columns except the SaleID in the SaleItem table. Select the single row option for the main form. Add the password to the ConnectionString. Move the Load button code to the New sub. Save and test the form.

27 27 Initial Form Design Sale form SaleItem grid

28 28 Dataset Designer to Add Column Add the Value column as an Element with the decimal data type

29 29 Action Open the dsSale.xsd dataset. Add an Element called Value with a decimal data type. Add the new column to the data grid with the TableStyles and GridColumnStyles properties. Name: objColumnStylegrdSaleItemValue Alignment: Right Format: 0.00 MappingName: Value ReadOnly: True Add the expression code. Call the expression code from the New subroutine.

30 30 Subform Value Column in Grid Open the TableStyles Open the columns Add a column Set the properties

31 31 Expression Code Public Sub CreateValueExpression() Dim tbl As DataTable = Me.objdsSale.Tables("SaleItem") Dim col As DataColumn = tbl.Columns("Value") col.Expression = "SalePrice*QuantitySold" End Sub Public Sub New() Try 'Attempt to load the dataset. Me.LoadDataSet() CreateValueExpression() Catch eLoad As System.Exception 'Add your error handling code here. End Try Me.objdsSale_PositionChanged() End Sub

32 32 Action Copy and paste the SalePrice column in the subform to create a Value column Assign properties so that it multiplies SalePrice by QuantitySold Copy and paste this new column to create a subtotal column Assign properties to make it a Summary calculation for Sum of the Value field Add subtotal and total fields to the Sale data block to display the subtotal and add the sales tax

33 33 Subform Subtotal Column Public Sub ComputeSaleTotal() Dim dr As DataRow Dim sum As Decimal = 0 Dim iSale As Integer = _ Me.BindingContext(objdsSale, "Sale").Position For Each dr In Me.objdsSale.Tables("Sale"). _ Rows(iSale).GetChildRows("SaleSaleItem") sum += dr("Value") Next editSubtotal.Text = sum.ToString("0.00") Dim dTax As Decimal = 0 If (Not IsNothing(editSalesTax.Text)) _ AndAlso (editSalesTax.Text.Trim <> "") _ Then dTax = CType(editSalesTax.Text, Decimal) editTotalDue.Text = (sum + dTax).ToString("0.00") End Sub

34 34 Calls to Compute Totals Private Sub objdsSale_PositionChanged() Me.lblNavLocation.Text = … ComputeSaleTotal() End Sub Private Sub grdSaleItem_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles grdSaleItem.Leave ComputeSaleTotal() End Sub Private Sub editSalesTax_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles editSalesTax.Leave ComputeSaleTotal() End Sub New Sale Leave grid Change tax

35 35 Action Add the routine to compute the subtotal. Add the three calls to perform the calculation. Modify the UpdateDataSource code to compensate for the Expression bug in Microsoft’s update code.

36 36 Code Changes for Update Bug Public Sub UpdateDataSource… if (Not (ChangedRows) Is Nothing) Then 'Open the connection. Me.OleDbConnection1.Open() 'Attempt to update the data source. OleDbDataAdapter1.Update(ChangedRows) Dim tbl As DataTable = Me.objdsSale.Tables("SaleItem") Dim exp As String = tbl.Columns("Value").Expression tbl.Columns("Value").Expression = String.Empty tbl = tbl.Copy() OleDbDataAdapter2.Update(tbl) Me.objdsSale.Tables("SaleItem").Columns("Value").Expression = exp ‘@OleDbDataAdapter2.Update(ChangedRows) End If Find this routine Comment this line out Insert 6 lines

37 37 Sale Form

38 38 Action Add a data adapter for CustomerList, creating a query for full name and phone. Add the fill command to the New sub. Add a combo box and assign the DataSource, DisplayMember, ValueMember and DataBindings: Selected Value. Create a similar data adapter and combo box for Payment Method.

39 39 Customer Query SELECT CustomerID, LastName + N', ' + FirstName + N' (' + Phone + N')' AS CustomerName FROM powder.Customer ORDER BY LastName, FirstName, Phone Combo Boxes only support two columns: Key Value and Display Remember to deselect the Advanced options to Insert/Delete/Update

40 40 Customer Combo Box Properties Dataset Display column Key column Bind to Form Be sure to add the Fill command in the New subroutine

41 41 Action Add a data adapter for the Item List lookup using the Inventory and Item tables. Create the dataset and add the fill command in the New sub. Add the data grid for the lookup list and set its properties. Add a new TableStyles entry and create entries for the ColumnStyles to set the display widths. Add the two sets of code to open the lookup list and transfer the selection to the dataset.

42 42 Code for SubGrid Lookup List Const iColCategory = 0 ' Column for drop down list Private row As Integer Private col As Integer Private Sub grdSaleItem_CurrentCellChanged( _ ByVal sender As Object, ByVal e As System.EventArgs) _ Handles grdSaleItem.CurrentCellChanged Dim cell As DataGridCell = grdSaleItem.CurrentCell If (cell.ColumnNumber <> iColCategory) Then grdItems.Visible = False Exit Sub ' Only care about last column End If row = grdSaleItem.CurrentRowIndex col = cell.ColumnNumber Dim rect As Rectangle = grdSaleItem.GetCellBounds(row, col) Dim tb As DataGridTextBox = grdSaleItem.Controls(2) tb.BackColor = System.Drawing.Color.Blue grdItems.Left = rect.Left + grdSaleItem.Left grdItems.Top = rect.Bottom + grdSaleItem.Top grdItems.Visible = True End Sub

43 43 Code for Data Transfer List Private Sub grdItems_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles grdItems.Click Dim tb As DataGridTextBox = grdItems.Controls(2) ' The next line index must be +1 because the ‘ subform grid has SaleID as a hidden column #0 Me.objdsSale.Tables("SaleItem"). Rows(row)(iColCategory + 1) = tb.Text grdItems.Visible = False End Sub This is the main difference when using a subform

44 44 Final Sale Form

45 45 SQL Server Report Structure Page header Page footer Body Customer Group Customer header Sale GroupSale header Item Detail Customer footer Sale footer

46 46 Action Create a new report project with SQL Reports Service. Add a new Shared Data Source to the Powder database. Add a new report—without using the Wizard. Create a query using Customer + Sale + SaleItem. Add the alias: QuantitySold*SalePrice As Value. In the layout tab, add a Report Header and a title test box. Drag a List box onto the report. Name: GroupCustomer. Grouping: =Fields!CustomerID.Value Drag customer fields onto the List and add labels. Preview the report.

47 47 Customer Sales Report Query Query Designer Right-click to add tables Create a new dataset Shared Data Source New report

48 48 Query: Calculated Value Column SELECT Customer.CustomerID, Customer.LastName, Customer.FirstName, Customer.Phone, Customer.EMail, Sale.SaleID, Sale.SaleDate, Sale.ShipCity, Sale.ShipState, Sale.PaymentMethod, SaleItem.SKU, SaleItem.QuantitySold, SaleItem.SalePrice, SaleItem.QuantitySold * SaleItem.SalePrice AS Value FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID ORDER BY Customer.CustomerID, Sale.SaleDate, SaleItem.SKU

49 49 Customer List Layout Toolbox or Fields List for Customer group Customer Fields/data Text box label Report headerLayout tab

50 50 Initial Preview Report header Data for one customer

51 51 Action Add a list for the Sale Group. Set Grouping: =Fields!SaleID.Value. Place Sale header items into list. Add a small list for SaleItem as the Detail, with no Grouping. Add the SaleItem boxes to Detail. Format Price and Value to 0.00. Add column heading text boxes in the Sale group, above the Detail list. Preview the report, clean up the layout, add separator lines.

52 52 Sales and SaleItem Groups Detail list has no Grouping property Column headings in GroupSale list Lines as separators

53 53 SaleItem Preview Customer header Sale header Detail Format: 0.00

54 54 Action Adjust the Customer and Sale list boxes to leave one row at the bottom of each. Drag the Value field into those spaces. Align it with the SaleItem Value to show the total. Set the format to $ #,##0.00. Add a Page Footer: Report/Footer. Add a text box and use an Expression to enter =Globals!PageNumber. Preview and adjust the report.

55 55 Final Layout Sale Footer Customer Footer Expression builder for page number Customer list Sale list

56 56 Final Report

57 57 Creating a Switchboard Form Add image with PictureBox Open forms and submenus with buttons Add report link with LinkLabel

58 58 Action Add an image or logo to the menu form (try clip art). Arrange the form buttons. Add a LinkLabel to open a report. Add the VisitLink subroutine to your code. Enter the code to call the VisitLink with the appropriate URL to open the desired form. Compile and test the links.

59 59 Open SQL Reports from Forms Sub VisitLink(ByVal lnk As LinkLabel, ByVal sURL As String) ' Change the color of the link text lnk.LinkVisited = True ' Call the Process.Start method to open the default browser System.Diagnostics.Process.Start(sURL) End Sub Private Sub lnkCustomerSalesReport_LinkClicked( _ ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles lnkCustomerSalesReport.LinkClicked Try VisitLink(sender, _ "http://localhost/Reports/Pages/Report.aspx?ItemPath=/Reports06/CustomerSales") Catch ex As Exception MessageBox.Show("Unable to open the report.") End Try End Sub

60 60 Action In the object navigator create a new entry in the Menus section and rename it (CustomerMenu) In the newly created Menus subsection, create a new entry and rename it Customers In the object navigator, in the same Menus sublist, add a new item and rename it CustomerSub Double click the icon to open the Menu Editor Add entries for AAMain, Customers, Close, and Help

61 61 Creating Menus Enter top-level options Add menu Right-click options Sub menu Rename each item

62 62 Action Drag a MainMenu onto the menu form. Add top-level links for at least Customers, Close, and Help. Under Customers, add the Customer and Sale forms, and the two reports. Rename each menu item. Double-click the Customer and Sale entries and add the code to open the forms. Add the code to open the two reports. Save, compile, and test everything.

63 63 Custom Menu Me.Close System.Diagnostics.Process.Start(sURL)

64 64 HTML Help Form Help File AllPowder.chm HTML Topic Properties: Help File: AllPowder.chm Help Context ID: 1 1 Topic A 2 Topic B 3 Topic C 4 Topic D 5 Topic E 6 Topic F compile

65 65 Action Create at least three HTML help files for the All Powder forms using an HTML editor or Wordpad. If necessary, download and install the HTML Help workshop. Create a new project in the workshop. Add the HTML files. Edit the HTML files to add keywords. Create the mapping file with a text editor and add it to the project. Set project options to build the TOC and index files. Compile and test the help file.

66 66 HTML Help Files <Object type="application/x-oleobject" classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e"> All Powder Board and Ski Shop Introduction to the All Powder Board and Ski Shop All Powder Board and Ski Shop sells and rents snowboards and skis for all levels of riders and skiers. The Board and Ski Shop Customers Sales

67 67 Topics Map File #define AllPowder100 #define Customers10000 #define Sales20000 Not required for SQL Server, but create it anyway so that you can use the same help file with other applications if necessary.

68 68 Help Compiler and File

69 69 Action Add a HelpProvider object to the main form. Set its HelpNamespace property to AllPowder.chm. Select the Form object, set the HelpNavigator property to TableOfContents For the Customer button, set the HelpKeyword to Customer and HelpNavigator to KeywordIndex. Write the code for the Help menu item. Compile and test everything.

70 70 Help File on Visual Basic Form HelpKeyword: Customer Keyword Index HelpProvider HelpNamespace: AllPowder.chm

71 71 Help Menu Code Private Sub mnuHelp_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles mnuHelp.Click ' Tell the Help system to open the Table of Contents Help.ShowHelp(Me.btnCustomer, "AllPowder.chm", _ HelpNavigator.TableOfContents, 0) End Sub Could also use HelpNavigator.KeywordIndex, “MainMenu”

72 72 Help In Action


Download ppt "1 All Powder Board and Ski SQL Server Workbook Chapter 6: Forms, Reports, and Applications Jerry Post Copyright © 2003."

Similar presentations


Ads by Google