Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > VB2010&Access คือผมมีปัญหาเกี่ยวกับปุ่มเพิ่ม แก้ไขข้อมูลในตารางครับ แนะนำทีนะครับ


 

[.NET] VB2010&Access คือผมมีปัญหาเกี่ยวกับปุ่มเพิ่ม แก้ไขข้อมูลในตารางครับ แนะนำทีนะครับ

 
Topic : 103777



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์



คือผมสามารถเพิ่มข้อมูลลงไปในตารางได้ตามปกตินะครับแต่ผมไม่สามารถที่จะแก้ไขข้อมูลได้ แล้วโปรแกรมก็ไม่ฟ้องว่า error จุดไหนแต่ไม่สามารถคลิกในตารางเพื่อแก้ไขข้อมูลได้ครับ

SS

Code (VB.NET)
001.Public Class vehicle
002.    Dim x As Integer
003.    Dim strCheck As String 'ตัวแปลเก็บข้อมูลคำสั่ง sql เอาไว้เช็คข้อผิดพลาด
004.    Dim strSQL As String 'ตัวแปลเก็บข้อมูลคำสั่ง sql สำหรับ เพิ่มข้อมูล
005.    Dim ID As String 'DPass ใช้เก็บรหัสข้อมูลหรัสหน่วยงานจากที่ผู้ใช้งานเลือก
006. 
007. 
008.    Private Sub vehicle_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
009. 
010.        Me.WindowState = FormWindowState.Maximized
011. 
012.        cb_search.SelectedIndex = 0
013.        ComboBox1.SelectedIndex = 0
014.        ComboBox2.SelectedIndex = 0
015.        ComboBox3.SelectedIndex = 0
016. 
017.        'เชื่อมต่อฐานข้อมูล.............................................................................
018.        Try 'คำสั่งดัก error สั่งให้ทำ
019.            With CnMysql
020.                If .State = ConnectionState.Open Then .Close() 'ตรวจสอบสถานะว่าตัวคอนเน็คฐานข้อมูลเปิดอยู่หรือเปล่า
021.                .ConnectionString = strcon 'กำหนดว่าฐานข้อมูลอยู่ที่ใหนในเครื่อง เชื่อมต่อฐานข้อมูลอะไร
022.                .Open() 'เปิดการใช้งานฐานข้อมูล
023.            End With
024. 
025.        Catch 'ถ้า error จะมาทำงานที่ Catch อัตโนมัติ
026.            MessageBox.Show("ติดต่อฐานข้อมูลไม่ได้") 'แสดงข้อความสาเหตุของการ error
027.        End Try 'สิ้นสุดคำสั่ง try catch
028.        'สิ้นสุด Code เชื่อมต่อฐานข้อมูล...............................................................
029.        Dim sqlDePart As String = "SELECT * FROM vehiclekind ORDER BY Vehicle_ID"
030.        Dim dsPart As New DataSet()
031.        da = New OleDb.OleDbDataAdapter(sqlDePart, CnMysql)
032.        da.Fill(dsPart, "DePart")
033.        If dsPart.Tables("DePart").Rows.Count <> 0 Then
034.            cmbkind.DataSource = dsPart.Tables("DePart")
035.            cmbkind.DisplayMember = dsPart.Tables("DePart").Columns("Vehicle_ID").ToString()
036.            cmbkind.ValueMember = dsPart.Tables("DePart").Columns("Vehicle_kind").ToString()
037.        End If
038. 
039.        ShowGrid() 'ดึงข้อมูลมาโชว์ในกริด
040.    End Sub
041. 
042.    Private Sub bt_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_add.Click
043.        'ตรวจสอบการกรอกข้อมูลของผู้ใช้งาน............................................................
044.        If txt_no.Text = "" Then
045.            MessageBox.Show("กรุณากรอกข้อมูลรหัส")
046.            Exit Sub
047.            txt_no.Focus()
048.        ElseIf txt_name.Text = "" Then
049.            MessageBox.Show("กรุณากรอกข้อมูลชื่อ")
050.            txt_name.Focus()
051.        End If
052.        'สิ้นสุดการตรวจสอบการกรอกข้อมูลของผู้ใช้งาน.......................................................
053. 
054.        'ตรวจสอบว่า หรัสหน่วยงานที่กรอกมานั้นมีอยู่แล้วในฐานข้อมูลหรือไม่........................
055.        strCheck = "SELECT * FROM tblvehicle WHERE Vehicle_StudentNo = '" & txt_no.Text & "'"
056.        da = New OleDb.OleDbDataAdapter() 'รีเฟรช da
057.        ds = New DataSet() 'รีเฟรช ds
058. 
059.        da = New OleDb.OleDbDataAdapter(strCheck, CnMysql)
060.        da.Fill(ds, "Chk_Emp")
061.        If ds.Tables("Chk_Emp").Rows.Count <> 0 Then
062.            MessageBox.Show("รหัสนี้มีอยู่แล้วในฐานข้อมูล", "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
063.            Exit Sub 'สั่งให้มันหยุดการทำงาน
064.        End If
065.        'สิ้นสุดตรวจสอบว่า หรัสหน่วยงานที่กรอกมานั้นมีอยู่แล้วในฐานข้อมูลหรือไม่........................
066. 
067. 
068.        'บันทึกข้อมูลลงใน Employee .........................................................
069.        'คำสั่ง sql สำหรับบันทึกข้อมูล INSERT INTO ชื่อตาราง (ชื่อฟิวล์1,ชื่อฟิวล์2,...,ชื่อฟิวล์ N) VALUES('ค่าของฟิว 1',ค่าฟิว 2,...,'ค่าฟิว N')
070.        strSQL = "Insert into tblvehicle(Vehicle_StudentNo,Vehicle_StudentName , Vehicle_StudenLastName, Vehicle_StudentBranch, Vehicle_StudentDegree, Vehicle_StudentYear, Vehicle_StudentPhone, Vehicle_StudentEmail, Vehicle_MemberNo, Vehicle_MemberName, Vehicle_MemberLastName, Vehicle_Memberoccupation, Vehicle_MemberPhone, Vehicle_MemberEmail, Vehicle_kind, Vehicle_registration, Vehicle_trademark, Vehicle_Color, Vehicle_jv, Vehicle_in) values('" & txt_no.Text & "','" & txt_name.Text & "','" & txt_lastname.Text & "','" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & ComboBox3.Text & "','" & txt_phone.Text & "','" & txt_email.Text & "','" & txt_no2.Text & "','" & txt_name2.Text & "','" & txt_lastname2.Text & "','" & txt_occ.Text & "','" & txt_phone2.Text & "','" & txt_email2.Text & "','" & cmbkind.SelectedValue.ToString() & "','" & txt_red.Text & "','" & txt_td.Text & "','" & txt_color.Text & "','" & txt_jv.Text & "','" & txt_in.Text & "')"
071. 
072.        Try
073.            With cm
074.                .CommandType = CommandType.Text 'กำหนดประเภทคำสั่ง
075.                .CommandText = strSQL ' ส่งค่าคำสั่ง sql ให้ object
076.                .Connection = CnMysql ' บอกเส้นทางเชื่อมต่อ
077.                .ExecuteNonQuery() ' สั่งให้ทำงาน
078. 
079.                MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว")
080.                ShowGrid() 'แสดงข้อมูลในกริด
081.                ClearText() 'ล้างข้อมูลใน text
082.            End With
083.        Catch
084.            MessageBox.Show("การเพิ่มข้อมูลผิดพลาด")
085.            Exit Sub
086.        End Try 'สิ้นสุดบันทึกข้อมูลลงใน Employee .........................................................
087.    End Sub
088. 
089.    '.... Sub ฟังชั่นของการโชว์ข้อมูลใน datagridview
090.    Sub ShowGrid()
091.        Dim strShow As String
092.        strShow = "SELECT * FROM tblvehicleQuery ORDER BY Vehicle_StudentNo"
093.        ds = New DataSet()
094.        da = New OleDb.OleDbDataAdapter(strShow, CnMysql)
095.        da.Fill(ds, "Show")
096.        If ds.Tables("Show").Rows.Count <> 0 Then
097.            DataGridView1.DataSource = ds.Tables("Show")
098. 
099.        End If
100. 
101.    End Sub '.... สิ้นสุด Sub ฟังชั่นของการโชว์ข้อมูลใน datagridview ... สิ้นสุด Sub ฟังชั่นของการโชว์ข้อมูลใน datagridview
102. 
103. 
104.    '.... Sub ฟังชั่นของการเคลียร์ข้อมูลใน textbox
105.    Sub ClearText()
106.        txt_no.Text = ""
107.        txt_name.Text = ""
108.        txt_lastname.Text = ""
109.        ComboBox1.SelectedValue = 0
110.        ComboBox2.SelectedValue = 0
111.        ComboBox3.SelectedValue = 0
112.        txt_phone.Text = ""
113.        txt_email.Text = ""
114.        txt_no2.Text = ""
115.        txt_name2.Text = ""
116.        txt_lastname2.Text = ""
117.        txt_occ.Text = ""
118.        txt_phone2.Text = ""
119.        txt_email2.Text = ""
120.        cmbkind.SelectedValue = 0
121.        txt_red.Text = ""
122.        txt_td.Text = ""
123.        txt_color.Text = ""
124.        txt_jv.Text = ""
125.        txt_in.Text = ""
126.    End Sub '.... สิ้นสุด Sub ฟังชั่นของการเคลียร์ข้อมูลใน textbox
127. 
128.    Private Sub bt_Canceled_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Canceled.Click
129.        ClearText()
130.    End Sub
131. 
132.    Private Sub bt_cor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_cor.Click
133.        'แก้ไขข้อมูล......................................................................
134.        'ตรวจสอบการเลือกข้อมูลเพื่อจะแก้ไข ว่าผู้ใช้งานเลือกหรือยัง
135.        If txt_no.Text = "" Then
136.            MessageBox.Show("กรุณาเลือกข้อมูลที่ต้องการแก้ไข")
137.            Exit Sub 'คำสั่งออกจากการทำงาน
138.        End If
139.        'เริ่มต้นการแก้ไข...................................
140.        'Update ชื่อตาราง set ชื่อฟิว = ค่าที่แก้ไข Where เงื่อนไข
141.        strSQL = "Update tblvehicle set Vehicle_StudentName ='" & txt_name.Text & "',Vehicle_StudenLastName = '" & txt_lastname.Text & "',Vehicle_StudentBranch = '" & ComboBox1.Text & "',Vehicle_StudentDegree='" & ComboBox2.Text & "',Vehicle_StudentYear='" & ComboBox3.Text & "',Vehicle_StudentPhone='" & txt_phone.Text & "',Vehicle_StudentEmail='" & txt_email.Text & "',Vehicle_MemberNo='" & txt_no2.Text & "',Vehicle_MemberName='" & txt_name2.Text & "',Vehicle_MemberLastName='" & txt_lastname2.Text & "',Vehicle_Memberoccupation='" & txt_occ.Text & "',Vehicle_MemberPhone='" & txt_phone2.Text & "',Vehicle_MemberEmail='" & txt_email2.Text & "',Vehicle_kind=" & cmbkind.SelectedValue.ToString() & ",Vehicle_registration='" & txt_red.Text & "',Vehicle_trademark='" & txt_td.Text & "',Vehicle_Color='" & txt_color.Text & "' ,Vehicle_jv='" & txt_jv.Text & "',Vehicle_in='" & txt_in.Text & "' where Vehicle_StudentNo='" & ID & "'"
142.        Try
143.            With cm
144.                .CommandType = CommandType.Text 'กำหนดประเภทคำสั่ง
145.                .CommandText = strSQL ' ส่งค่าคำสั่ง sql ให้ object
146.                .Connection = CnMysql ' บอกเส้นทางเชื่อมต่อ
147.                .ExecuteNonQuery() ' สั่งให้ทำงาน
148. 
149.                MessageBox.Show("การแก้ไขเรียบร้อยแล้ว")
150.                ShowGrid() 'แสดงข้อมูลในกริด
151.                ClearText() 'ล้างข้อมูลใน text
152.            End With
153.        Catch
154.            MessageBox.Show("การแก้ไขข้อมูลผิดพลาด")
155.            Exit Sub
156.        End Try
157.    End Sub
158. 
159.    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
160.        If e.RowIndex < 0 Then 'ป้องกันการคลิกเลือกที่หัวคอลัม ถ้าเลือกหัวจะไม่ให้ทำงาน
161.            Exit Sub
162.        End If
163. 
164.        'โชว์ข้อมูลจากการเลือกข้อมูลในกิดวิว
165. 
166.        Dim sqlSelect As String 'เป็นตัวแปลเก็บข้อมูลที่ดึงจากฐานข้อมูลตามที่ผู้ใช้งานเลือก
167.        ID = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString() 'เลือกข้อมูลจาก Cellsที่0 = เลขที่อัตรานั่นเอง^^
168.        sqlSelect = "Select * from tblvehicle Where Vehicle_StudentNo ='" & ID & "'" 'ดึงข้อมูลที่มีเลขที่ตัราตรงกับ DPass ที่เลือก
169.        da = New OleDb.OleDbDataAdapter(sqlSelect, CnMysql) 'สั่งให้ DataAdapter ทำงานตามคำสั่ง sqlSelect
170.        ds = New DataSet() 'รีเฟทดาต้าเซ็ต
171.        da.Fill(ds, "Emp_Show") 'เอาข้อมมูลที่ da ดึงมาได้ใส่ไว้ใน ds
172. 
173.        If ds.Tables("Emp_Show").Rows.Count <> 0 Then
174.            txt_no.Text = ID
175.            txt_name.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_StudentName").ToString()
176.            txt_lastname.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_StudenLastName").ToString()
177.            ComboBox1.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_StudentBranch").ToString()
178.            ComboBox2.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_StudentDegree").ToString()
179.            ComboBox3.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_StudentYear").ToString()
180.            txt_phone.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_StudentPhone").ToString()
181.            txt_email.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_StudentEmail").ToString()
182.            txt_no2.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_MemberNo").ToString()
183.            txt_name2.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_MemberName").ToString()
184.            txt_lastname2.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_MemberLastName").ToString()
185.            txt_occ.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_Memberoccupation").ToString()
186.            txt_phone2.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_MemberPhone").ToString()
187.            txt_email2.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_MemberEmail").ToString()
188.            cmbkind.SelectedValue = ds.Tables("Emp_Show").Rows(0)("Vehicle_kind").ToString()
189.            txt_red.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_registration").ToString()
190.            txt_td.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_trademark").ToString()
191.            txt_color.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_Color").ToString()
192.            txt_jv.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_jv").ToString()
193.            txt_in.Text = ds.Tables("Emp_Show").Rows(0)("Vehicle_in").ToString()
194.        End If
195.    End Sub
196. 
197.    Private Sub txt_search_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_search.TextChanged
198.        Dim strwhere As String
199.        Dim strShow As String
200.        Select Case cb_search.Text
201.            Case "รหัสนักศึกษา"
202.                strwhere = " where Vehicle_StudentNo like '%" & txt_search.Text & "%'"
203.            Case "ชื่อ"
204.                strwhere = " where Vehicle_StudentName like '%" & txt_search.Text & "%'"
205.        End Select
206. 
207.        strShow = "SELECT * FROM tblvehicleQuery " & strwhere & " ORDER BY Vehicle_StudentNo"
208.        ds = New DataSet()
209.        da = New OleDb.OleDbDataAdapter(strShow, CnMysql)
210.        da.Fill(ds, "Show")
211.        DataGridView1.DataSource = ds.Tables("Show")
212.        If ds.Tables("Show").Rows.Count = 0 Then
213.            MessageBox.Show("ไม่มีข้อมูลตามเงื่อนไขที่กำหนด")
214.        End If
215.    End Sub
216. 
217.    Private Sub bt_del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
218. 
219.    End Sub
220.End Class




Tag : .NET, Ms Access, VB.NET, VS 2010 (.NET 4.x)

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-12-19 07:35:57 By : parawichai View : 1085 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

มี Tools สุดยอดอย่าง Visual Studio แล้วทำไมไม่ Debug ดูล่ะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-19 08:54:25 By : mr.win
 

 

No. 2



โพสกระทู้ ( 147 )
บทความ ( 0 )



สถานะออฟไลน์


Debug Error อยู่ไหน อ้าวมาให้ดูหน่อย งี้ตอบไม่ได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-19 09:09:52 By : angelrings0
 

 

No. 3



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์


คือมันไม่เตือนError อะไรสักอย่างเลยอะครับ โค้ดดึงข้อมูลเอามาไว้ในTextเพื่อแก้ไข หรือโค้ดปุ่มแก้ไขก็ใส่หมดแล้วอะครับ แต่พอเวลาคลิกไปที่ตารางข้อมูลมันไม่โชว์ในTextอะครับมันเลยแก้ไขไม่ได้

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-19 09:58:54 By : parawichai
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : VB2010&Access คือผมมีปัญหาเกี่ยวกับปุ่มเพิ่ม แก้ไขข้อมูลในตารางครับ แนะนำทีนะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่