Usuario
uso de combo box

Pregunta
-
buenos días, tengo una duda tengo un proyecto escolar:
tengo 2 combobox y una tabla en acces, lo que tiene que hacer es que al seleccionar un combobox con un valor el otro combo me deje seleccionar otro valor diferente pero de la misma lista de valores. si me podrian porfavor. gracias.
- Cambiado Karen Malagón viernes, 27 de marzo de 2015 22:42 Programming with Access
Todas las respuestas
-
Hola:
En un Form con 2 ComboBox, copia y pega el siguiente codigoOption Explicit On
Option Strict On
Imports System.Data.SqlClientPublic Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Me.ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList
Dim lsQuery As String = "Select EmployeeID, LastName From Employees Order By LastName"
lP_CargaCombo(Me.ComboBox1, lsQuery, "LastName", "EmployeeID")
lP_CargaCombo(Me.ComboBox2, lsQuery, "LastName", "EmployeeID")
End SubPrivate Sub lP_CargaCombo(ByRef cboComboBox As ComboBox, ByVal vsQuery As String, ByVal vsVisualiza As String, ByVal vsValor As String)
Dim CadConexion As String = "Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
Try
Dim loDataTable As New DataTable
Using loConexion As New SqlConnection(CadConexion)
Using loDataAdapter As New SqlDataAdapter(vsQuery, loConexion)
loDataAdapter.Fill(loDataTable)
End Using
End Using
If loDataTable.Rows.Count > 0 Then
'Creamos un nuevo registro para el elemento en blanco
Dim loFila As DataRow = loDataTable.NewRow()
loFila.Item(vsValor) = 0
loFila.Item(vsVisualiza) = ""
' Insertamos la nueva fila en la primera posición del objeto DataTable.
loDataTable.Rows.InsertAt(loFila, 0)
End If
With cboComboBox
.DataSource = loDataTable
.DisplayMember = vsVisualiza
.ValueMember = vsValor
.SelectedIndex = -1
End With
Catch ex As Exception
MessageBox.Show(ex.Message, "lP_CargaCombo", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End SubPrivate Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
If CInt(Me.ComboBox1.SelectedValue) = CInt(Me.ComboBox2.SelectedValue) Then
MessageBox.Show("Es el mismo valor que el combo 2")
Me.ComboBox1.SelectedIndex = -1
End If
End SubPrivate Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
If CInt(Me.ComboBox2.SelectedValue) = CInt(Me.ComboBox1.SelectedValue) Then
MessageBox.Show("Es el mismo valor que el combo 1")
Me.ComboBox2.SelectedIndex = -1
End If
End Sub
End ClassP.D.
El ejemplo es para SQL, asi que lo tendras que cambiar para Access
Un saludo desde Bilbo
Carlos- Propuesto como respuesta Karen Malagón viernes, 27 de marzo de 2015 22:41
-