Dim connection As New OleDbConnection(con)
connection.Open()
Dim myTable As DataTable = New DataTable("MyTable")
myTable.Columns.Add(New DataColumn("date"))
myTable.Columns.Add(New DataColumn("product"))
myTable.Columns.Add(New DataColumn("channel"))
myTable.Columns.Add(New DataColumn("position"))
myTable.Columns.Add(New DataColumn("Impression"))
myTable.Columns.Add(New DataColumn("Impression to use"))
myTable.Columns.Add(New DataColumn("impression/now"))
Dim sdate As Date
Dim edate As Date
sdate = CType(DateTimePicker1.Text, Date)
edate = CType(DateTimePicker2.Text, Date)
Try
'Add columns and column names to the datatable
Dim cmd As OleDbCommand = New OleDbCommand("SELECT ch_day,P_name,ch_name,ch_posi,imp_total,imp_use,imp_now FROM Impress WHERE (Impress.ch_day>=#" & sdate & "# And Impress.ch_day<=#" & edate & "#)And (P_name='" & cbo_web.Text & "')And(ch_name='" & cbo_ch.Text & "')And(ch_posi='" & cbo_posi.Text & "') order by ch_id", connection)
Dim dr As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.Default)
Dim myRow As DataRow
While dr.Read()
myRow = myTable.NewRow
myRow.Item(0) = dr(0) 'We add the 1st value of the row to our 1st column of the datatable
myRow.Item(1) = dr(1)
myRow.Item(2) = dr(2)
myRow.Item(3) = dr(3)
myRow.Item(4) = dr(4)
myRow.Item(5) = dr(5)
myRow.Item(6) = dr(6)
myTable.Rows.Add(myRow)
End While
dr.Close()
'Add datatable to datagridview
Dim myData4 As DataTable = myTable
ds = New DataSet()
ds.Tables.Add(myData4)
DataGridView1.DataSource = myData4
connection.Close()
Catch ErrProcess As Exception
' MsgBox(ex.ToString) 'Show error when something goes wrong
MessageBox.Show("ไม่สามารถคำนวณImpression จากรายการที่ท่านเลือก เนื่องจาก " & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
Tag : .NET, Ms Access, VBScript, VB.NET, VS 2012 (.NET 4.x)