VB 6.0 - VB6, How to Fill MsFlexGrid with data for display

Asked By Lennie Kuah on 17-Apr-10 11:21 PM
Hi VB6 Friends,

I need your help. please help me.

Due to lack for using VB6 for a few years I am having problem trying to meet my IT Manager request to enhance existing VB6 application by using MsFlexGrid to display data.

I have trying to load MsFlexgrid using ADORecord set and display msflexgrid. Just don't know how to do it anymore. Please help me with sample VB6 coding for me to learn the coding and logic of it.

Thank you very much.
Anoop S replied to Lennie Kuah on 17-Apr-10 11:49 PM
You can use this code to fill flexgrid from record set

Dim i as Integer
i=1
With MSFlexGrid1
.Clear
.Rows = 2
// ALL for heading if you need
.TextMatrix(0, 0) = "Student Id"
.TextMatrix(0, 1) = "FIRST NAME"
.TextMatrix(0, 2) = "LAST NAME"
.TextMatrix(0, 3) = "Address"
.TextMatrix(0, 4) = "Phone NO"
If rs.EOF <> True Then
     Do
       If sxx <> rs(0) Then
       
        .TextMatrix(i, 0) = rs(0)
        .TextMatrix(i, 1) = rs(1)
        .TextMatrix(i, 2) = rs(2)
        .TextMatrix(i, 3) = rs(3)
        .TextMatrix(i, 4) = rs(4)
    
        i = i + 1
        .Rows = .Rows + 1
        End If
        rs.MoveNext
       Loop Until rs.EOF = True
 End If
Lennie Kuah replied to Anoop S on 18-Apr-10 12:54 AM
Hi Anoop5,

Thank you for sharing the Vb coding with me.

On the ToolBox, I couldn't find the Msflexgrid control to drag it to the FORM.
Can you please let me know the DLL or Component that I must add as Reference/


You are just awesome being so helpful and generous sharing information  and helping me.  Yeee....haaaaa.....

Anoop S replied to Lennie Kuah on 18-Apr-10 01:02 AM
right click the toolbox and then new window will comes and  there will option for add the component, check the MSflexgrid and add that
Lennie Kuah replied to Anoop S on 18-Apr-10 03:51 AM
Hi Anoop,
Thanks for the suggestion,. I managed to drag the MsFlexGrid control onto the form and also add a Command Button to it.
I copied your sample coding and tested it using SQL SERVER table STUDENT.

To my surprise it generate this error message:
SubScript out of range
    
       DIM I AS INTEGER
       I = 1

         With MSFlexGrid1
                   .Clear
                    .Rows = 2


       // ALL for heading if you need
      .TextMatrix(0, 0) = "Student Id"
     .TextMatrix(0, 1) = "FIRST NAME"
     .TextMatrix(0, 2) = "LAST NAME"    <------ generate error  SubScript out of range

     .TextMatrix(0, 3) = "Address"
     .TextMatrix(0, 4) = "Phone NO"


If rs.EOF <> True Then
     Do
            If sxx <> rs(0) Then       
                 .TextMatrix(i, 0) = rs(0)
                 .TextMatrix(i, 1) = rs(1)
                 .TextMatrix(i, 2) = rs(2)
                 .TextMatrix(i, 3) = rs(3)
                 .TextMatrix(i, 4) = rs(4)    
                  i = i + 1
                 .Rows = .Rows + 1
          End If


           rs.MoveNext
       Loop Until rs.EOF = True
 End If

         

    END WITH

Anoop S replied to Lennie Kuah on 18-Apr-10 05:20 AM
Hi

.TextMatrix(0, 0) = "Student Id"
     .TextMatrix(0, 1) = "FIRST NAME"
     .TextMatrix(0, 2) = "LAST NAME"    <------ generate error  SubScript out of range

     .TextMatrix(0, 3) = "Address"
     .TextMatrix(0, 4) = "Phone NO"

the error comes because your grid has only 2 column, thats why, Right click on the MSFlexgrid and add 5 column
Lennie Kuah replied to Anoop S on 18-Apr-10 08:19 PM
Hi Anoop,

Thank you very much for pointing out my error. That is just awesome.  I have modified the sample coding to suit my IT System Analyst specifications and User Requirements.

Yaaa...hooooo.......the coding is working now and I would like to post the working VB6 coding here here to share it with other Newbies who may experience similar problems.

HERE ARE THE CODING:

 With Me.MSFlexGrid1
            .Clear
            .Rows = 2
            .Cols = adoRst.Fields.Count
                      
           
            Dim i As Integer         

          ' format FlexGrid Header and column width
           For i = 0 To (adoRst.Fields.Count - 1)
                .TextMatrix(0, i) = adoRst.Fields(i).Name
                .ColWidth(i) = 1400
            Next       

            'fill with MsFlexGrid cells with data using ADO Recordset
             Do While Not adoRst.EOF
                            
                 .TextMatrix(IRow, 0) = adoRst.Fields(0).Value
                 .TextMatrix(IRow, 1) = adoRst.Fields(1).Value               

                  If Len(adoRst.Fields(2).Value) > 0 Then .TextMatrix(IRow, 2) = adoRst.Fields(2).Value
                  If Len(adoRst.Fields(3).Value) > 0 Then .TextMatrix(IRow, 3) = adoRst.Fields(3).Value
                  If Len(adoRst.Fields(4).Value) > 0 Then .TextMatrix(IRow, 4) = adoRst.Fields(4).Value
                                    
                  .Rows = .Rows + 1
                  IRow = IRow + 1
                  adoRst.MoveNext
             Loop           
       
    End With

Thank you very much Anoop. Appreciate your help very much. So glad that I met you at this awesome FORUm

victor replied to Lennie Kuah on 05-Dec-10 11:13 PM
Hi this is a code when displaying data in msflexgrid. first you have to a make a Function name RecDisplay and load it in a form. See below...
function RecDisplay(sql as variant)

set rs=new adodb.recordset
rs.open sql,conn,adopnStatic,AdlockReadOnly

with msflexgrid1
.rows=1

 while not rs.eof
  .additem rs!EmployeeID & vbtab & rs!FirstName & vbtab & rs!lastName & vbtab & rs!age & vbtab & rs!address & vbtab & rs!PhoneNumber
 rs.movenext
wend

end with
end function

private sub form_load()
RecDisplay " your sql code. ..  "

end sub


-- you can email me for some help at victortagupa@yahoo.com