1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
Private Sub btnCreateStatement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateStatement.Click Dim myConn As New MySqlConnection Dim myComm As New MySqlCommand Dim myAdapter As New MySqlDataAdapter Dim myData As New DataTable Dim strSQL As String Dim sEndDate As String Dim CustomerRef As String Dim current As Double Dim thirty As Double Dim sixty As Double Dim ninety As Double Dim onetwenty As Double Dim amount As Double Dim unallocated As Double Dim nBFBal As Double Dim nCFBal As Double CustomerRef = CustomerRefComboBox.Text sEndDate = Format(DateTimePicker2.Value, "yyyy-MM-dd") strSQL = " SELECT tr.TransID, tr.Date, trt.Category, trt.Descr, cz.CustomerRef, tr.Amount, SUM( tr.Amount ) AS TotalGroup, tr.Notes, " & _ "PERIOD_DIFF(CONCAT(YEAR('" & sEndDate & "'),IF(MONTH('" & sEndDate & "')<10,'0',''),MONTH('" & sEndDate & "')),CONCAT(YEAR(tr.Date),IF(MONTH(tr.Date)<10,'0',''),MONTH(tr.Date))) AS Days, " & _ "IFNULL( (Select SUM(AllocationAmount)FROM Transactions T1 LEFT JOIN TransactionAllocations TA ON TA.TransactionID = T1.TransID " & _ "LEFT JOIN Transactions T2 ON T2.TransID = TA.TransactionID_Allocation WHERE(tr.TransID = T1.TransID) AND T2.CustomerID = '3' ) * -1, 0) AS TotalAgainstCustomer, " & _ "IFNULL( (Select SUM(AllocationAmount)FROM Transactions T1 LEFT JOIN TransactionAllocations TA ON TA.TransactionID_Allocation = T1.TransID " & _ "WHERE tr.TransID = T1.TransID) * -1, 0) AS PaidAmount " & _ "FROM Customers cz, Transactions tr, TransTypes trt " & _ "WHERE (tr.CustomerID = cz.CustomerID AND cz.CustomerRef = '" & CustomerRef & "' AND tr.TransTypeID = trt.TransTypeID) " & _ "AND (tr.Date<='" & sEndDate & "') " & _ "AND NOT tr.TransTypeID IN ('RESOLVE DEBIT', 'RESOLVE CREDIT') " & _ "GROUP BY IFNULL( LinkTo, TransID ) " & _ "HAVING TotalGroup <>0 " & _ "ORDER BY tr.Date, tr.TransID LIMIT 0, 30" myConn = GetConnection() Try myConn.Open() Try myComm.Connection = myConn myComm.CommandText = strSQL myAdapter.SelectCommand = myComm myAdapter.Fill(myData) DataGridView1.DataSource = myData Dim myRow As DataRow For Each myRow In myData.Rows Dim myCol As DataColumn For Each myCol In myData.Columns nBFBal = 0 If Convert.ToDateTime(myRow.Item("Date")) >= DateTimePicker1.Value Then If myRow.Item("TotalAgainstCustomer").ToString() <> 0 Then nBFBal = nBFBal + Convert.ToDouble(myRow.Item("Amount").ToString()) Else nBFBal = nBFBal + myRow.Item("Amount").ToString() amount = myRow.Item("Amount").ToString() + myRow("PaidAmont").ToString() End If If myRow.Item("Days").ToString() <= 0 Then current = current + amount ElseIf myRow.Item("Days").ToString() = 1 Then thirty = thirty + amount ElseIf myRow.Item("Days").ToString = 2 Then sixty = sixty + amount ElseIf myRow.Item("Days").ToString() = 3 Then ninety = ninety + amount ElseIf myRow.Item("Days").ToString() = 4 Then onetwenty = onetwenty = amount End If End If Next nCFBal = nBFBal If myRow.Item("Date") < DateTimePicker2.Value Then Globals.Sheet64.TransactionDate.Value = myRow.Item("Date").ToString Globals.Sheet64.TransactionType.Value = myRow.Item("Category").ToString Globals.Sheet64.TransDescription.Value = myRow.Item("Descr").ToString Globals.Sheet64.TransDocument.Value = myRow.Item("Notes").ToString If myRow.Item("TotalAgainstCustomer") <> 0 Then Globals.Sheet64.stm_amount.Value = myRow.Item("Amount").ToString nCFBal = nCFBal + Convert.ToDouble(myRow.Item("Amount").ToString()) amount = Convert.ToDouble(myRow.Item("Amount").ToString()) - Convert.ToDouble(myRow.Item("TotalAgainstCustomer").ToString()) Else Globals.Sheet64.stm_amount.Value = myRow.Item("Amount").ToString nCFBal = nCFBal + Convert.ToDouble(myRow.Item("Amount").ToString()) amount = Convert.ToDouble(myRow.Item("Amount").ToString()) - Convert.ToDouble(myRow.Item("PaidAmount").ToString()) End If End If Next Catch myError As MySqlException MessageBox.Show("There was an error reading from the database: " & myError.Message) End Try Next Catch myError As MySqlException MessageBox.Show("Error connecting to the database: " & myError.Message) Finally If myConn.State <> ConnectionState.Closed Then myConn.Close() End If End Try End Sub