weight01
asked on
Populate Combos from DB and leave empty item
Hi
I am populating some combos from an access db but when they are populated the first record of the table is visible. How can i make this blank or have text such as [Enter User Name].
Thanks
Code for populating combos:
Dim conn As New OleDbConnection("Provider= Microsoft. Jet.OleDb. 4.0;Data Source=" & Application.StartupPath & "\ITAssets.mdb")
Dim UserAdapter As New OleDbDataAdapter("SELECT Name FROM tblUsers", conn)
Dim UserDT As New DataTable("tblUsers")
Call UserAdapter.Fill(UserDT) 'Execute SQL above and fill data table with records.
'Populate Combo Boxes
cboUser.DataSource = UserDT
I have tried adding a blank record to the DB and that sort of works but leaves a blank record as a choice.
Thanks
I am populating some combos from an access db but when they are populated the first record of the table is visible. How can i make this blank or have text such as [Enter User Name].
Thanks
Code for populating combos:
Dim conn As New OleDbConnection("Provider=
Dim UserAdapter As New OleDbDataAdapter("SELECT Name FROM tblUsers", conn)
Dim UserDT As New DataTable("tblUsers")
Call UserAdapter.Fill(UserDT) 'Execute SQL above and fill data table with records.
'Populate Combo Boxes
cboUser.DataSource = UserDT
I have tried adding a blank record to the DB and that sort of works but leaves a blank record as a choice.
Thanks
Does
cboUser.SelectedIndex = -1
not produce the effect you want?
Roger
cboUser.SelectedIndex = -1
not produce the effect you want?
Roger
ASKER
Roger
Thanks did not work
Thanks anyway
Thanks did not work
Thanks anyway
ASKER
Dave
Did not work
thanks anyway
Did not work
thanks anyway
What error or manifestation are you seeing? Are you sure the data table is populated?
ASKER
The combo box is being populated with the data, however, it always shows the first record. I would like that to be either blank or some text such as [Select a User]
ASKER
Just to clarify, I recieve no errors with my code and all combos are populated. I use the following to poplulate all the combos on a form:
Public Sub FillCombos()
Dim conn As New OleDbConnection("Provider= Microsoft. Jet.OleDb. 4.0;Data Source=" & Application.StartupPath & "\ITAssets.mdb")
Dim UserAdapter As New OleDbDataAdapter("SELECT Name FROM tblUsers", conn)
Dim DeptAdapter As New OleDbDataAdapter("SELECT Department FROM tblDepartment", conn)
Dim EFCTAGAdapter As New OleDbDataAdapter("SELECT EFCTAG FROM tblEFCTags", conn)
Dim ModelAdapter As New OleDbDataAdapter("SELECT Model FROM tblModel", conn)
Dim UserDT As New DataTable("tblUsers")
Dim DepartmentDT As New DataTable("tblDepartment")
Dim EFCTagDT As New DataTable("tblEFCTags")
Dim ModelDT As New DataTable("tblModel")
Call UserAdapter.Fill(UserDT) 'Execute SQL above and fill data table with records.
Call DeptAdapter.Fill(Departmen tDT)
Call EFCTAGAdapter.Fill(EFCTagD T)
Call ModelAdapter.Fill(ModelDT)
'Populate Combo Boxes
cboUser.DataSource = UserDT
cboUser.DisplayMember = ("Name")
cboDepartment.DataSource = DepartmentDT
cboDepartment.DisplayMembe r = ("Department")
cboEFCRef.DataSource = EFCTagDT
cboEFCRef.DisplayMember = ("EFCTAG")
cboModel.DataSource = ModelDT
cboModel.DisplayMember = ("Model")
'Types
With cboType
.Items.Add("Laptop")
.Items.Add("Desktop")
.Items.Add("Printer")
.Items.Add("Server")
.Items.Add("Switch")
.Items.Add("Hub")
End With
'Manufacturer
With cboManufacturer
.Items.Add("HP")
.Items.Add("Dell")
.Items.Add("Cisco")
.Items.Add("IBM")
.Items.Add("AMD")
.Items.Add("CMS")
.Items.Add("ZooStorm")
End With
'Supplier
With cboSupplier
.Items.Add("Supplier A")
.Items.Add("Supplier B")
End With
Public Sub FillCombos()
Dim conn As New OleDbConnection("Provider=
Dim UserAdapter As New OleDbDataAdapter("SELECT Name FROM tblUsers", conn)
Dim DeptAdapter As New OleDbDataAdapter("SELECT Department FROM tblDepartment", conn)
Dim EFCTAGAdapter As New OleDbDataAdapter("SELECT EFCTAG FROM tblEFCTags", conn)
Dim ModelAdapter As New OleDbDataAdapter("SELECT Model FROM tblModel", conn)
Dim UserDT As New DataTable("tblUsers")
Dim DepartmentDT As New DataTable("tblDepartment")
Dim EFCTagDT As New DataTable("tblEFCTags")
Dim ModelDT As New DataTable("tblModel")
Call UserAdapter.Fill(UserDT) 'Execute SQL above and fill data table with records.
Call DeptAdapter.Fill(Departmen
Call EFCTAGAdapter.Fill(EFCTagD
Call ModelAdapter.Fill(ModelDT)
'Populate Combo Boxes
cboUser.DataSource = UserDT
cboUser.DisplayMember = ("Name")
cboDepartment.DataSource = DepartmentDT
cboDepartment.DisplayMembe
cboEFCRef.DataSource = EFCTagDT
cboEFCRef.DisplayMember = ("EFCTAG")
cboModel.DataSource = ModelDT
cboModel.DisplayMember = ("Model")
'Types
With cboType
.Items.Add("Laptop")
.Items.Add("Desktop")
.Items.Add("Printer")
.Items.Add("Server")
.Items.Add("Switch")
.Items.Add("Hub")
End With
'Manufacturer
With cboManufacturer
.Items.Add("HP")
.Items.Add("Dell")
.Items.Add("Cisco")
.Items.Add("IBM")
.Items.Add("AMD")
.Items.Add("CMS")
.Items.Add("ZooStorm")
End With
'Supplier
With cboSupplier
.Items.Add("Supplier A")
.Items.Add("Supplier B")
End With
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Roger, you have confirmed to me that there is no staight forward solution.
As a solution I have already created a record in the DB user table called -- Select a User -- and included this code as part of the validation when clicking a save button to prevent a user saving that record :
ElseIf cboUser.Text = "" Or cboUser.Text = "-- Select a User --" Then
MessageBox.Show("Please enter a User.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information )
cboUser.Focus()
Thanks
As a solution I have already created a record in the DB user table called -- Select a User -- and included this code as part of the validation when clicking a save button to prevent a user saving that record :
ElseIf cboUser.Text = "" Or cboUser.Text = "-- Select a User --" Then
MessageBox.Show("Please enter a User.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information
cboUser.Focus()
Thanks
Yes, here is an extension of what Roger is talking about that I should have given you. What you do is disable the Submit button or the NextList until myList.SelectedIndex <> 0
Private Sub mylist_SelectedIndexChange d(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mylist.SelectedIndexChange d
If mylist.SelectedIndex = 0 Then
Submit1.Disabled = True
nextList.Enabled = False
Else
Submit1.Disabled = False
nextList.Enabled = True
End If
End Sub
Dave
Private Sub mylist_SelectedIndexChange
If mylist.SelectedIndex = 0 Then
Submit1.Disabled = True
nextList.Enabled = False
Else
Submit1.Disabled = False
nextList.Enabled = True
End If
End Sub
Dave
Private Sub FillList()
mylist.DataSource = UserDT
mylist.DataTextField = "Name"
mylist.DataValueField = "ID"
mylist.DataBind()
mylist.Items.Insert(0, "--- Select a name ---")
mylist.SelectedIndex() = 0
End Sub
hth
Dave