Advance Autocomplete/Autosuggest in a Textbox Using Visual Basic 2008 and MySQL database

When searching a data from the database using textbox in Visual Basic 2008, commonly, it’s hard for us to search the exact data. So, in this tutorial I created an autosuggest textbox, so that it will be easy for you to search the correct record in your table. For instance, the word “google”. Let’s begin: 1.First Create a database in MySQL named “employeesdb” then create the table.
  1.  
  2. CREATE TABLE IF NOT EXISTS `employees` (
  3. `EMPLOYEE_ID` int(11) NOT NULL,
  4. `FIRST_NAME` varchar(255) DEFAULT NULL,
  5. `LAST_NAME` varchar(255) DEFAULT NULL,
  6. `EMAIL` varchar(255) DEFAULT NULL,
  7. `PHONE_NUMBER` varchar(255) DEFAULT NULL,
  8. `HIRE_DATE` datetime DEFAULT NULL,
  9. `JOB_ID` varchar(255) DEFAULT NULL,
  10. `SALARY` int(11) DEFAULT NULL,
  11. `COMMISSION_ID` int(11) DEFAULT NULL,
  12. `MANAGER_ID` int(11) DEFAULT NULL,
  13. `DEPARTMENT_ID` int(11) DEFAULT NULL,
  14. PRIMARY KEY (`EMPLOYEE_ID`),
  15. KEY `COMMISSION_ID` (`COMMISSION_ID`),
  16. KEY `DEPARTMENT_ID` (`DEPARTMENT_ID`),
  17. KEY `JOB_ID` (`JOB_ID`),
  18. KEY `MANAGER_ID` (`MANAGER_ID`)
  19. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
2. Insert all the data in the employees table.
  1.  
  2. INSERT INTO `employees` (`EMPLOYEE_ID`, `FIRST_NAME`, `LAST_NAME`, `EMAIL`, `PHONE_NUMBER`, `HIRE_DATE`, `JOB_ID`, `SALARY`, `COMMISSION_ID`, `MANAGER_ID`, `DEPARTMENT_ID`) VALUES
  3. (101, 'neena', 'Kochar', 'nKochar', '123-1231-212', '1989-09-21 00:00:00', 'AD_VP', 17000, NULL, 100, 90),
  4. (102, 'lex', 'De Haan', 'lDe Haan', '113-2122-2121', '1990-01-13 00:00:00', 'AD_VP', 17000, NULL, 100, 90),
  5. (103, 'alexander', 'Hunold', 'aHunold', '211-4515-1515', '1991-05-21 00:00:00', 'IT_PROG', 9000, NULL, 102, 60),
  6. (104, 'Bruce', 'Ernst', 'bErnst', '515-541-151', '1999-07-02 00:00:00', 'IT_PROG', 6000, NULL, 103, 60),
  7. (107, 'Diana', 'Lorents', 'dLorents', '515-515-5156', '1999-11-16 00:00:00', 'IT_PROG', 4200, NULL, 103, 60),
  8. (124, 'Kevin', 'Mourgos', 'kMourgos', '559-887-333', '1995-10-17 00:00:00', 'IT_PROG', 5800, NULL, 100, 50),
  9. (141, 'Trenne', 'Rajs', 'tRajs', '212-548-989', '1997-01-29 00:00:00', 'ST_MAN', 3500, NULL, 124, 50),
  10. (142, 'Curtis', 'Davies', 'cDavies', '333-999-777', '1998-03-15 00:00:00', 'ST_CLERK', 3100, NULL, 124, 50),
  11. (143, 'Randal', 'Matos', 'rMatos', '4444-5654-54', '1998-09-07 00:00:00', 'ST_CLERK', 2600, NULL, 124, 50),
  12. (144, 'Peter', 'Vargas', 'pVargas', '6565-5641-87', '2000-01-10 00:00:00', 'ST_CLERK', 2500, NULL, 124, 50),
  13. (149, 'Ellen', 'Zlotkey', 'eZlotkey', '656-4847-45', '1996-11-05 00:00:00', 'ST_CLERK', 10500, NULL, 100, 50),
  14. (174, 'Jonathan', 'Abel', 'jAbel', '5988-4556-564', '1999-05-24 00:00:00', 'SA_MAN', 11000, 0, 100, 80),
  15. (176, 'Kimberly', 'Taylor', 'kTaylor', '687-695-8754', '1987-09-17 00:00:00', 'SA_REP', 8600, 0, 149, 80),
  16. (178, 'Jinnefer', 'Grant', 'jGrant', '552-6541-897', '1996-02-17 00:00:00', 'SA_REP', 7000, 0, 149, 80),
  17. (200, 'Michael', 'Whalen', 'mWhalen', '2121-5465-541', '1997-08-17 00:00:00', 'SA_REP', 4400, 0, 149, NULL),
  18. (201, 'Pat', 'Hartstein', 'pHartstein', '14564-541-45', '1994-07-07 00:00:00', 'AD_ASST', 13000, NULL, 101, 10),
  19. (205, 'Shelley', 'Fay', 'sFay', '515-215-1156', '1994-07-07 00:00:00', 'MK_MAN', 6000, NULL, 100, 20),
  20. (206, 'William', 'Higgins', 'wHiggins', '566-112-5156', '1995-09-26 00:00:00', 'AC_MGR', 12000, NULL, 201, 20),
  21. (207, 'hatch', 'Glets', 'hGlets', '556-5465-515', '1989-03-07 00:00:00', 'AC_ACCOUNT', 8300, NULL, 101, 110),
  22. (100, 'Steven', 'King', '[email protected]', NULL, NULL, NULL, 24000, NULL, 100, NULL);
3. Now, Open the Microsoft Visual Basic 2008 and create a form. Put a TextBox and a Label. Search Form 4. Click the TextBox and go to the properties. Under it, click the AutoComplete mode and select Suggest. After selecting it, click AutoComplete Source and select Custom Source. Properties 5. Double click a Form and set up your connection. And do the declaration.
  1. Imports MySql.Data.MySqlClient
  2. Public Class Form1
  3.  
  4. Public Function mysqldb() As MySqlConnection
  5. Return New MySqlConnection("server=localhost;user id=root;database=employeesdb")
  6. End Function
  7. Public con As MySqlConnection = mysqldb()
  8.  
  9. Public cmd As New MySqlCommand
  10. Public da As New MySqlDataAdapter
  11. Public dt As New DataTable
  12.  
  13. end class
Reminders: Before setting up your connection make sure that you add MySql.Data.dll as your reference. 6. After the declaration, create a public sub of the AutoSuggest event.
  1. Public Sub auto_suggest(ByVal member As String, ByVal table As String, ByVal txt As Object)
  2. Try
  3. dt = New DataTable
  4. 'open the connection
  5. con.Open()
  6. 'holds the data in the database
  7. With cmd
  8. .Connection = con
  9. .CommandText = "select " & member & " from " & table
  10. End With
  11. '''''''''''''''fill data in the table
  12. da.SelectCommand = cmd
  13. da.Fill(dt)
  14.  
  15. ''function of autocomplete
  16. Dim r As DataRow
  17. txt.AutoCompleteCustomSource.Clear()
  18.  
  19. For Each r In dt.Rows
  20. txt.AutoCompleteCustomSource.Add(r.Item(0).ToString)
  21.  
  22. Next
  23. ''''''''''''''''''''''''
  24. Catch ex As Exception
  25. MsgBox(ex.Message)
  26. End Try
  27. ''''close the connection
  28. con.Close()
  29. da.Dispose()
  30.  
  31. End Sub
7. After that, go back to the design view and double click the Form. Call the name of your public sub and put it in a Form1_Load.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.  
  3. 'call a public sub
  4. 'the job_id is the field of a table , the employees is the name of a table and a textbox is an object
  5. auto_suggest("JOB_ID", "employees", TextBox1)
  6.  
  7. End Sub
8. Now run your work. Complete Source Code is included. Download it and run on your computer.

Comments

Submitted byDavid MAdriz (not verified)on Fri, 12/23/2016 - 05:52

Quiero felicitarte amigo te felicito personas como tu ayudan a que nosotros podamos seguir a delante feliz navidad compañero

Add new comment