Microsoft KB Archive/149094

= How To Determine the Contents of a Cell in a DBGrid Control =

Article ID: 149094

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q149094



SUMMARY
This article shows how to determine the contents of a selected cell in a DBGrid control.



MORE INFORMATION
The DBGrid control allows you to display the contents of a recordset or a table. With this method, you can take the data from a cell in the MouseUp event of the DBGrid control.

This example uses a DBGrid control bound to Data control. The Data control uses the BIBLIO.MDB database for information. In the MouseUp event of the DBGrid control, the column and row positions of the cell are assigned to an integer variable. A bookmark is then set using the row position. Executing the CellValue method on the Columns collection of the DBGrid control determines the contents of the cell. Those contents then appear on a TextBox control.

Sample Code
 Start Visual Basic. If it is already running, on the File menu, click New Project. Add a DBGrid, TextBox, and a Data control to the Form1 form. Set the following controls to the specified properties:

 Data control DatabaseName property: BIBLIO.MDB RecordSource property: Titles DBGrid control DataSource property to Data1.   Copy the following code sample to the Form1 code window: Option Explicit Dim igColumn As Integer Dim igRow As Integer Dim vargBookmark As Variant

Private Sub DBGrid1_MouseUp(Button As Integer, Shift As Integer, _      X As Single, Y As Single) igColumn = DBGrid1.ColContaining(X)  'Set the Cell Column number igRow = DBGrid1.RowContaining(Y)     'Set the Cell Row number

vargBookmark = DBGrid1.RowBookmark(igRow) 'Set Bookmark Value

'Show the contents of the cell in a textbox Text1.Text = DBGrid1.Columns(igColumn).CellValue(vargBookmark)

End Sub

 On the Run menu, click Start or press the F5 key. Click on a cell and note that the contents is shown in the TextBox.</ol>

Additional query words: kbVBp400 kbVBp600 kbdse kbDSupport kbVBp kbVBp500

Keywords: kbhowto KB149094

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.