Use Module in AutoAppend and Suggest Textbox Using Visual Basic 2008 and MS Access Database

On my first tutorial I teach you how to AutoSuggest a Textbox Using Visual basic 2008 And MySQL Database. Now, in this Tutorial I will show you how to Module the AutoAppend and Suggest a TextBox using Visual Basic 2008 and MS Access Database. An AutoAppend TextBox is when you type a data in a TextBox it will append instantly, whatever, first record you’re going to search. For instance, when you type in the Web Browser.
Let's Begin: 1. Create a Module in the Visual Basic 2008 named “autoappend” and set up a connection from MS Access Database to Visual Basic 2008.
  1. Module autoppend
  2.     'OleDbConnection instance takes Connection String as an argument and the value is pass to the Constructor statement.
  3.     Public Function mydb() As OleDb.OleDbConnection
  4.         Return New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
  5.                                          & Application.StartupPath & "\member.accdb;")
  6.  
  7.     End Function
  8.     'pass the value of a connection to con
  9.     Public con As OleDb.OleDbConnection = mydb()
  10. End Module
2. After that, create a procedure of AutoAppend and Autosuggest.
  1.     'procedure of your autoappend and autosugest
  2.     Public Sub autocompletetxt(ByVal sql As String, ByVal txt As TextBox)
  3.         Try
  4.  
  5.             Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, con)
  6.  
  7.             Dim dt As New DataTable
  8.             da.Fill(dt)
  9.  
  10.             Dim r As DataRow
  11.             txt.AutoCompleteCustomSource.Clear()
  12.  
  13.             For Each r In dt.Rows
  14.                 txt.AutoCompleteCustomSource.Add(r.Item(0).ToString)
  15.  
  16.             Next
  17.         Catch ex As Exception
  18.             MsgBox(ex.Message)
  19.         End Try
  20.  
  21.     End Sub
3.Go back to the Design View and create just like this. Search Form 4. Click the TextBox and go to the Properties. Under the Properties click AutoComplete Mode and select Suggest and Append. After that, click AutoComplete Source and select Costume Source. Properties 5. Double click the Form and call the name of the Public Sub of an AutoAppend and AutoSuggest event.
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.          'declaring sql as string
  5.         Dim sql As String
  6.         'set your query on the sql string
  7.         sql = "SELECT name FROM tblnames"
  8.         'call the autocomplete event located in the module.
  9.         autocompletetxt(sql, TextBox1)
  10.     End Sub
  11. End Class
Now, Press F5 to run your project. Download the complete source code and run it on your computer.

Add new comment