Sunday 21 January 2018

Populating Data To Listbox From Database in VB 6

Populating-Data-To-Listbox-From-Database-in-VB6
List Box is a control in visual basic 6 which is used to display various items available where a user can select one or more items depending on the Multiselect property set from the properties panel. The items in a list box can be added at design time from the list property from the properties panel or Populate Data To Listbox From Database at run time. We can also use its style property to set it to standard or checkbox where we can have check box against each item. The List Box also supports various events important of which are discussed here.

Important properties of a List box

  1. Multiselect: This property is used if we want to select more than one item from the list. The other options available under this property are: None, Simple and Extended. 
  2. Sorted: This property is used if we want the list items to be in a sorted order or not. 
  3. Style: The options available under this property are Standard and Checkbox which displays a check box against each list.

Important events of a List box

  1. Click: This is the default event of a List box which is executed when we click on any item. 
  2. Double Click: This event is executed when we double click on any item. 
  3. Key press: This event is executed when any key is pressed on the list box. 
  4. Scroll: This event gets executed when we scroll in the listbox.
How to Populate Listbox from a database table
 ‘Here is the Module  
 Option Explicit  
 Public Con As New ADODB.Connection  
 Public rs As New ADODB.Recordset  
 Public rs2 As New ADODB.Recordset  
 Public sql As String  
 Public query As String  
 Public Sub Connection()  
 Set Con = New ADODB.Connection  
   Con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test.mdb;Persist Security Info=False"  
 End Sub  
 ' add the following code to the load event of form1  
 ' list1 is the listbox on form1......  
  'call the connection  
 Connection  
 sql = "SELECT (investigation ) As Investigations FROM investigation order by investigation asc "  
 Set rs = New ADODB.Recordset  
 rs.Open sql, Con, adOpenDynamic, adLockOptimistic  
 Do While Not rs.EOF  
   List1.AddItem UCase(rs!Investigations)  
       rs.MoveNext  
   Loop  
 rs.Close: Set rs = Nothing  

VB6

0 comments:

Post a Comment

 

© 2018 Mastering Web Development: HTML, Bootstrap, PHP, ASP.NET & VB.NET Essentials - Designed by Mukund | Privacy Policy | Sitemap

About Me | Contact Me | Write For Us