Auto Complete Text Box from Access database - vb.net

I have a text box "Textbox1" and a set of 30,000 words stored in an access database. I would like to set the VB Textbox1's auto complete source to the access database. How do I do this in vb.net? I am a novice programmer at present.

Sample:
From an access Database
Create a DataSet in your vb project, connected to that database
Add New Item -> Data -> DataSet
In your .xsd designer, add a new TableAdapter, connect it to your Access file, create a query.
Query and add them to the TextBox.AutoCompleteCustomSource
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'NamesDataSet.Names' table. You can move, or remove it, as needed.
Me.NamesTableAdapter.Fill(Me.NamesDataSet.Names)
'get my names from the dataset
Dim myNames = From n In NamesDataSet.Names Select n.Name
TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
'add names to custom list
TextBox1.AutoCompleteCustomSource.AddRange(myNames.ToArray())
End Sub

Related

Delete record from Access database vb.net

In this project I use Access database which is displayed in DataGridView. I am trying to delete acces row but i my looking for was not successful.
Code to delete record from DataGridView:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim index As Integer
index = DataGridView1.CurrentCell.RowIndex
DataGridView1.Rows.RemoveAt(index)
ZoznamBindingSource.RemoveCurrent().
‘Dim da As OledbDataAdapter
‘Dim ds As dataSet
da.Update(ds)
End Sub
The last line of code give me an error: SystemNullReferenceException. I know rhe dataset is problem but i don’t know which code will replace it with.
Any solution?
The whole point of a BindingSource is that it is the one and only point of contact for bound data. You shouldn't have to touch the UI and you shouldn't have to touch the data source.
In your case, you should be calling RemoveCurrent on the BindingSource. That will flag the underlying DataRow as Deleted and then, when you call Update on your data adapter, the corresponding database record will be deleted.

Using Combo Box text for part of a Binding Source

Hello I am trying to display data in a grid view using a combo box containing multiple databases. Each database has a binding source. However, instead of coding a huge select statement is there a way that I can use the TableList.SelectedText and put it in front of the BindingSource so I don't have to code so many instances?
I tried doing it below but it does not work.
Private Sub TableList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TableList.SelectedIndexChanged
DataGridView1.DataSource = TableList.SelectedText(BindingSource)
DataGridView1.AutoGenerateColumns = True

VB 2013: how to update database using datagridview values

I have a DGV in my form that can be edited. I want to write back the updated values to the database so that they are available for next read.
I searched for answers around net and found couple of ways of doing it by opening a connection and updating it. However, i was wondering if there is any direct way of updating the database by using a single command.
I am a novice at VB and learning it only for a library project, hence find it difficult to understand and implement statements that deal with SQLs in the code. pls help... thanks
The proper approach to this is to use a data adapter to populate a DataTable that you then bind to the grid, then use the same data adapter to save the changes from that table back to the database. E.g.
Private adapter As New SqlDataAdapter("SELECT * FROM MyTable", "connection string here")
Private builder As New SqlCommandBuilder(adapter)
Private table As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Query the database and populate the DataTable.
adapter.Fill(table)
'Bind the data to the UI.
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
BindingSource1.EndEdit()
'Save the changes back to the database.
adapter.Update(table)
End Sub
The BindingSource would be added to the form in the designer.

I am trying to alphabetize a combo box using a query in my vb code. How do i get it to work Im having problems?

I want to know if its possible to somehow use a query in my vb code to alphabetize a list in a combo box that I have a dataset connected to?
Here is my code:
Private Sub ValueSourceAvailabilityBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles ValueSourceAvailabilityBindingNavigatorSaveItem.Click
Me.Validate()
Me.ValueSourceAvailabilityBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ValueTrackerDataSet)
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ValueTrackerDataSet3.SA_CountryCode' table. You can move, or remove it, as needed.
Me.SA_CountryCodeTableAdapter.Fill(Me.ValueTrackerDataSet3.SA_CountryCode)
'TODO: This line of code loads data into the 'ValueTrackerDataSet2.SA_Client' table. You can move, or remove it, as needed.
Me.SA_ClientTableAdapter.Fill(Me.ValueTrackerDataSet2.SA_Client)
'TODO: This line of code loads data into the 'ValueTrackerDataSet1.Cubes' table. You can move, or remove it, as needed.
Me.CubesTableAdapter.Fill(Me.ValueTrackerDataSet1.Cubes)
'TODO: This line of code loads data into the 'ValueTrackerDataSet.ValueSourceAvailability' table. You can move, or remove it, as needed.
Me.ValueSourceAvailabilityTableAdapter.Fill(Me.ValueTrackerDataSet.ValueSourceAvailability)
End Sub
Private Sub ClientIDComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ClientIDComboBox.SelectedIndexChanged
SELECT *
FROM dbo.SA_Client
ORDER by ClientName
End Sub
End Class
You can't just put a SQL statement inside of a VB .Net sub like that. You need to either use LINQ/Entity Framework, or format the query as a string and send it to SQL. The first method would depend on how your entity is set up, while the second method would look something like this:
'The SQL Connection
Dim sqlLConn As New SqlConnection()
'The SQL Command
Dim sqlCmd As New SqlCommand()
'Set the Connection String
sqlConn.ConnectionString = "connection string goes here"
'Open the connection
sqlConn.Open
'Set the Connection to use with the SQL Command
sqlCmd.Connection = SQLConn
' Set your query
sqlCmd.CommandText = "SELECT * FROM dbo.SA_Client ORDER by ClientName"
' DO STUFF WITH THE sqlCmd query here...
'Close the connection
sqlConn.Close()
First option: You can do it programatically by your sql Query
SELECT * FROM tableName ORDER BY columnName ASC/DESC
second option by settiong the Sorted option to TRUE
Would it solve your problem if the data you retrieved from the database already had the data sorted in ClientName order? In that case, you'll need to change the SQL associated with the Fill method on your SA_ClientTableAdapter data adapter.
If you need to sort the data separately from the DataSet and DataTable, you have a couple of options.
You can use LINQ, and bind to a sorted version of your data. Rather than binding directly to the DataSet or DataTable, you'd bind to an expression:
[whatever you're binding] = Me.ValueTrackerDataSet2.SA_Client.OrderBy(c=>c.ClientName)
You can use a DataView over the DataTable, which is (one of) the older ways we used to do this:
Dim dv as DataView = New DataView(Me.ValueTrackerDataSet2.SA_Client, "", "ClientName", DataViewRowState.CurrentRows)
[whatever you're binding] = dv

VB 2008 Displaying data in ListBox then Transfer the data to another ListBox

In VB 2008 I created 2 list box. The first list box is to load all the data in my database in a specific row, the other list box is when I double click on the data/item on the first list box the specific data/item need to be transfer to the second list box.
I manage to transfer the data, but the output it gave was wrong. Instead of the actual name of the given data/item the output it gave was System.Data.DataRowView. I tried using .ToString() but nothing happens. I used the drag and drop method for the data adapter connection and the database I'm using is MySQL. I use the "Use data bound items" on list box 1.
You should do it like this,
Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) _
Handles ListBox1.DoubleClick
' checks if the item is empty
If ListBox1.SelectedItem.ToString.Length <> 0 Then
' adds on listbox 2
ListBox2.Items.Add(ListBox1.Text)
End If
End Sub
See this,
with simple code you can use this
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox2.Items.Add(ListBox1.SelectedItem)
End Sub