Dim sql As String
sql = "คำสั่ง SELECT"
Dim da As New OleDbDataAdapter(sql, Conn)
Dim ds As New DataSet
da.Fill(ds, "tmp")
combobox.DataSource = ds.Tables("tmp")
combobox.DisplayMember = "ชื่อตาราง"
combobox.ValueMember = "ชื่อตาราง"
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim comboBox As New ComboBox
Dim daysOfWeek As String() = _
New String() {"Monday", "Tuesday", "Wednesday", _
"Thursday", "Friday", "Saturday", _
"Sunday"}
With comboBox
.DataSource = daysOfWeek
.Location = New System.Drawing.Point(12, 12)
.Name = "comboBox"
.Size = New System.Drawing.Size(166, 21)
.DropDownStyle = ComboBoxStyle.DropDownList
End With
Me.Controls.Add(comboBox)
End Sub
End Class
Dim sql As String
sql = "คำสั่ง SELECT"
Dim da As New OleDbDataAdapter(sql, Conn)
Dim ds As New DataSet
da.Fill(ds, "tmp") ' อันนี้ tmp ตั้งชื่อเป็นอะไรก็ได้ แต่ถ้าให้ดี ตั้งเหมือนชื่อ table ดีที่สุดจะได้ไม่ลืม
combobox.DataSource = ds.Tables("tmp")
combobox.DisplayMember = "ชื่อ ColumnName" 'อยากให้แสดงอะไร ?
combobox.ValueMember = "รหัส ColumnID" 'เก็บ เอาไว้อ้างอิง หรือเอาไว้บันทึกลง db
Public Sub ShowDataToCombo(ByVal _Tname As String, ByVal _FillShowname As String, ByVal _Cbo As ComboBox)
Dim dr As SqlDataReader
Dim CurrentItem As String
With Com
.CommandType = CommandType.Text
.CommandText = "Select * From " & _Tname & " "
.Connection = Conn
dr = Com.ExecuteReader
If dr.HasRows Then
_Cbo.BeginUpdate()
_Cbo.Items.Clear()
Do While (dr.Read())
CurrentItem = dr.Item("" & _FillShowname & "").ToString()
_Cbo.Items.Add(CurrentItem)
Loop
End If
End With
_Cbo.EndUpdate()
dr.Close() : dr.Dispose()
Private Sub Combo2_KeyDown(KeyCode As Integer, Shift As Integer)
Set RS = New ADODB.Recordset
RS.Open "select * from Table2 where SN = '" & Combo2.Text & "'", cn, adOpenKeyset, adLockOptimistic
If Not RS.EOF Then
Me.Combo1.Text = RS!Device
End If
RS.Close
Set RS = Nothing
End Sub