Sub btn_add_Click
If dep_name.Text = "" Then
MessageBox.Show("กรุณาระบุ ชื่อแผนก ", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
dep_name.Focus()
Else
strcheck = "select count(*) from dep where dep_name = '" & Me.dep_name.Text & "' "
cmddata = New OleDbCommand(strcheck, objConn)
num = cmddata.ExecuteScalar()
data = "insert into dep(dep_id,dep_name) values ('" & dep_id.Text & "','" & dep_name.Text & "')"
Try
With cmddata
.CommandType = CommandType.Text
.CommandText = data
.Connection = conn
.ExecuteNonQuery()
MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว")
auto_id = auto_id + 1
dep_id.Text = auto_id.ToString("000")
End With
Catch ex As Exception
MsgBox("ไม่สามารถเพิ่มข้อมูลได้")
End Try
End If
Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
admin_menu.Show()
Me.Hide()
End Sub
รบกวนด้วยนะครับ ขอบคุณครับผม
Tag : .NET, Ms Access, VBScript, Crystal Report, Win (Windows App), VB.NET
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strcheck, num, Data As String
If dep_name.Text = "" Then
MessageBox.Show("กรุณาระบุ ชื่อแผนก ", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
dep_name.Focus()
Else
Dim Conn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=sayki.accdb"
Dim objConn As New OleDbConnection(Conn)
objConn.Open()
strcheck = "select count(*) from dep where dep_name = '" & Me.dep_name.Text & "' "
Dim cmddata As New OleDbCommand(strcheck, objConn)
num = cmddata.ExecuteScalar()
Data = "insert into dep(dep_id,dep_name) values ('" & dep_id.Text & "','" & dep_name.Text & "')"
Try
With cmddata
.CommandType = CommandType.Text
.CommandText = Data
.ExecuteNonQuery()
MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว")
Me.NewID()
End With
Catch ex As Exception
MsgBox("ไม่สามารถเพิ่มข้อมูลได้")
End Try
End If
End Sub
Sub NewID() ' ใช้หา running number ลำดับต่อไป โดยเอาเลข id ล่าสุด มา+1
Try
Dim Conn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=sayki.accdb"
Dim objConn As New OleDbConnection(Conn)
objConn.Open()
Dim strcheck As String = "select dep_id from dep order by dep_id desc "
Dim cmddata As New OleDbCommand(strcheck, objConn)
Dim dr As OleDbDataReader = cmddata.ExecuteReader
If dr.HasRows Then
dr.Read()
Me.dep_id.Text = (dr.GetInt32(0) + 1).ToString ' +ตรงนี้
Else
Me.dep_id.Text = "1" ' หากยังไม่มีเลย ก็เริ่มด้วย 1
End If
objConn.Close()
dr.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class