Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6

Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6

Previously I have made an article on Create Microsoft Access Database Connection Visual Basic 6. Here we will use it to connect the database with Microsoft Visual Basic 6. But we have not made a table in the database in the previous article. View the Table of Contents to Database into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6 on the three components of the data container has a different way. Thus we have to discuss it one by one. But before we create tables and input to database tables microsoft acces

Steps Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6 is:

  1. Create tables

Here I create the table Siswa with fields as shown below:

Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6_1

  1. Fill the table

After creating the table, we fill the table as shown below:

Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6_2

  1. To Display Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6

Next we will displaying some of the fields that is only NIS, nama_siswa, and tempat_lahir.

DataGrid

To display the contents of the table into the DataGrid we must adding the DataGrid component toolbox by pressing CTRL + T then the controls tab check the Microsoft DataGrid Control 6.0 (SP6) (OLEDB)

Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6_3

So that the DataGrid component appears on the toolbox, then adding the DataGrid component into form. After that we will make a sub or function to set the DataGrid header and displaying the contents of database tables into the DataGrid.

Sub to set the DataGrid header source code as follows:

Sub HeaderDatagrid()
With DataGrid1
.AllowUpdate = False
.AllowDelete = False
.AllowAddNew = False
.Columns(0).Caption = "NIS"
.Columns(0).Width = 1000
.Columns(0).Alignment = dbgCenter
.Columns(1).Caption = "NAMA"
.Columns(1).Width = 1000
.Columns(1).Alignment = dbgLeft
.Columns(2).Caption = "TEMPAT LAHIR"
.Columns(2).Width = 1600
.Columns(0).Alignment = dbgCenter
End With
End Sub

Information: To set the DataGrid header in different ways with the MSFlexGrid and listview. For Sub DataGrid to set the header is NOT called in the form load but after this source code :

Set DataGrid1.DataSource = RS
Call HeaderDatagrid   'untuk mengatur header datagrid

Then the sub to display the contents of the table to the DataGrid source code as follows:

Sub ViewDatagrid()
Dim SQL As String
CONN.CursorLocation = adUseClient
SQL = "Select nis,nama_siswa,tempat_lahir from siswa"
Set RSSiswa = CONN.Execute(SQL)
With RSSiswa
    If Not (.BOF And .EOF) Then
      mvBookMark = .Bookmark
    End If
End With
Set DataGrid1.DataSource = RSSiswa.DataSource
Call HeaderDatagrid
End Sub

After the sub dial to display the contents of the table to a DataGrid on the form load. To display the contents of the table to the DataGrid completed.

MSFlexGrid

Now we will continue to display the contents of database tables into the data container MSFlexGrid. To display the MSFlexGrid components do almost the same with the DataGrid. Press CTRL + T then check Microsoft Flrxgrid Control 6.0 (SP6).

Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6_3

Adding MSFlexGrid component into form and set the height and width of the MSFlexGrid. After that we create a Sub or Function to adjust the header and to display the contents of the table into the MSFlexGrid. Sub to set MSFlexGrid source code as follows:

Sub HeaderMsflexgrid()
With MSFlexGrid1
    .Cols = 3
    '.Rows = 5
    .TextMatrix(0, 0) = "NIS"
    .ColWidth(0) = 1000
    .ColAlignment(0) = flexAlignCenterCenter
    .TextMatrix(0, 1) = "NAMA"
    .ColWidth(1) = 1000
    .ColAlignment(1) = flexAlignLeftCenter
    .TextMatrix(0, 2) = "TEMPAT LAHIR"
    .ColWidth(2) = 1500
    .ColAlignment(2) = flexAlignCenterCenter
End With
End Sub

After that call Sub to set the header MSFlexGrid in the form load then, now MSFlexGrid header is created. Then we proceed to display the contents of the table into the MSFlexGrid. We make Sub source code as follows:

Sub ViewMsflexgrid()
Dim row As Long
Dim i As Long
‘row = 2
SQL = "select nis,nama_siswa,tempat_lahir from siswa"
Set RS = CONN.Execute(SQL)
With RS
Do While Not RS.EOF
MSFlexGrid1.TextMatrix(row - 1, 0) = .Fields(0)
MSFlexGrid1.TextMatrix(row - 1, 1) = .Fields(1)
MSFlexGrid1.TextMatrix(row - 1, 2) = .Fields(2)
row = row + 1
MSFlexGrid1.Rows = row
.MoveNext
Loop
End With
End Sub

After completed then we call Sub to display the contents of the table into the form load.

ListView

The last step to display the contents of database tables into listview. View listview component to press CTRL + T check Microsoft Windows Common Controls 6.0 (SP6)

Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6_3

Adding listview component into form set the height and width. Then we create a Sub to set the header and for displaying contents of database tables into the data container listview. Sub to set the header listview source code as follows:

Sub HeaderListview()
With ListView1
    'mengatur tampilan listview
    .View = lvwReport
    .FullRowSelect = True
    .GridLines = True
    .HotTracking = True
    .HoverSelection = True
    'mengatur header
    .ColumnHeaders.Add 1, , "NIS", 1000
    .ColumnHeaders.Add 2, , "NAMA", 1500
    .ColumnHeaders.Add 3, , "TEMPAT LAHIR", 1600, 2
End With
End Sub

After finished creating sub header listview, then we create a Sub to display the contents of the table into listview Sub to displaying contents of database tables to the listview source code as follows:

Sub ViewListview()
Dim list As ListItem
SQL = "Select nis,nama_siswa,tempat_lahir from siswa"
Set RS = CONN.Execute(SQL)
With RS
ListView1.ListItems.Clear
Do While Not RS.EOF
Set list = ListView1.ListItems.Add
list.Text = !nis
list.SubItems(1) = !nama_siswa
list.SubItems(2) = !tempat_lahir
.MoveNext
Loop
End With

End Sub 

Then call the Sub above into the form load. These last step is completed for Displaying Content Database Tables into the DataGrid, ListView, MSFlexGrid in Microsoft Visual Basic 6.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s