Dim sql As String = "SELECT * FROM tbFaculty WHERE status = '1'"
Dim objCmd As New OleDbCommand(sql, objConn)
Dim reader As OleDbDataReader = objCmd.ExecuteReader()
While reader.Read()
cmbfac.Items.Add(reader("Faculty"))
End While
Public Sub FillComboBox(ByVal CBO As ComboBox, ByVal sSQL As String)
Dim DA As New OleDbDataAdapter(sSQL, CN)
DA.Fill(DS, "Table1")
CBO.DataSource = DS.Tables("Table1")
CBO.DisplayMember = DS.Tables("Table1").Columns(2).ToString
CBO.ValueMember = DS.Tables("Table1").Columns(1).ToString
DA.Dispose()
End Sub
Dim strSQL As String = "SELECT * FROM tbFaculty WHERE status = '1'"
Call FillCombox(cboFac,strSQL)
FacCode=cboFac.SelectValue
Dim sql As String = "SELECT * FROM tbFaculty WHERE status = '1'"
Dim objCmd As New OleDbCommand(sql, objConn)
Dim reader As OleDbDataReader = objCmd.ExecuteReader()
While reader.Read()
cmbfac.Items.Add(reader("ID") & " : " & reader("Faculty") )
End While
Option Explicit On
Imports System.Data.OleDb
Public Class Form1
Dim DS As DataSet
Dim CN As New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=" & Application.StartupPath & "\dbMain.mdb") '<==== CHANGE HERE
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CN.Open()
DS = New DataSet()
' ดึงข้อมูลมาใส่ combobox
Call FillComboBox(ComboBox1, "SELECT * FROM tbFaculty WHERE status = '1'")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'สิ่งที่คุณต้องการ ฤ เปล่า ??
MsgBox(ComboBox1.SelectedValue)
End Sub
'เอาไว้ select ข้อมูลมาใส่ใน combobox
'เวลาเรียกใช้ Call FillComboBox(ชื่อ combobox, "SELECT * FROM ......")
Public Sub FillComboBox(ByVal CBO As ComboBox, ByVal sSQL As String)
Dim DA As New OleDbDataAdapter(sSQL, CN)
DA.Fill(DS, "Table1")
CBO.DataSource = DS.Tables("Table1")
CBO.DisplayMember = DS.Tables("Table1").Columns(2).ToString
CBO.ValueMember = DS.Tables("Table1").Columns(1).ToString
DA.Dispose()
End Sub
End Class