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 myDataAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim CustomersDataRow As oztech_testDataSet.CustomersRow = CType(CType(Me.CustomersBindingSource.Current, DataRowView).Row(), oztech_testDataSet.CustomersRow)
Dim strSQL As String
Dim sEndDate As String
Dim CustomerRef As String
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 = '14' ) * -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
myDataAdapter.SelectCommand = myComm
myDataAdapter.Fill(myData)
Catch myError As MySqlException
MessageBox.Show("There was an error reading from the database: " & myError.Message)
End Try
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
End Class
|