Private Sub BNSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BNSearch.Click
Try
Dim sql_search As String
Dim search As String
Dim search2 As String
search = CBContractor.Text
search2 = TXTItem.Text
Dim num As Integer
If search = "ALL " Then
If search2 = "" Then
num = 1
Else
num = 2
End If
Else
If search = "" Then
If search2 = "" Then
num = 0
Else
num = 1
End If
Else
If search2 = "" Then
num = 2
Else
num = 3
End If
End If
End If
sql_search = " SELECT IT_PlannedList.PlannedListID, IT_Description.Item_Desc AS Item, IT_PlannedList.Remark, Contractor.Contractor_Name AS CT, IT_PlannedList.OrderedTimes, IT_PlannedList.Qty, IT_TransContractor.ReceivedQty AS [Rec Qty], IT_PlannedList.Qty - IT_TransContractor.ReceivedQty AS [Awaiting Qty], IT_PlannedList.Unit FROM (((Contractor INNER JOIN IT_PlannedList ON Contractor.Contractor_ID = IT_PlannedList.ContractorID) INNER JOIN IT_TransContractor ON IT_PlannedList.ItemNo = IT_TransContractor.ItemNo AND IT_PlannedList.ContractorID = IT_TransContractor.ContractorID AND IT_PlannedList.OrderedTimes = IT_TransContractor.OrderedTimes) INNER JOIN IT_Description ON IT_TransContractor.ItemNo = IT_Description.ItemNo) "
Dim sqlWhere As String = ""
Select Case num
Case 0 : sqlWhere = ""
Case 1 : sqlWhere = "where Item_Desc LIKE'" & search2 & "'"
Case 2 : sqlWhere = "where Contractor_Name LIKE '" & search & "'"
Case 3 : sqlWhere = "where Contractor_Name LIKE '" & search & "' and Item_Desc LIKE'" & search2 & "'"
End Select
Dim sqlemp As String
sqlemp = sql_search & sqlWhere
' MessageBox.Show(num)
da = New OleDbDataAdapter(sqlemp, conn)
da.Fill(ds, "Contractor")
If ds.Tables("Contractor").Rows.Count <> 0 Then
With DataGridView1
.ReadOnly = True
.DataSource = ds.Tables("Contractor")
End With
Else
End If
'------------------------------- ตาราง -----------------------------------
'Dim cs As New DataGridViewCellStyle()
'cs.Font = New Font("Ms Sans Serif", 10, FontStyle.Bold)
With DataGridView1
' .ColumnHeadersDefaultCellStyle = cs 'กำหนดข้อความแต่ละคอลัมน์
.Columns(0).HeaderText = "PlannedListId"
.Columns(1).HeaderText = "Item"
.Columns(2).HeaderText = "Remark"
.Columns(3).HeaderText = "CT"
.Columns(4).HeaderText = "Ordertimes"
.Columns(5).HeaderText = "Qty"
.Columns(6).HeaderText = "Rec Qty"
.Columns(7).HeaderText = "Awaiting Qty"
.Columns(8).HeaderText = "Unit"
.Columns(0).Width = 100 'กำหนดความกว้าง
.Columns(1).Width = 210
.Columns(2).Width = 210
.Columns(2).Width = 210
.Columns(3).Width = 75
.Columns(4).Width = 75
.Columns(5).Width = 50
.Columns(6).Width = 50
.Columns(7).Width = 50
.Columns(8).Width = 50
End With
'------------------------------- ตาราง -----------------------------------
Catch ex As Exception
MessageBox.Show("ไม่พบข้อมูล", "Error !!", MessageBoxButtons.OK, MessageBoxIcon.Information)
'MessageBox.Show(ex.Message)
End Try
End Sub
รูปข้อมูลในDB
รูปที่หน้าโปรแกรม
ต้องการให้มันออกมาเปน
plannedlistID ITEM Remark CT Ordertimes QTY Rec Qty Awaiting Qty unit
56 microsoft office C2 (CK) 1 43 43 0 unit
....
....
....
Tag : Ms Access, Win (Windows App), VB.NET, VS 2010 (.NET 4.x), Windows
select plannedlistID ,ITEM, Remark, CT, Ordertimes ,QTY, SUM(Rec Qty) as Rec_Qty ,QTY-SUM(Rec Qty) as Awaiting_Qty,unit from ชื่อตาราง
group by plannedlistID ,ITEM, Remark, CT, Ordertimes ,QTY,unit
มันบอกว่า plannedlistID ไม่ได้อยู่ใน Group by ครับ
Date :
2013-10-17 08:39:12
By :
fonfire
No. 7
Guest
อันนี้ผมลอง Group by ตามที่คุณFONFIREบอก
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.DataRow
Public Class Plan_List_Dialog
Dim conn As New OleDbConnection
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
'Dim dvm As DataViewManager
Public sendVar As Integer
Dim dt As DataTable
Dim dr As DataRow
Private Sub BNSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BNSearch.Click
' Try
Dim sql_search As String
Dim search As String
Dim search2 As String
search = CBContractor.Text
search2 = TXTItem.Text
Dim num As Integer
If search = "ALL " Then
If search2 = "" Then
num = 1
Else
num = 2
End If
Else
If search = "" Then
If search2 = "" Then
num = 0
Else
num = 1
End If
Else
If search2 = "" Then
num = 2
Else
num = 3
End If
End If
End If
sql_search = " SELECT IT_PlannedList.PlannedListID, IT_Description.Item_Desc AS Item, IT_PlannedList.Remark, Contractor.Contractor_Name AS CT, IT_PlannedList.OrderedTimes, IT_PlannedList.Qty, SUM(IT_TransContractor.ReceivedQty), IT_PlannedList.Qty - IT_TransContractor.ReceivedQty AS [Awaiting Qty], IT_PlannedList.Unit FROM (((Contractor INNER JOIN IT_PlannedList ON Contractor.Contractor_ID = IT_PlannedList.ContractorID) INNER JOIN IT_TransContractor ON IT_PlannedList.ItemNo = IT_TransContractor.ItemNo AND IT_PlannedList.ContractorID = IT_TransContractor.ContractorID AND IT_PlannedList.OrderedTimes = IT_TransContractor.OrderedTimes) INNER JOIN IT_Description ON IT_TransContractor.ItemNo = IT_Description.ItemNo) "
Dim sqlWhere As String = ""
Select Case num
Case 0 : sqlWhere = ""
Case 1 : sqlWhere = "where Item_Desc LIKE'" & search2 & "'"
Case 2 : sqlWhere = "where Contractor_Name LIKE '" & search & "' "
Case 3 : sqlWhere = "where Contractor_Name LIKE '" & search & "' and Item_Desc LIKE'" & search2 & "'"
End Select
Dim sqlemp As String
Dim group As String = " group by IT_PlannedList.PlannedListID ,IT_Description.Item_Desc, IT_PlannedList.Remark, Contractor.Contractor_Name, IT_PlannedList.OrderedTimes ,IT_PlannedList.Qty,IT_PlannedList.Unit"
sqlemp = sql_search & sqlWhere & group
' MessageBox.Show(num)
da = New OleDbDataAdapter(sqlemp, conn)
da.Fill(ds, "Contractor")
If ds.Tables("Contractor").Rows.Count <> 0 Then
With DataGridView1
.ReadOnly = True
.DataSource = ds.Tables("Contractor")
End With
Else
End If
'------------------------------- ตาราง -----------------------------------
'Dim cs As New DataGridViewCellStyle()
'cs.Font = New Font("Ms Sans Serif", 10, FontStyle.Bold)
With DataGridView1
' .ColumnHeadersDefaultCellStyle = cs 'กำหนดข้อความแต่ละคอลัมน์
.Columns(0).HeaderText = "PlannedListId"
.Columns(1).HeaderText = "Item"
.Columns(2).HeaderText = "Remark"
.Columns(3).HeaderText = "CT"
.Columns(4).HeaderText = "Ordertimes"
.Columns(5).HeaderText = "Qty"
.Columns(6).HeaderText = "Rec Qty"
.Columns(7).HeaderText = "Awaiting Qty"
.Columns(8).HeaderText = "Unit"
.Columns(0).Width = 100 'กำหนดความกว้าง
.Columns(1).Width = 210
.Columns(2).Width = 210
.Columns(2).Width = 210
.Columns(3).Width = 75
.Columns(4).Width = 75
.Columns(5).Width = 50
.Columns(6).Width = 50
.Columns(7).Width = 50
.Columns(8).Width = 50
End With
'------------------------------- ตาราง -----------------------------------
'Catch ex As Exception
' MessageBox.Show("ไม่พบข้อมูล", "Error !!", MessageBoxButtons.OK, MessageBoxIcon.Information)
' 'MessageBox.Show(ex.Message)
'End Try
End Sub
End Class
[head]เปลี่ยนเปนใส่ sum -> IT_PlannedList.Qty - SUM(IT_TransContractor.ReceivedQty) AS [Awaiting Qty][head]
sql_search = " SELECT IT_PlannedList.PlannedListID, IT_Description.Item_Desc AS Item, IT_PlannedList.Remark, Contractor.Contractor_Name AS CT, IT_PlannedList.OrderedTimes, IT_PlannedList.Qty, IT_TransContractor.ReceivedQty, sum(IT_PlannedList.Qty - IT_TransContractor.ReceivedQty) AS [Awaiting Qty], IT_PlannedList.Unit FROM (((Contractor INNER JOIN IT_PlannedList ON Contractor.Contractor_ID = IT_PlannedList.ContractorID) INNER JOIN IT_TransContractor ON IT_PlannedList.ItemNo = IT_TransContractor.ItemNo AND IT_PlannedList.ContractorID = IT_TransContractor.ContractorID AND IT_PlannedList.OrderedTimes = IT_TransContractor.OrderedTimes) INNER JOIN IT_Description ON IT_TransContractor.ItemNo = IT_Description.ItemNo) "
Dim sqlWhere As String = ""
Select Case num
Case 0 : sqlWhere = ""
Case 1 : sqlWhere = "where Item_Desc LIKE'" & search2 & "'"
Case 2 : sqlWhere = "where Contractor_Name LIKE '" & search & "' "
Case 3 : sqlWhere = "where Contractor_Name LIKE '" & search & "' and Item_Desc LIKE'" & search2 & "'"
End Select
Dim sqlemp As String
Dim group As String = " group by IT_PlannedList.PlannedListID ,IT_Description.Item_Desc, IT_PlannedList.Remark, Contractor.Contractor_Name, IT_PlannedList.OrderedTimes ,IT_PlannedList.Qty,IT_PlannedList.Unit"
sqlemp = sql_search & sqlWhere & group
มานขึ้น error ครับ
Date :
2013-10-17 09:27:40
By :
ปวีณวัช
No. 12
Guest
แก้ไขๆ ลงcode ผิด
sql_search = " SELECT IT_PlannedList.PlannedListID, IT_Description.Item_Desc AS Item, IT_PlannedList.Remark, Contractor.Contractor_Name AS CT, IT_PlannedList.OrderedTimes, IT_PlannedList.Qty, IT_TransContractor.ReceivedQty, IT_PlannedList.Qty - sum(IT_TransContractor.ReceivedQty) AS [Awaiting Qty], IT_PlannedList.Unit FROM (((Contractor INNER JOIN IT_PlannedList ON Contractor.Contractor_ID = IT_PlannedList.ContractorID) INNER JOIN IT_TransContractor ON IT_PlannedList.ItemNo = IT_TransContractor.ItemNo AND IT_PlannedList.ContractorID = IT_TransContractor.ContractorID AND IT_PlannedList.OrderedTimes = IT_TransContractor.OrderedTimes) INNER JOIN IT_Description ON IT_TransContractor.ItemNo = IT_Description.ItemNo) "
Dim sqlWhere As String = ""
Select Case num
Case 0 : sqlWhere = ""
Case 1 : sqlWhere = "where Item_Desc LIKE'" & search2 & "'"
Case 2 : sqlWhere = "where Contractor_Name LIKE '" & search & "' "
Case 3 : sqlWhere = "where Contractor_Name LIKE '" & search & "' and Item_Desc LIKE'" & search2 & "'"
End Select
Dim sqlemp As String
Dim group As String = " group by IT_PlannedList.PlannedListID ,IT_Description.Item_Desc, IT_PlannedList.Remark, Contractor.Contractor_Name, IT_PlannedList.OrderedTimes ,IT_PlannedList.Qty,IT_PlannedList.Unit"
sqlemp = sql_search & sqlWhere & group