Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class FormMenu
Private con As New OleDbConnection(conn)
Private sqlselect As String
Private oledbreader As OleDbDataReader
Private cmd As New OleDbCommand
Private Sub User()
IDbtt.Enabled = False
Databasebtt.Enabled = False
End Sub
Private Sub Admin()
IDbtt.Enabled = True
Databasebtt.Enabled = True
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = loginmenu.Usertext.Text
con.Open()
Dim sql As String = "SELECT * FROM LoginUser WHERE LUser = 'test' " <=== คำถามอยู่ตรงนี้คะ ถ้าเรากำหนดชื่อ Login เป็น คำว่า "test" ก็จะสามารถกำหนดสิทธิของ ไอดี "test" ได้ แต่ถ้าไม่กำหนดมันทำไม่ได้ จุดประสงค์เราอยากให้มันเช็คชื่อ ไอดี ให้อัตโนมัติ นะคะ ทำยังไงดี ลองมาหลายแบบแล้ว????
Dim dtb As New DataTable
'Dim cmd As New OleDbCommand(sql, con)
Dim adapter As New OleDbDataAdapter(sql, con)
adapter.Fill(dtb)
If dtb.Rows.Count > 0 Then
Dim userclass = dtb.Rows(0)("Class").ToString()
If userclass = "User" Then <=== ตรงนี้เป็น Class User กำหนดไว้ว่าถ้าชื่อไอดี เป็น Class User ก็ให้ปิดปุ่มตามที่ต้องการ
User()
ElseIf userclass = "Admin" Then <=== ส่วนตรงนี้เป็น Class Admin กำหนดไว้ให้เปิดหมดค่ะ (แต่จริงๆไม่ใส่ก็ได้เน่อะ มันเปิดหมดอยู่แล้วอิอิ)
Admin()
End If
End If
End Sub
Private Sub DataEmp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Open()
EmpInfo()
If (namebox.Items.Count > 0) Then
namebox.SelectedIndex = 0
End If
End Sub
Private Sub namebox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles namebox.SelectedIndexChanged
empdetial()
End Sub
Private Sub EmpInfo()
'Dim sql As String = "SELECT DISTINCT(EmpFrontName) FROM Employee" ตรวจสอบค่าซ้ำ
Dim sql As String = "SELECT * FROM Employee WHERE EmpFrontName"
cmd = New OleDbCommand(sql, con)
Dim adapter = New OleDbDataAdapter(cmd)
data = New DataSet()
adapter.Fill(data, "Emp")
namebox.Items.Clear()
For i = 0 To data.Tables("emp").Rows.Count - 1
namebox.Items.Add(data.Tables("emp").Rows(i)("EmpFrontName"))
Next
End Sub
Private Sub empdetial()
Dim sql As String = "SELECT * FROM Employee WHERE EmpFrontName = @Fname"
cmd = New OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("Fname", namebox.SelectedItem)
adapter = New OleDbDataAdapter(cmd)
data.Clear()
adapter.Fill(data, "Fnameinfo")
EmpID.Text = data.Tables("Fnameinfo").Rows(0)("IDEmp")
name1.Text = data.Tables("Fnameinfo").Rows(0)("EmpLastName")
name2.Text = data.Tables("Fnameinfo").Rows(0)("EmpNickName")
positioning.Text = data.Tables("Fnameinfo").Rows(0)("EmpPosition")
duty.Text = data.Tables("Fnameinfo").Rows(0)("EmpJob")
Bdate.Text = data.Tables("Fnameinfo").Rows(0)("EmpBeginDate")
Money.Text = data.Tables("Fnameinfo").Rows(0)("EmpSalary")
StatusBox.Text = data.Tables("Fnameinfo").Rows(0)("StatusEmp")
End Sub
End Class
Private Sub EmpInfo()
'Dim sql As String = "SELECT DISTINCT(EmpFrontName) FROM Employee" ตรวจสอบค่าซ้ำ
Dim sql As String = "SELECT IDEmp,EmpFrontName FROM Employee WHERE EmpFrontName"
Cmd = New OleDbCommand(sql, con)
Dim adapter = New OleDbDataAdapter(Cmd)
Data = New DataSet()
adapter.Fill(Data, "Emp")
With NameBox
.DataSource = Data.Tables("Emp")
.DisplayMember = "EmpFrontName"
.ValueMember = "IDEmp"
End With
End Sub
Private Sub empdetial()
Dim sql As String = "SELECT * FROM Employee WHERE EmpFrontName = @Fname"
cmd = New OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("Fname", NameBox.SelectedValue)
adapter = New OleDbDataAdapter(cmd)
Data.Clear()
adapter.Fill(Data, "Fnameinfo")
EmpID.Text = Data.Tables("Fnameinfo").Rows(0)("IDEmp")
name1.Text = Data.Tables("Fnameinfo").Rows(0)("EmpLastName")
name2.Text = Data.Tables("Fnameinfo").Rows(0)("EmpNickName")
positioning.Text = Data.Tables("Fnameinfo").Rows(0)("EmpPosition")
duty.Text = Data.Tables("Fnameinfo").Rows(0)("EmpJob")
Bdate.Text = Data.Tables("Fnameinfo").Rows(0)("EmpBeginDate")
Money.Text = Data.Tables("Fnameinfo").Rows(0)("EmpSalary")
StatusBox.Text = Data.Tables("Fnameinfo").Rows(0)("StatusEmp")
End Sub
dropdownlist เวลาเลือกก็เอา id ของ Employee ไป where ครับ
ถ้า where EmpFrontName ชื่อมันซ้ำกันมันก็เลือกแต่อันแรก แต่ถ้าใช้ ID ของคนๆนั้นมันไม่ซ้ำแน่ๆครับ
Private Sub EmpInfo()
Dim dtAdapter As New OleDbDataAdapter
Dim dt As New DataTable
Dim sqlText As String = "select * from employee "
dtAdapter = New OleDbDataAdapter(sqlText, con)
dtAdapter.Fill(dt)
With namebox
.DataSource = dt
.DataValueField = "idemp"
.DataTextField = "EmpFrontName"
.DataBind()
End With
End Sub
Private Sub empdetial()
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim sqlText As String = "select * from employee where idemp = '" & namebox.SelectedValue & "' "
dtAdapter = New OleDbDataAdapter(sqlText, con)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
EmpID.Text = dt.Rows(0)("idemp")
...
...
...
End If
End Sub
ขออภัยตัวเดิมผมให้ไปเป็น DropdownList ของแบบ Web
T_T
เอาใหม่ๆ
Code (VB.NET)
Private Sub EmpInfo()
Dim dtAdapter As New OleDbDataAdapter
Dim dt As New DataTable
Dim sqlText As String = "select * from employee "
dtAdapter = New OleDbDataAdapter(sqlText, con)
dtAdapter.Fill(dt)
With namebox
.DataSource = dt
.DisplayMember = "EmpFrontName"
.ValueMember = "idemp"
End With
End Sub
Private Sub empdetial()
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim sqlText As String = "select * from employee where idemp = '" & namebox.SelectedValue & "' "
dtAdapter = New OleDbDataAdapter(sqlText, con)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
EmpID.Text = dt.Rows(0)("idemp")
...
...
...
End If
End Sub