Rabu, 04 Mei 2016

MSFlexgrid Data Retrieval With ADO

Open the database and execute the query. Loop through the recordset's Fields array to get the field names and use them as column headers.
For each record in the Recordset, loop through the fields saving their values in the FlexGrid.

Source Code :

1. Form load

Option Explicit
Dim i as integer
dim sSql as string
dim rs as New Adodb.Recordset

Private sub Initview()
       sSql = " SELECT" & _
                " Code, Firstname, Lastname, Address, City" & _
              " FROM tbl_contact" & _
              " WHERE Inactive = '" & Trim("Y") & "'"
       GetRecordset Strcon, sSql,rs
       With vsflist
            If rs.RecordCount > 0 Then
               .Rows = rs.RecordCount + 1
               For i = 1 To .Rows - 1
                        .TextMatrix(i, .ColIndex("No")) = i
                        .TextMatrix(i, .ColIndex("code")) = IIf(IsNull(rs(0)) = True, "", rs(0))
                        .TextMatrix(i, .ColIndex("firstname")) = IIf(IsNull(rs(1)) = True, "", rs(1))
                        .TextMatrix(i, .ColIndex("lastname")) = IIf(IsNull(rs(2)) = True, "", rs(2))
                        .TextMatrix(i, .ColIndex("address")) = IIf(IsNull(rs(4)) = True, "", rs(4))

                        .TextMatrix(i, .ColIndex("city")) = IIf(IsNull(rs(5)) = True, "", rs(5))
                        rs.MoveNext
                    Next i
                Else
                    .Rows = 1
                End If

      End With
End Sub

2. Module

Public Function GetRecordset(ByVal strcn As String, _
                    ByVal strqry As String, _
                    rs As ADODB.Recordset) _
                    As Boolean
GetRecordset = False

   Set rs = New ADODB.Recordset
  
   rs.Open strqry, strcn, adOpenKeyset
   GetRecordset = rs.RecordCount
   GetRecordset = True
  
   If GetRecordset = False Then
      Set rs = Nothing
      rs.Close
   End If
End Function


EmoticonEmoticon