MSFlexGrid and Access

sillyman
10-26-2004, 03:10 PM
OK I dont really know how to add data to MSFlexGrid could someone tell me how and guide me on how to display only certain data according to my search which is stored in variable VarCustomer. Below is my code:

Dim sQuery As String
sQuery = "Select * FROM [Customer]"
rs.Open sQuery, cnn, adOpenForwardOnly, adLockReadOnly

With rs
If Not (rs.EOF And rs.BOF) Then
If .Fields("ID").Value = VarCustomer Then
MSFlexGrid1.FormatString = "ID"
MSFlexGrid1.DataSource = rs
Else
MsgBox "incorrect"
End If
End If

Harkon
10-26-2004, 03:37 PM
you should change your sQuery to:

'if id holds non numeric values
sQuery = "SELECT * FROM Customer WHERE ID = '" & VarCustomer & "'"
'if it holds numeric
sQuery = "SELECT * FROM Customer WHERE ID = " & VarCustomer

then just set the datasource to rs as u do.
However this is not the best way to populate an msflexgrid.
You should write a function to fill the grid and then recall it if data is to change.
But, if it works it's cool.
try and let us know

sillyman
10-26-2004, 03:53 PM
Nah I get a type mismatch error and it highlights this MSFlexGrid1.DataSource = rs

code I am using

Dim sQuery As String
sQuery = "Select * FROM [Customer] where ID = " & VarCustomer
rs.Open sQuery, cnn, adOpenForwardOnly, adLockReadOnly

MSFlexGrid1.FormatString = "ID"

With rs
If Not (rs.EOF And rs.BOF) Then
MSFlexGrid1.DataSource = rs
End If
End With

I have even tried it without the with statement

:confused:

sillyman
10-26-2004, 04:33 PM
I ahve sort of like got the code working

With rs
If Not (rs.EOF And rs.BOF) Then
MSFlexGrid1.AddItem VarCustomer
MSFlexGrid1.AddItem !Name
End If
End With

rajeeshun
10-27-2004, 01:47 AM
You can find very useful codings/Fuctions for MSFlexGrid in Knowldge base section of this forum. It is written by FlyGuy with many examples.

sillyman
10-27-2004, 04:38 PM
FlyGuy has complicated code can someone easily explain how I can populate a MSFlexGrid with Access data?

You can find very useful codings/Fuctions for MSFlexGrid in Knowldge base section of this forum. It is written by FlyGuy with many examples.

Dennis DVR
10-27-2004, 08:19 PM
are you using MSFlexgrid (Classic)? or MSHFlexgrid (Hierarchical Flexgrid)?

Obviously you are using ADO recordset so binding it to a Classic MSFlexgrid would give you a type mismatch error, If you want to use the Classic MSFlexgrid you can either use the DAO recordset and bind it to your MSFlexgrid using the DataSource property as already been provided, or populate the MSFlexgrid using the Do loop and populate each row and column using the TextMatrix property. i.e.

assumed that the rs is already opened (Untested code)

Dim i As Long, j As Long
'if the rs.RecordCount returns -1 you will need to change your cursorlocation to adUseClient and change your lockType property dynamic (I think?)
msflexgrid1.visible = false 'to make it more faster
msflexgrid1.Cols = rs.Field.Count
msflexgrid1.Rows = rs.RecordCount + 1 'set the maximum rows of flexgrid immediately to make it more faster
Do While Not rs.EOF
i = i + 1 'ignore fixed row
For j = 1 To 10 'ignore fixed column
msflexgrid1.TextMatrix(i, j) = rs.Fields(j)
Next
'you can also increment the row by one, but this make the loading a bit slower
'msflexgrid1.Rows = msflexgrid1.Rows + 1
rs.MoveNext
Loop
msflexgrid.visible = true


Alternatively, you can use keep your code, but you have to change your Classic MSFlexgrid control to MSHFlexgrid control (Hierarchical) MSHFlexgrid control uses the ADO technology.

btw: you can also populate the MSHFlexgrid (Hierarchical) you using the loop as I have provided.

noccy
10-28-2004, 03:39 AM
Hi!

I would also recomend using the MSHFlexgrid. Then you can populate the grid directly from the recordset.

noccy

sillyman
10-28-2004, 07:28 AM
Cheers I'll check it out!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum