I'm working on a project where a form will pull information from an Access database. We have techs that are assigned to various stores, and instead of listing all of the stores and assigning a tech, I was looking to have a list of techs and assigning stores. Right now, the database has 4 fields.
Gate Tech - Text Field
Expiration Date - Date Field
Doors - Yes/No checkbox
Stores - Lookup & Relationship field that can have multiple selections
My form has a text box, search button, and a rich text box. If someone puts in a store number, it searches the Lookup & Relationship field and if that store is checked then it will put the tech in the rich text box.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim text1 As String = TextBox1.Text
myConnection.Open()
Dim lstscmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1 WHERE Stores = #Stores", myConnection)
lstscmd.Parameters.AddWithValue("#Stores", CInt(text1))
dr = lstscmd.ExecuteReader
While dr.Read
RichTextBox1.AppendText(dr("Gate Tech").ToString + Environment.NewLine)
End While
myConnection.Close()
End Sub
At While dr.Read I get
Row handles must all be released before new ones can be obtained.
I would like it search the Stores field for any selected stores, and return the Gate Tech value to the rich text box.
Any help with this would be greatly appreciated.
I found where I was going wrong and the best way to solve it.
Instead of pulling the data from the table field, I created a query in access and pointed my search to that.
Dim lstscmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1 WHERE Stores = #Stores", myConnection)
lstscmd.Parameters.AddWithValue("#Stores", CInt(text1))
Replace Table1 (or your table name) with Query1 (or your query name) and it should start pulling values.
Related
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
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.
I'm trying to fill either a combobox or a list with an sql query, I can get them to produce the number of entries pulled but not the names of the entries, and not multiple entries.
The code in question is simple:
Dim RegisterApt As New StudentsDataSetTableAdapters.TestTableAdapter
Try
txtTestPull.Items.Add(RegisterApt.FillByStudentsTest(StudentsDataSet.Test, StudentInsert.School, StudentInsert.School))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
What I can't seem to find online is how to do this.
All I'd like to do is pull results using my sql query which I know works,
and push the resulting rows to the list or combobox
Here's the step by step on how to fill a ComboBox and a DataGridView using ADO.Net. I use the Northwind database as a sample.
1. Add a DataSet
Right-click your project and choose Add, then choose New Item. Choose Dataset in the next window.
2. Add a DataTable
Connect to your database and drag a table into the middle area. In this example, I choose the Customers table.
3. Add a query
Right-click your DataTable, and choose Add, then choose Query.
Choose Use SQL statements in the next window, click Next.
Choose SELECT which returns rows in the next window, click Next.
4. Write a query
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax
FROM dbo.Customers
WHERE Country = #Country
I add a WHERE clause to filter the data. Click Next.
Gives names to your methods. I use FillByCountry in the first textbox and GetDataByCountry in the second textbox.
Save your project and build it first because you're adding a new DataSet.
5. Add some controls in your form
Add a Button, a TextBox, a ComboBox and a DataGridView. You can change the names, but I use the default names in this example.
6. Write some code to get the data and bind it to ComboBox and DataGridView
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' declare a DataTable
Dim dt As New DataSet1.CustomersDataTable
' declare a DataAdapter
Dim da As New DataSet1TableAdapters.CustomersTableAdapter
' use the DataAdapter to fill the DataTable
da.FillByCountry(dt, TextBox1.Text)
' bind the DataTable to a DataGridView
DataGridView1.DataSource = dt
' bind the DataTable to a ComboBox
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "CustomerID"
ComboBox1.DisplayMember = "CompanyName"
End Sub
End Class
7. Run the project and see the result
I have a combobox created and a table where I have first and last name both as columns. I want my columnbox to list the names of the first and last names put together and I'm not sure how to progress. I tried changing my DisplayMember to "First Name" but that only displays the first name when I run the program, I want BOTH.
This code was automatically added to my .vb file when I changed the displaymember to first name only:
Private Sub ComboBox3_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
End Sub
Is there something I must add here? I'll stay here diligently until I get an answer and answer questions.
.
.
.
.
.
.
.
.
.
.
.
.
.
ALSO
WHEN I Used a binding script to update my tables immediately, can I use a modified one to update the combobox immediately?
Private Sub BindGridSalespeople()
Dim constring As String = "server=classified;database=classified"
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT * FROM Salespeople", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
If you cannot add column to the DataSource used for filling ComboBox with values
Then add Computed Column in your SQL Table
ALTER TABLE YourTable ADD FullName AS (FirstName + ' ' + LastName);
Then use new column for DisplayMember in the combobox
ComboBox.DisplayMember = "FullName"
But I think adding column to the database only for UI purpose isn't best approach.
Exist much methods to add columns to the DataTable(Expression Column) if you using DataTable
Or adding column in the Select statement of SQL query
Or playing with properties of entities if you using Entities Framework
I want to find a specific value from an Access database. Then manipulate this value as part of validation (weigh a user inputted value against a min and max values in the database)
I can get the data in from the database but I can't seem to fill a data table. I want to user to type in the primary key and then when the button is clicked an SQL statment uses that input to find the value then put that value into a Data Table and spit that value out on a textbox. I figure if I can get it spitting out the right value then I can put the value into a variable that I can manipulate.
I don't need to change the data in the database I just need to collect the values in order to verify user entered data. Here is the code I have so far.
Private Sub btnValidate_Click_1(sender As System.Object, e As System.EventArgs) Handles btnValidate.Click
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
dbConnect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\crabara\Desktop\Project Alpha 3\MDB.accdb;Persist Security Info=False;"
dbConnect.Open()
da = New OleDb.OleDbDataAdapter("SELECT MoldVinylWeightMin FROM PROCESS_INFO WHERE PCRNumber =" & txtPcr.Text, dbConnect)
da.Fill(ds, "MinWeight")
TextBox1.Text = ds.Tables("MinWeight").Rows(0).Item(0)
End Sub
Right now it throws the error "Data type mismatch in criteria expression." Any help would be great, I can't find much info on this.
If PRCNumber is a numeric type in the database you will not need to quote it
so: PCRNumber ='" & txtPcr.Text & "'"
becomes
PCRNumber = " & txtPcr.Text