September 17th, 2012, 03:01 AM
Combo Box problem - how to populate data
HI to all,
I have a combo box. my problem is that, i have 4 textbox below
here are the data from frmPilotShiftingSchedules.frm
My problem is this:
how can i automatically load data in textbox,for example, if you choose a name or a value in combo box cboPilot , the data will also appear to textbox? Thanks for the help..
here is my code
Private Sub LoadComboBoxes_DB()
Dim dt As DataTable
dt = CollectData("SELECT Pilot_ID,Pilot_Name + ' ' + Emp_No AS Pilot_Name FROM vw_Pilot ")
If dt.Rows.Count = 0 Then Exit Sub
Me.cboPilot.DataSource = Nothing
Me.cboPilot.DataSource = dt
Me.cboPilot.ValueMember = "Pilot_ID"
Me.cboPilot.DisplayMember = "Pilot_Name"
Me.cboPilot.SelectedIndex = -1
here is the 2nd code
Private Sub cboPilot_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPilot.SelectedValueChanged
Dim dt As DataTable
'If bLoad = True Then Exit Sub
'dt = CollectData("select * from vw_Pilot_Scheduling where Pilot_ID = '" & Me.cboPilot.SelectedValue & "'")
dt = CollectData("SELECT Pilot_ID,Pilot_Name + ' ' + Emp_No AS Pilot_Name FROM vw_Pilot_Shifting ORDER BY Pilot_Name ")
'dt = CollectData("SELECT * from vw_Pilot_Shifting")
If dt.Rows.Count > 0 Then
Me.txtScheduleNo.Text = dt.Rows(0)("Schedule_No")
Me.txtMonth.Text = dt.Rows(0)("Month_No")
Me.txtTimeFrom.Text = dt.Rows(0)("Day_Time_From")
Me.txtTimeTo.Text = dt.Rows(0)("Day_Time_To")
September 17th, 2012, 12:58 PM
With a Combo Box, the Click event places the item selected from the list in the text box portion and collapses the list. Therefore I use the dblClick event to react to the contents. I also use the KeyPress event to call the Click event if an [Enter] (Chr$(13)) is detected. The normal process would then be to click a member of the list and then hit the [Enter] key. The user can also use the arrow keys to navigate up and down the list, and then just enter when the correct one is found. If the list is long, and you want to give the user the opportunity to narrow down the list with key strokes, you should load the Combo Box alphabetically sorted and make sure the first item in the list is invalid (I use No Match with a space in front of it).
I trust this is what you meant.
September 17th, 2012, 09:02 PM
Originally Posted by couttsj
Okey sir, Opps., i did not understand fully. For your info, im just a newbie in vb2010., and i've not practiced programming for a very long period of time.
can u give me a light example?? i am very sorry for disturbing you.
September 18th, 2012, 01:36 PM
You are using VB.Net and my experience is with VB6, so there may be some slight differences in application. But the basic operation of the Combo Box should be the same. The list contents of the Combo Box or the List Box is a single string with each element separated by a Null character (Chr$(0)). The Combo Box consists of a Text Box and a List Box. When you click on an element in the list of a Combo Box, that element is copied to the Text Box and the ListIndex property is updated to point to that element. Both of these are now available for use, the list is collapsed, and the focus is on the Text Box portion. If you intercept the [Enter] keystroke (Chr$(13)) in the KeyPress event, you can process this information further.
Originally Posted by xpasaway
I have included an example from a working program below that recovers customer information from a database. The list portion of the Combo Box is loaded from a 2 dimensional array that relates the Customer Name to the Customer Number. This is necessitated because there can be identical names, but the numbers are unique. The array is loaded by a database query sorted by name, but the first element of the list is always loaded with an invalid name that will always be at the top of the list:
ABC AUTO IMPORTS
The list will attempt to find whatever is typed into the Text Box. If an invalid name (or partial name) is entered, the list will always go to the top, and selecting the first item (ListIndex = 0) will produce an error message, because it is invalid. I am using a Combo Box configured as a Dropdown Combo, because I want to allow new customers to be added here, but if you do not want to allow any entry, you should configure it as a Dropdown List. You will also notice that I am issuing a system call to automatically show the Dropdown list whenever the Combo Box receives the focus (this will be different in .net).
Edit: The space in front of No Match got removed in transmission.
Private Sub Customer_Click()
LSelect.Visible = True
If Customer.ListIndex < 0 Then Exit Sub
CustNum.Text = CustListData(1, Customer.ListIndex)
Private Sub Customer_DblClick()
Dim Result As Long
Dim vKeys As Variant
Dim vExceptions As Variant
Screen.MousePointer = 11
vKeys = "SELECT CustName, CustNo From Customer ORDER BY CustName;"
Result = GetLocalData(vKeys, CustListData, vExceptions)
For N% = 0 To UBound(CustListData, 2)
Customer.AddItem CustListData(0, N%)
ListFlg = False
Screen.MousePointer = 0
Private Sub Customer_DropDown()
If ListFlg Then
ListFlg = False
Private Sub Customer_GotFocus()
Tmp% = SendMessage(Customer.hWnd, CB_SHOWDROPDOWN, 1, ByVal 0&)
Private Sub Customer_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Customer.ListIndex < 0 Then
MsgBox "No Selection Made!"
RecNo = CustNum.Text
LSelect.Visible = False
Last edited by couttsj; September 18th, 2012 at 01:39 PM.