MSFlexGrid problem

HI friends,

    I am developing an application for a school. Here one module is Entering Attendance for the students and Marks for different tests conducted.

Marks Entry: I will select Classcode,Medium and Student Number. The subject for that class will be displayed and the user can enter marks. That all in a grid.
I selected MDFlexGrid. Given query and retrieved subjects and displayed in the grid. But how can i enter marks corr. to the subjects. In the grid any column is not accepting user input.

Is this the right choice to take grid or any other control to be used. Pls, give me the solution.

Who is Participating?

The MSFlexGrid does not inherently support editing. Their is an article on MSDN that explains how to add this functionality.

MSFlexGrid does not editing. you can type text into the MSFlexGrid but the cursor will not be there.
if you want to try pls type the following code in the keypress property of the MSFlexGrid control.

if keyascii= 8 then
   if len(obj.text)>0 then
      obj.text = mid(obj.text,1,len (obj.text)-1)
   end if
   obj.text = obj.text & chr(keyasci)
end if

U cannot use MSFlexGrid to edit or change the value in its cells. Whem u type in the new values, it would not be saved to the database.

Below is my suggestion :

To make this technique work, add a TextBox control to the form and then set its Visible property to False, its MultiLine property to True, and its BorderStyle property to 0-None. These routines cause the phantom TextBox (named txtCellEditor) to appear and disappear as needed:

' These variables keep track of the cell that was active
' when edit mode was entered.
Dim cellRow As Long, cellCol As Long

Sub ShowCellEditor()
    With MSHFlexGrid1
        ' Cancel range selection, if any.
        .RowSel = .Row
        .ColSel = .Col
        ' Move the cell editor into place by making it one pixel smaller
        ' than the current cell.
        txtCellEditor.Move .Left + .CellLeft, .Top + .CellTop, _
            .CellWidth - ScaleX(1, vbPixels, vbTwips), _
            .CellHeight - ScaleY(1, vbPixels, vbTwips)
        ' Transfer the contents of the current cell into the TextBox.
        txtCellEditor.Text = .Text
        ' Move the TextBox in front of the grid.
        txtCellEditor.Visible = True
        ' Remember current coordinates for later.
        cellRow = .Row
        cellCol = .Col
    End With
End Sub

Sub HideCellEditor(Optional Cancel As Boolean)
    ' Hide the TextBox control if necessary.
    If txtCellEditor.Visible Then
        ' If the operation hasn't been canceled, transfer the contents
        ' of the TextBox into the cell that was active.
        If Not Cancel Then
            MSHFlexGrid1.TextMatrix(cellRow, cellCol) = txtCellEditor.Text
        End If
        txtCellEditor.Visible = False
    End If
End Sub


The ShowCellEditor routine can move the TextBox into place, thanks to the grid's CellLeft, CellTop, CellWidth, and CellHeight properties. The next step is to determine when cell editing is activated. In the demonstration program, this happens when the grid is double-clicked or when the user presses an alphanumeric key when the grid has the input focus:

Private Sub MSHFlexGrid1_DblClick()
End Sub

Private Sub MSHFlexGrid1_KeyPress(KeyAscii As Integer)
    ' If it's an alphanumeric key, it is passed to the TextBox.
    If KeyAscii >= 32 Then
        txtCellEditor.Text = Chr$(KeyAscii)
        txtCellEditor.SelStart = 1
    End If
End Sub


Edit mode is terminated when the TextBox loses the focus (for example, when the user clicks elsewhere in the grid), or when either the Enter or Esc key is pressed:

Private Sub txtCellEditor_LostFocus()
End Sub

Private Sub txtCellEditor_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 13
        Case 27
            HideCellEditor True     ' Also cancel the edit.
    End Select
End Sub


Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

It's similar to the codes provided at that link suggested by Z but I definitely am not copying his answer or anything, just coincidence. It uses textboxes superimposed over the cells to make it look as it belongs to the flexgrid.

Sorry in advance to u guys if u think that I'm trying to "jump the queue" here.

Chandramouli kArchitectCommented:
Flexgrid doesn't accept userinput. But u can overcome this using TextMatrix Property. To enter value in the FlexGrid
MSFlexGrid1.TextMatrix(1,1) = "Hello"

The TextMatrix Property is read/write property. All u have to do this pass the x,y co-ords to assign some text.

Chandramouli kArchitectCommented:
u said:"In the grid any column is not accepting user input"

If this is ur problem, then following is the answer.

Try this code
Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
With MSFlexGrid1
    .TextMatrix(.Row, .Col) = .TextMatrix(.Row, .Col) & Chr(KeyCode)
End With
End Sub

U can also perform Input validation using Keycode Value like Keycode should be between 48 and 57 for Numeric Inputs and so on.

This question has been locked with a Proposed Answer, and remains open today.  Questions in the LOCKED versus OPEN queue draw little to zero additional attention by Experts.

If the Proposed Answer did not serve your needs, please reject it and comment with an update.  If the Proposed Answer helped you, please accept it to grade and close this question.  If you need help splitting points between multiple experts, please comment here with details so we can help you.

EXPERTS ->  Please guide me here in terms of closing recommendations if the Asker does not respond in 4 days.

Thanks to all,
Moondancer - EE Moderator

As Neo78 stated, the answer I gave is a full answer to the question, and his answer is a variation of mine. The answer kcm76 locked the question with is the same that azhaguraj gave earlier. As a result, if the questioner does not come back then I woulkd recommed giving the points to me, or splitting them between Neo78 and I.

Dear kcm76
I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.

See: <>
Paragraph: Site Update for Wednesday, November 06, 2002

By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.

EXPERTS: I will return in seven days to close this question.
Please leave your thoughts and recommendations here


Thanks !

Experts Exchange Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.