Enter text from text box into combo box vb.net - sql-server

I have two form, A and B.
On FORM A user will select a country code from combobox and it will then be saved to DB.
On FORM B a textbox shows the country code that was saved to the database earlier.
I want to change the country code in FORM B when edit is selected.
How to change:
1. the textbox will first be hidden
2. a combobox with all the country codes will be shown with selected value equals to the hidden textbox value.
I have tried putting the info into the combobox like the textboxes straight from the database when it is blank, e.g:
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
but this does not work. I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong. Is there a way of doing this? I have a work around where I don't let the user update the info if the combobox is blank, but I want the country code to be already there so that the user does not have to select a country code again if it is not wrong. Any help with this problem would be greatly appreciated.
EDIT:
datatable.Rows(0).Item(2)
holds a country code, for example, Ireland (+353), United Kingdom (+44) or U.S.A. (1).
This is the code I have for calling the information from the database:
sqlVisitorDetails = "SELECT * FROM visitorDetails WHERE idNumber=#idNumber"
sqlCon.Open()
sqlCmd = New SqlCommand(sqlVisitorDetails, sqlCon)
sqlCmd.Parameters.AddWithValue("#idNumber", txtIdNumber.Text)
dtVisitorDetails = loadDtVisitorDetails()
txtFirstName.Text = CStr(dtVisitorDetails.Rows(0).Item(1))
txtLastName.Text = CStr(dtVisitorDetails.Rows(0).Item(2))
txtContactNumber.Text = CStr(dtVisitorDetails.Rows(0).Item(3))
txtCountryCode.Text = CStr(dtVisitorDetails.Rows(0).Item(4))
txtAddress.Text = CStr(dtVisitorDetails.Rows(0).Item(5))
The country code (e.g. 'Ireland (+353)') is stored in dtVisitorDetails.Rows(0).Item(4) and this is put into the text box txtCountryCode.
When edit is clicked on the form, the text box txtCountryCode is hidden and the combobox cbCountryCode is visible (before edit is clicked txtCountryCode is shown and cbCountryCode is hidden). I then want the country code (in this case 'Ireland (+353)') to be shown in the cbCountryCode combo box. At the moment when the combobox is shown it is blank and the user has to choose a country code again, even if it's right. I hope this makes things clearer.

From the best i can understand from your question.
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
will not work if the DropDownStyle in the properties is set to DropDownList, change it to DropDown instead(if its not).
And to this:
I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong.
you have to bind the data to the ComboBox to make it work.
EDIT:
If possible, use ColumnName instead of Index for getting a data from datatable. Since you're selecting all record from your database, your may not know when then index would change(when a column is added or deleted from DB)
cbCountryCode.Text = CStr(dataTable.Rows(0).Item("CountryCodeColumn"))

How about that?
cbCountryCode.ClearSelection()
cbCountryCode.SelectedIndex = cbCountryCode.Items.IndexOf(cbCountryCode.Items.FindByText(datatable.Rows(0).Item(2).ToString))

Try this;
cbCountryCode.SelectedItem = CStr(dataTable.Rows(0).Item(2))
The .Text property is the currently selected text in the combo, not the currently selected item.
EDIT: (version 3!)
This definitely works! Create a new project, with a new WinForms project and Form1, etc. Add a button (btnTest) and a combobox (cboTest) and paste this code in;
Private Sub Form1_Load() Handles MyBase.Load
' Add some items
cboTest.Items.Add("U.S.A (+1)")
cboTest.Items.Add("Ireland (+353)")
cboTest.Items.Add("U.K. (+44)")
' Select the first item
cboTest.SelectedIndex = 0
End Sub
Private Sub btnTest_Click() Handles btnTest.Click
' Select the UK entry
cboTest.SelectedIndex = cboTest.FindString("U.K.")
End Sub
And I hate to have to say it, but it works like a charm! I've tested it with the combobox set to DropDown AND to DropDownList and the result is the same. Please check you have the right data going into and out of your routines from the dataTable/Row/Item you are using!!

Try simple:
comboBox.Items.Add("Item 1");

Related

ComboBox blank text with SQL Server

I have here the code of my combobox in VB.net which is connected to my SQL Server 2012.
Public Sub LoadCBXStocks(Optional QueryForCBXStocks As String = "")
If QueryForCBXStocks = "" Then
SQL.ExecQuery("SELECT DISTINCT Category FROM tblProduct ORDER BY Category ASC;")
Else
SQL.ExecQuery(QueryForCBXStocks)
End If
If SQL.HasException(True) Then
Exit Sub
cbxStocksCategory.Items.Clear()
For Each r As DataRow In SQL.DBDT.Rows
cbxStocksCategory.Items.Add(r("Category").ToString)
Next
cbxStocksCategory.Items.Add("")
End Sub
I'm trying to add a blank text to my combobox and I've successfully done it. Now my problem is when I select the blank text in my combobox, all of my data in my listview goes to blank data BUT when I select the items that is not blank text (as you can see in my for each loop, the Category is the column name and under that I have Drinks, Grilled etc.). My Listview shows the data which is base also on my query that you can see in if else condition.
What I want is when I select the blank text on my combobox. My Listview will show all the data like when it is first show (I mean without filtered)
If you, reading this cannot get specifically what I want please bear with me and I will try to edit it so you can get my thoughts.
Thank you.

Macro won't select value in ComboBox

