Friday, March 19, 2010

Displaying the data in Visual Basic 6.0 Datagrid Controls

Using the self made module that you have incorporated in your Visual Basic  6.0 project, you can use this in displaying the data you have in your MS Access or SQL Server.

Objects needed:

Add Datagrid control in your form. By default, Datagrid is not included in the list of toolbox at the right side of Visual Basic 6.0. So you need to add this by clicking Project in the Menu Bar, then Components, or just hit Ctrl+t in your keyboard. Select Microsoft Datagrid Controls 6.0 (OLEDB), remember to check the checkbox opposite the component. Click apply, then OK.

Suppose you have a table named Account in your database, with fields: Username, Password

Drag the Datagrid in the form. Double click in the form or form_load and write the following code:

Dim rs as recordset
Set rs = gsql(“Select * from Account”)
If rs.recordcount <> 0 then
Set datagrid1.datasource = rs
End if

Finished, you can now display the contents of your database in the datagrid. You can put this code anywhere you want, in the button or in the textbox. This code is not fix, you can always change the sql query depending on what you need.

If this code did not function well, kindly check your configuration of the database module.


No comments:

Post a Comment