Solved

Setting Combo Box Defaults in vb Express

Posted on 2014-02-03
3
230 Views
Last Modified: 2014-02-06
I have a combobox linked to an SQL table, see attached code

'Read SQL data source in combobox
        cmd = New SqlCommand("SELECT * FROM tbl_ProductStandards", con)
        If con.State = ConnectionState.Closed Then con.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader()
        While sdr.Read = True
            cboProductCode.Items.Add(sdr.Item("ProductCode"))
        End While
        sdr.Close()

But i would like to make an automatic selection in the combo box on form open if the combo box is blank

I have tried a variety of things including .selecteditem and although the code does not error, nothing vible ever appears in the combobox.

All suggestions very welcome.
0
Comment
Question by:SweetingA
3 Comments
 
LVL 17

Accepted Solution

by:
Kent Dyer earned 500 total points
ID: 39831765
What you are looking for is the selected attribute..

<option value="audi" selected>Audi</option>

Open in new window

You will want to make this the default position if you will.
0
 
LVL 40
ID: 39831790
cmd = New SqlCommand("SELECT * FROM tbl_ProductStandards", con)
        If con.State = ConnectionState.Closed Then con.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader()
If srd.HasRows then
       While sdr.Read = True
            cboProductCode.Items.Add(sdr.Item("ProductCode"))
        End While
Else
     cboProductCode.Items.Add("Sorry, no product to show")
End If
       sdr.Close()
0
 

Author Comment

by:SweetingA
ID: 39834132
Hello KentDyer,

I have tried various forms of using selected property but none work (no error just no visble selection in combo box field)

Could you please list the exact code
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now