 |
|
[VB.Net]Error ตรง Syntax error in INSERT INTO statement. ครับ รบกวนผู้รู้ช่วยแก้ไขหน่อยครับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
If txtEmployeeID.Text = "" Then
MessageBox.Show("กรุณาระบุรหัสพนักงาน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtEmployeeID.Focus()
Exit Sub
End If
If lsvPrintColorList.Items.Count = 0 Then
MessageBox.Show("กรุณาป้อนรายการเบิกสีหมึก !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtPrintCLID.Focus()
Exit Sub
End If
Dim sqlDisclose As String = ""
Dim comDisclose As OleDbCommand = New OleDbCommand
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Try
If MessageBox.Show("คุณต้องการบันทึกรายการเบิกสีหมึก ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
AutoGenerateTranID()
sqlDisclose = "INSERT INTO Disclose (TransID,EmployeeID) "
sqlDisclose &= " VALUES('" & LastTransID & "',"
sqlDisclose &= "'" & txtEmployeeID.Text & "')"
With comDisclose
.CommandType = CommandType.Text
.CommandText = sqlDisclose
.Connection = Conn
.ExecuteNonQuery()
End With
Dim i As Integer = 0
Dim tmpPrintCLID As String = ""
Dim sqlPrintColor As String = ""
Dim tmpColorInShop As Integer = 0
Dim tmpColorInDisclose As Integer = 0
Dim tmpDiscloseNum As Integer = 0
Dim tmpDate_Disclose As Date
tmpDate_Disclose = Date.Now
For i = 0 To lsvPrintColorList.Items.Count - 1
sqlDisclose = "INSERT INTO DiscloseDetail(TransID,PrintCLID,Date_Disclose,ColorInShop,"
sqlDisclose &= " ColorInDisclose,DiscloseNum)"
sqlDisclose &= " VALUES('" & LastTransID & "',"
tmpPrintCLID = lsvPrintColorList.Items(i).SubItems(0).Text
tmpDiscloseNum = CInt(lsvPrintColorList.Items(i).SubItems(2).Text)
sqlDisclose &= "'" & tmpPrintCLID & "',"
sqlDisclose &= "'" & tmpDate_Disclose & "',"
sqlDisclose &= tmpDiscloseNum & ","
sqlDisclose &= ")"
With comDisclose
.CommandText = sqlDisclose
.ExecuteNonQuery()
End With
sqlPrintColor = "SELECT PrintCLID,ColorInShop,ColorInDisclose FROM PrintColor"
sqlPrintColor &= " WHERE (PrintCLID='" & tmpPrintCLID & "')"
da.SelectCommand.CommandText = sqlPrintColor
da.Fill(ds, "PrintColor")
tmpColorInShop = CInt(ds.Tables("PrintColor").Rows(0).Item("ColorInShop"))
tmpColorInDisclose = CInt(ds.Tables("PrintColor").Rows(0).Item("ColorInDisclose"))
ds.Tables("PrintColor").Clear()
sqlDisclose = "UPDATE PrintColor"
sqlDisclose &= " SET ColorInShop=" & tmpColorInShop - tmpDiscloseNum & ","
sqlDisclose &= " ColorInDisclose=" & tmpColorInDisclose + tmpDiscloseNum
sqlDisclose &= " WHERE (PrintCLID='" & tmpPrintCLID & "')"
With comDisclose
.CommandText = sqlDisclose
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกรายการเบิกสีหมึกเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
ClearAllCustomerData()
ClearAllPrintColorData()
lsvPrintColorList.Items.Clear()
cboDepartment.SelectedIndex = 0
txtEmployeeID.Enabled = True
txtEmployeeID.Focus()
End If
Catch ErrProcess As Exception
MessageBox.Show("ไม่สามารถบันทึกรายการเบิกสีหมึกได้ เนื่องจาก " & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
End Sub
ลองตรวจสอบหลายรอบแล้วก็หาจุดพลาดไม่เจอ รบกวนผู้รู้ผู้เชี่ยวชาญช่วยหาหน่อยครับว่า Error ตรงไหน
เท่าที่ผมตรวจสอบมา น่าจะติดช่วงบรรทัดที่ 052 แต่ลองดูแล้วแก้แล้วก็ขึ้น syntax error
Tag : Ms Access, VB.NET, Windows
|
ประวัติการแก้ไข 2017-09-08 15:25:22
|
 |
 |
 |
 |
Date :
2017-09-08 15:23:25 |
By :
ford24832 |
View :
1565 |
Reply :
13 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง Debug ดู sqlDisclose ค่ามันหน่อยครับ
|
 |
 |
 |
 |
Date :
2017-09-12 17:10:52 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเอา " , " หลัง บรรทัดที่ 64 ออกครับ
|
 |
 |
 |
 |
Date :
2017-09-13 10:43:17 |
By :
thongchai001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มอง SQL แล้วงงตาม 555
Code (VB.NET)
sqlDisclose = "INSERT INTO DiscloseDetail(TransID,PrintCLID,Date_Disclose,ColorInShop,"
sqlDisclose &= " ColorInDisclose,DiscloseNum)"
sqlDisclose &= " VALUES('" & LastTransID & "',"
tmpPrintCLID = lsvPrintColorList.Items(i).SubItems(0).Text
tmpDiscloseNum = CInt(lsvPrintColorList.Items(i).SubItems(2).Text)
sqlDisclose &= "'" & tmpPrintCLID & "',"
sqlDisclose &= "'" & tmpDate_Disclose & "',"
sqlDisclose &= tmpDiscloseNum & ","
sqlDisclose &= ")"
ถ้าเราเขียนใหม่จะได้ว่า
Code (VB.NET)
tmpPrintCLID = lsvPrintColorList.Items(i).SubItems(0).Text
tmpDiscloseNum = CInt(lsvPrintColorList.Items(i).SubItems(2).Text)
sqlDisclose = "INSERT INTO DiscloseDetail(TransID,PrintCLID,Date_Disclose,ColorInShop,ColorInDisclose,DiscloseNum)"
sqlDisclose &= " VALUES('" & LastTransID & "','" & tmpPrintCLID & "','" & tmpDate_Disclose & "','" & tmpDiscloseNum & ",)"
ผมว่าพอเราเขียนให้สั้นลงจะมองเห็นข้อผิดพลาดได้ง่ายกว่าไม๊ครับ
แนะนำให้ใช้ &_ แทนการขึ้นบรรทัดใหม่ เพื่อให้โค้ดไม่รกจนเกินไป
Code (VB.NET)
tmpPrintCLID = lsvPrintColorList.Items(i).SubItems(0).Text
tmpDiscloseNum = CInt(lsvPrintColorList.Items(i).SubItems(2).Text)
sqlDisclose = "INSERT INTO DiscloseDetail(TransID,PrintCLID,Date_Disclose,ColorInShop," &_
" ColorInDisclose,DiscloseNum)" &_
" VALUES('" & LastTransID & "'," &_
"'" & tmpPrintCLID & "'," &_
"'" & tmpDate_Disclose & "'," &_
tmpDiscloseNum & ","&_
")"
ก็ยังได้เห็นได้ง่ายขึ้นไปอีกว่า มัน ผิดพลาดจากจุดไหน นะครับ
|
 |
 |
 |
 |
Date :
2017-09-13 11:21:36 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2017-09-12 17:10:52
รายละเอียดของการตอบ ::
ADO.NET:Execute NonQuery "INSERT INTO DiscloseDetail(TransID,PrintCLID,Date_Disclose,ColorInShop, ColorInDisclose,DiscloseNum) VALUES('0000012','2','14/9/2560 9:11:39',1,)"
The command text "INSERT INTO DiscloseDetail(TransID,PrintCLID,Date_Disclose,ColorInShop, ColorInDisclose,DiscloseNum) VALUES('0000012','2','14/9/2560 9:11:39',1,)" was executed on connection "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\Users\I'm Ford\Desktop\Part II\Database\DM.mdb", returning the number of rows affected.
Time: 14/9/2560 9:11:46
Thread:<No Name>[17436]
ได้ประมาณนี้ครับ อย่างนี้คือผมใส่ค่า values ไม่ตรงกันใช่ไหมครับ
|
 |
 |
 |
 |
Date :
2017-09-14 09:24:18 |
By :
ford24832 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องขออภัยทุกท่านที่เข้ามาตอบนะครับ พอดีช่วงนี้ผมกำลังอยู่ในช่วงฝึกงานและทำโปรเจ็คโปรแกรม โค้ดที่ผมนำมาเพื่อขอความรู้และความช่วยเหลือก็เป็นหนึ่งในโปรแกรมที่ผมกำลังทำขึ้นมาครับ ผมขอขอบคุณทุกท่านมากๆนะครับที่ให้ความช่วยเหลือกับผม
|
 |
 |
 |
 |
Date :
2017-09-14 09:32:37 |
By :
ford24832 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันจะ INSERT ได้ไงครับ เพราะค่า COLUMN กับ Parameter ไม่ตรงกัน
INSERT INTO DiscloseDetail
(TransID,PrintCLID,Date_Disclose,ColorInShop, ColorInDisclose,DiscloseNum) มี 6
VALUES('0000012','2','14/9/2560 9:11:39',1,) มี 4
|
 |
 |
 |
 |
Date :
2017-09-14 09:36:06 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องขอบคุณทุกท่านนะครับที่ให้ความช่วยเหลือ ตอนนี้ผมแก้ได้แล้วครับ เพราะผมลืมตรวจสอบว่าค่า Insert Column และ Parameter ไม่ตรงกัน
|
 |
 |
 |
 |
Date :
2017-09-14 10:05:41 |
By :
ford24832 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถึงได้บอกไปละครับ
ว่าถ้าโค้ดสั้นลงจะเห็นข้อผิดพลาดง่ายขึ้น
แต่กระนั้นยังมองไม่เห็นเรื่องของจำนวน Parameter อยู่ดี
แต่ของแบบนี้มันแก้กันได้ จริงไม๊ 555
|
 |
 |
 |
 |
Date :
2017-09-14 10:44:06 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2017-09-14 16:52:47 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|