
How to Populate MshFlexgrid in vb6
Create a module on your vb6 project and name that module PopulateGridModule. Then paste this code. Be sure that you have already established a database connection on you MySql or Access database. or else this method will not run.
Here is my connection type: Check it out for your reference.
Public Function connectionString()
connectionString = "DRIVER={" & ReadIniValue(App.Path & "\conme.ini", "DEFAULT", "DRIVER") & "};" _
& "SERVER=" & ReadIniValue(App.Path & "\conme.ini", "DEFAULT", "HOST") & ";" _
& "DATABASE=" & ReadIniValue(App.Path & "\conme.ini", "DEFAULT", "DBNAME") & ";" _
& "UID=" & ReadIniValue(App.Path & "\conme.ini", "DEFAULT", "UID") & ";" _
& "PWD=" & ReadIniValue(App.Path & "\conme.ini", "DEFAULT", "PWD") & ";" _
& "OPTION=3"
End Function
A Simple reminder!, here in this connection string I used the conme.ini method.
Now this code will populate the MSHFlexGrid on your project.
Public Sub populateFlexGrids(dboQuery As String, grid As MSHFlexGrid)
Call init
grid.Clear
With rs
On Error GoTo errHandler
.Open dboQuery, conn, adOpenKeyset, adLockOptimistic
If rs.RecordCount = 0 Then
MsgBox "No Data Found!", vbCritical, "POS Message:"
Set grid.DataSource = rs
Else
Set grid.DataSource = rs
End If
.Close
End With
conn.Close
Dim columnWidth As Double
Dim counter As Integer
columnWidth = (grid.Width / grid.Cols)
If columnWidth <= 2000 Then
columnWidth = 2000
End If
For counter = 0 To grid.Cols
grid.ColWidth(counter) = columnWidth
Next counter
grid.Refresh
Exit Sub
errHandler:
MsgBox Err.Description, vbCritical + vbOKOnly, "Admin: Populate Element Module - Populate Flexgrid: Error"
End Sub
Then call this function:
Call PopulateGridModule.populateFlexgrids("Select * from YOUTABLE", MSHFlexgrid1)
Now you can put this code OnLoad or OnClick.
Good Luck!