Filter databound combobox based on value in another column from the same dataset - vb.net

I am attempting to create a process scheduler as a project in school this summer and am quite new to using VB. Currently I have a combobox bound to the "EmployeeBindingSource" which holds information for every single employee in a company. Each employee is assigned a skill level for each work station in the building. What I would like to do is if an employee is listed as having "0" skill in say, the assembly station, when selecting an employee to assign to the assembly station, that particular employee will not appear in the combobox.
From doing some research I believe that rather than binding the combobox to the data source in the designer, I will have to assign values to the each combo box in the code on a form load.
This is the access table that holds all the employee information
In the table we can see that Steve has a skill level of "0" in the AS_Level category (assembly).
However, here we can see that he still appears as an option for the assembly area when creating a new project
Currently all the data binding happens in the designer for each combo box and therefore no code has been written for the data binding. At the moment each combo box is bound to the "Full_Name" column in the access table.
Again, I am quite new to VB so I apologize if this is too vague. Let me know if I can give any more helpful information. Thank you all in advance.
After adding the code suggested here is what I have
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
datafile = "C:\Users\Jacob\Desktop\Halton\HaltonProject.accdb"
connString = provider & datafile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "SELECT * FROM [Employee] WHERE (SP_Level <> '" & 0 & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
Dim userfound As Boolean = False
Dim Full_Name As String
While dr.Read
userfound = True
Full_Name = dr("Full_Name").ToString
SP_Emp1.Items.Add(Full_Name)
SP_Emp2.Items.Add(Full_Name)
SP_Emp3.Items.Add(Full_Name)
SP_Emp4.Items.Add(Full_Name)
End While
myConnection.Close()
Works completely now!

Based on your comments and post, I assume, the checkboxes on the left enable/disable the comboboxes on that row, and since you're binding them individually if you change your query so it looks like this:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Employee] WHERE AS_Level <> 0 , myConnection) Dim dr As OleDbDataReader = cmd.ExecuteReader
Note that the where clause will vary depending on your needs, so it could be WHERE AS_Level <> 0 or WHERE SP_Level <> 0 and so on.
I have never used an Ole database so I'm not quite sure about the syntax, hope this helps.

Related

How I handle duplicated record in textbox.AutoCompleteCustomSource in VB.net

I wrote a code for auto complete textbox in vb.net.
I have textbox search for the company name in 'company_list' table.
But some company have exactly same name, so when I input the name, it shows just 1 text.
I'd like to make these two names when I input the textbox.
Below is my code.
Dim sql as String = "SELECT * FROM company_info"
cmd = New MySqlCommand(sql, con)
con.Open()
adapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable
adapter.Fill(dt)
cName.AutoCompleteCustomSource.Clear()
For Each r In dt.Rows
cName.AutoCompleteCustomSource.Add(r.item(1).ToString)
Next
con.Close()
I have two same company name as below.
[TKTech]
[TKTech]
These two are totally different company with same name, so I need to choice between them.
When I input to textbox(cName), it just show 1 name for these company.
Please let me know how I handle it.
Thank you in advance.
Frank

List View Population Errors

I have 3 columns that need populating when a user presses 'search' however every time that I click 'search' only the Employee ID appears, neither the 'First Name' nor the 'Last Name' are present in the List View. The data does exist in my Access Database, this is proved as the program produces a blank record instead of a null value error. The code that I am using to populate the List View is:
ds.Clear()
lstClockin.Items.Clear()
con.ConnectionString = provider & datafile
con.Open() 'Open connection to the database
sqlstatement = "SELECT * FROM [EmployeeAccounts]"
da = New OleDb.OleDbDataAdapter(sqlstatement, con)
da.Fill(ds, "allmembers") 'Fill the data adapter
con.Close()
Dim recordCount, x As Short
recordCount = 0
x = 0
recordCount = ds.Tables("allmembers").Rows.Count
With ds.Tables("allmembers")
Do Until x = recordCount
lstClockin.Items.Add(.Rows(x).Item(0))
lstClockin.Items(x).SubItems.Add(.Rows(x).Item(1))
lstClockin.Items(x).SubItems.Add(.Rows(x).Item(2))
lstClockin.Items(x).SubItems.Add(.Rows(x).Item(3))
x = x + 1
Loop
End With
The first 3 columns in the Database are, [Employee ID], [First Name] & [Last Name]
Any suggestions are welcome; however I have ruled out using a DataGridView or any control. As this program needs to use a ListView. Thankyou in advance!
There are several things that can be improved in the code:
Dim SQL = "SELECT Id, Name, Fish FROM Sample"
Using dbcon As New OleDbConnection(ACEConnStr)
Using cmd As New OleDbCommand(SQL, dbcon)
dbcon.Open()
Dim lvi As ListViewItem
myLV.SuspendLayout()
Using rdr = cmd.ExecuteReader
Do While rdr.Read
lvi = New ListViewItem(rdr.GetInt32(0).ToString)
If rdr.IsDBNull(1) Then
lvi.SubItems.Add("")
Else
lvi.SubItems.Add(rdr.GetString(1))
End If
If rdr.IsDBNull(2) Then
lvi.SubItems.Add("")
Else
lvi.SubItems.Add(rdr.GetString(2))
End If
myLV.Items.Add(lvi)
Loop
End Using
myLV.ResumeLayout()
End Using
End Using
Connections and other DB Provider objects allocate resources which need to be released or your app will leak. Using blocks for things that implement Dispose will close and dispose of them for you
There is no need for an DataAdapter, DataSet and DataTable since you are copying the data to the control. This code uses a DataReader to get the data.
Rather than SELECT * the query specifies the columns/order so it can use the Getxxxxx methods to get typed data. That doesnt matter a great deal in this case because everything gets converted to string for the ListView. lvi.SubItems.Add(rdr(COLUMN_NAME).ToString()) would also work.
It seems unlikely the ID column could be null, so the code only checks the other 2 for DbNull (another thing the DGV can handle without help).
Since the ListView is suboptimal and slow in adding items, SuspendLayout and ResumeLayout are used to minimize paints while it is populated.
I am not at all sure what ...the program produces a blank record means, but in order use a ListView like it is a grid, the View property must be Details and you have to have added 3 columns in the IDE (or manually create them in code). Nothing will show without those settings.
If the DataTable is needed/used elsewhere, you can still fill one without a DataAdpater and populate the LV from it:
...
dt.Load(cmd.ExecuteReader)
For Each row As DataRow In dt.Rows
lvi = New ListViewItem(row(0).ToString())
If DBNull.Value.Equals(row(1)) Then
lvi.SubItems.Add("")
Else
lvi.SubItems.Add(row(1).ToString())
End If
If DBNull.Value.Equals(row(2)) Then
lvi.SubItems.Add("")
Else
lvi.SubItems.Add(row(2).ToString())
End If
myLV.Items.Add(lvi)
Next
This uses a different DBNull check since it is using a DataRow and not the DataReader.

VB.Net and Access - Getting Totaliser Value

I'm trying to databind a textbox to a totaliser value in an access database. I currently update the database via OleDbCommand and then edit any existing entries via databinding on the form.
I have everything working fine, but I want a textbox to show the totaliser (sum) of a particular column in the datbase. Access shows this totaliser underneath the column if the database is opened.
Is there a method to bind this value to the textbox?
Thanks
Well, however you are accessing the database, you need to make a call to get the SUM of your desired table.
If we are talking about SQL, it would look something like:
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = " SELECT SUM(total) AS Total From YourTable"
cmd.CommandText = sSQL
OleDbDataReader dr = cmd.ExecuteReader()
If dr.Read() Then
set total = Convert.ToInt32(dr["Total"])
End If
You could load this into a DataTable/DataSet or use the DataReader and assign the result (the sum) to a textbox, like:
TextBox1.Text = total
If you are using Linq it could look like (this is just an example):
Dim yourObject = From cust In db.Customers
Group By cust.City
Into Average(cust.Orders.Count)
Order By Average
DataGridView1.DataSource = yourObject

How to get specific value in MS Access using VB.net

I'm trying to get the specific value in my Ms Access Database..
Example.
Here is my table in Ms Database..
I have 3 Columns, that is Name,Age and Telelphone number.
Here are the values ..
Row 1 = [Name = John ,Age =13,Telephone Num = 456)].
Row 2 = [Name = Mark, Age =11,telephone num = 123)].
Row 3 = [Name = Maye, Age =15,telephone num = 789)]
then Example I only want to get the Age of Mark and that is 11
or John's telephone number and that is 456..and
display it using textbox.
All I want is to get specific value and I don't care about the other value..
Well im Using Visual Studio 2012 and MS Access 2010..
heres my code..
Im new about VB.net so that im not relly familiar about the codes..^_^
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Vincelouie\Desktop\FBES INFO SYSTEM\Database1.accdb")
con.Open()
Dim selectString As String = "SELECT Age FROM Table1"
' this line passes in the SQL statement and the OleDbConnection object.
Dim cmd As OleDbCommand = New OleDbCommand(selectString, con)
'Send the CommandText to the connection, and then build an OleDbDataReader.
Dim reader As OleDbDataReader = cmd.ExecuteReader()
reader.Read()
StudentTextBox.Text = reader.GetValue(0).ToString()
'Close the reader and the related connection.
reader.Close()
con.Close()
Thank you..
All help are really appreciated..
I'm not going to teach you all about ADO.NET here. If you want to learn the basics of data access in VB.NET then there are plenty of places that you can learn that. Here are some code examples:
http://www.vbforums.com/showthread.php?469872-Retrieving-and-Saving-Data-in-Databases
Addressing this question specifically, you will create an OleDbCommand containing an appropriate SQL query and call ExecuteScalar. That thread I linked to provides an example.

VB.NET 2010 database search

I have a ms access database connected to my vb application through the data wizard.
i want to allow the user to search the database and display their results on a datagrid.
for example user searches for 50 – 55 year Old man under 1.8 meters in height
so far i can display the total amount of people on the database with this code
Private Sub lblTotalPeople_Click(sender As System.Object, e As System.EventArgs) Handles lblTotalPeople.Click
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AssignmentDatabase.accdb")
' Use wildcard'
Dim cmd As OleDbCommand = New OleDbCommand("Select COUNT(*) From Table1", con)
'' or Where username='" & TextBox1.Text & "'
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "Table1")
DataGridView2.DataSource = myDataSet.Tables("Table1").DefaultView
End Sub
how would i search the database based on what the user searches or what would i use?
Assuming you don't already you need to learn how to use SQL.
In your code above the SQL statement is
Select COUNT(*) From Table1
You would need to replace this SQL with a search that uses a value from the user (most likely from a textbox). This text
'' or Where username='" & TextBox1.Text & "'
appears to be part way to some SQL that may work but it looks dangerous. You should also research SQL Injection as using this directly means users could access/damage your Access database.