ohthatlove
New member
- Joined
- Jan 27, 2011
- Messages
- 1
- Programming Experience
- Beginner
Hi all, I'm doing a assignment on World Sports Games in VB.Net (Excel).
I need to split the data according to its category to show the records from the database and show the changes in the blue box below.
I have manage to show all the records but i couldn't show the records according to category as there's errors.
Here's the code I've done so far and attached screenshots for reference.
I need to split the data according to its category to show the records from the database and show the changes in the blue box below.
I have manage to show all the records but i couldn't show the records according to category as there's errors.
Here's the code I've done so far and attached screenshots for reference.
VB.NET:
Imports System.Data
Imports System.Data.SqlClient
Public Class ThisWorkbook
Private Sub ThisWorkbook_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
Dim objCn As New SqlConnection
Dim objCmd As New SqlCommand
Dim strSQL As String = ""
Dim objDs As New DataSet
Dim objDr As DataRow
Dim objAd As New SqlDataAdapter
Dim objSheet As Excel.Worksheet
Dim objRange As Excel.Range
objSheet = Me.Worksheets(1)
objCn.ConnectionString = "Data Source=unknown;Initial Catalog=SportsDB;Persist Security Info=True;User ID=123;Password=#;"
objCmd.Connection = objCn
strSQL = "SELECT * FROM tblTeam,tblCategory WHERE "
strSQL &= " intCategoryTypeIdTE=intCategoryTypeIdCA AND strDeleteDateTE = '' ORDER BY strTypeCodeCA"
objCmd.CommandText = strSQL 'Assign the SQL to the CommandText property
objAd.SelectCommand = objCmd 'Tell the Adapter object to use the Command object, objCmd
objAd.Fill(objDs, "tblTeamData") 'Begin filling up the DataSet with member records
'The objRange will start representing the cell at row 2 col 2.
strSQL = "SELECT * FROM tblCategory"
objCmd.CommandText = strSQL
objAd.Fill(objDs, "tblCategoryData")
'Begin from row 1 column 11 (K1)
objRange = objSheet.Cells(1, 11)
For Each objDr In objDs.Tables("tblCategoryData").Rows
objRange.Value = objDr.Item("strTypeCodeCA")
objRange = objRange.Offset(1, 0) 'Move to next row, same column
Next
objRange = objRange.Offset(-2, -10) 'Using the offset technique to move up by 2 row and move left by 10 column
objRange.Value = "Team Name"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "Leader Name"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "Contact"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "Email"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "Address"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "National Identification Number"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "Country"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "Registration Date"
objRange.Font.FontStyle = "Bold"
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.Value = "Category"
objRange.Font.FontStyle = "Bold"
objRange = objSheet.Cells(4, 1)
For Each objDr In objDs.Tables("tblTeamData").Rows
objRange = objRange.Offset(0, 0) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 20
objRange.Value = objDr.Item("strTeamNameTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 20
objRange.Value = objDr.Item("strFullNameTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 8
objRange.Value = objDr.Item("intContactTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 20
objRange.Value = objDr.Item("strEmailTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 20
objRange.Value = objDr.Item("strAddressTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 20
objRange.Value = objDr.Item("strNationalIdTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 10
objRange.Value = objDr.Item("strCountryTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 10
objRange.Value = objDr.Item("strRegistrationDateTE")
objRange = objRange.Offset(0, 1) 'Using the offset technique to move right by 1 column
objRange.ColumnWidth = 10
objRange.Value = objDr.Item("strTypeCodeCA")
objRange.Validation.Add(Type:=Excel.XlDVType.xlValidateList, _
AlertStyle:=Excel.XlDVAlertStyle.xlValidAlertStop, _
Operator:=Excel.XlFormatConditionOperator.xlBetween, Formula1:="=$K$1:$K$4")
objRange = objRange.Offset(1, -8) 'Move to the next row and move to left by 8 columns
Next
objCn.Close()
objCn.Dispose()
End Sub
Private Sub ThisWorkbook_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub
End Class