Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting Combo Box Defaults in vb Express

Posted on 2014-02-03
3
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
Kent Dyer earned 1500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

721 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