I'm working on an asset management database for my company. Part of its function will be to track where loaner computers go. So I have built a 2 level navigation form (NavigationFRM).
What I would like to happen is when you select a tab on the the top of the form, it automatically fills in information on the sub form of the form. To do this:
I have created an embedded macro that will select a value on a hidden combo box;
After update event on the combo box will fill out information on the form.
The problem is the macro works and sets the value of the combo box, but that's it. It doesn't trigger a change of info in the other fields.
I'm told that I have to a re-query on the combo box, but I can't access it as it's on a sub form. If I could get the macro to access that combo box and re-query the data, then I could move on.
Main form = NavigationFRM
sub navigation form = LoanerFRM1
Combo box = LoanerSelectCMBO
Please let me know of any other info you may need to assist.

How to Populate Label with String Array on ComboBox Item Selection in VB.Net

I have an application in VB.Net where I'm trying to fill a label with some string data that I have in an array. I have a ComboBox which holds some states as the index collection/values. When I select a particular value in the combobox, I want to pull string data from the assigned array, and populate the label with it as a "clickable link a browser window. I'm lost on this, yet here is what I have in my code stub:
Private Sub cboSelectState_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSelectState.SelectedIndexChanged
'Create a string array
Dim AlabamaCities() As String = {"http://www.rolltide.com/", "http://www.crimsontidehoops.com/", "http://centralalabamapride.org/"}
Dim strAlabama As String
'Populate label with the array data, on a particular value selection in combo box.
If cboSelectState.SelectedValue("Georgia") Then
strAlabama = CStr(AlabamaCities(3))
lblLinkOutput.Text = strAlabama
End If
End Sub
So when I pick Alabama in my combo box, I want the label to show:
http://www.rolltide.com
http://www.crimsontidehoops.com
http://centralalabamapride.org
The links will be clickable from the label and populate in the same tab whenever clicked. I haven't tried the clickable link part yet, and I will try once I get this down.
I know it's probably bad starting form out the gate. But I'm trying to get the form down to gain the knowledge and plan out a bigger project, and accomplish something better when I think of it. I appreciate your knowledge and assistance.
Firstly, it would make sense to use a Dictionary to store the data. The state names will be the keys and the values would be the arrays of URLs. You would then display the keys in the ComboBox and, when a selection is made, use the selected key to get the corresponding value from the Dictionary.
At that point, you won't be using a Label if you want clickable links. You should use a TableLayoutPanel as a container and then add one LinkLabel to the table for each URL in the array. You can then use a single handler for all the LinkClicked events.

MS ACCESS 2007 restrict editing to form fields until combobox selection is made

How do i restrict the editing of form fields until a combobox selection is made? I am using MS Access 2007.
Thanks for your help.
On the form, you can start off with all the fields being locked (cannot edit) and disabled (cannot click on them) (you can find those in the property sheet of the form). Then on the On Change event of the combo box, in VBA, cycle through all the fields and enable them again.
This answer provides code for how to loop through controls on a form. In this example, you would set ctl.Enabled = True and ctl.Locked = False (where ctl is the control you are referencing)
A note, to make sure that the combo box is always changed, I usually put "Choose One..." as the first item of the list. And check it on form submission to make sure it's not still selected. That way, it forces the On Change event to fire where it wouldn't if someone used the first option of the list.
Hope that was at least enough to get you started...
EDIT: Additional info
In the table design, I put default value of "Choose 1" on the Combo Box and made it the first option. So when the form comes up, that's what you see.
You can set the Locked property of your memo and date fields to Yes (found in the property sheet under data or you can use the .Locked = True in VBA.
Then, in the on change event of the combo box, click the ... and use the code builder and put something like this:
Dim ctl As Control
If Me.Combo77.Value <> "Choose 1" Then
'Loop through all controls on form
For Each ctl In Me.Controls
'Only text boxes, not labels, or combo box ect.
If ctl.ControlType = acTextBox Then
'Unlock the field and allow editing
ctl.Locked = False
End If
Next
End If
I tried it out and it works on my little test database.
Here is the MSDN article on the control types: MSDN Control Types
To do it this way, I would recommend what I said above and put the first item of the Combo Box something they're not going to choose.

Get data into one form field based on another's field in access 2003

I am trying to update existing form field based on another combo box selection.
I have a DB with such relations: http://img696.imageshack.us/img696/7396/relationse.jpg
I have a such form: http://img233.imageshack.us/img233/9758/getdata.jpg
Which Offers to select only existing IDs in database.
All i want to do is when user selects (change) existing combo box "Filmas_ID" value, in nearby input box with value "Unbound" shows up data related to this ID (You know - Just for informative reasons).
(SELECT Filmas.Nosaukums FROM Filmas WHERE Filmas_ID = combo box "Filmas_ID" value; )
It looks very simple, but i had no success yet to get it work.
Thanks for your help! =)
There are a few handy ways to do this, one involves including the data in the combo. Let us say the combo is set up like so:
RowSource : SELECT Filmas_ID, Nosaukums FROM Filmas
ColumnCount : 2
You now have a choice of setting the column widths so that filmas_id is selected, but Nosaukums is shown in the combo. Alternatively, you can set the textbox to reference the combo column, like so:
= mycombo.column(1)
Where columns count from zero.
Another method is to use DlookUp in the control source of the textbox:
= DLookUp("Nosaukums","Filmas", "Filmas_ID = " & mycombo)
Where Filmas_ID is numeric and the required value is in the bound column of the combo.

Resources