Microsoft KB Archive/191717

= How To Display Boolean Values in Bound DataGrid =

Article ID: 191717

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Data Access Components 2.0
 * Microsoft Data Access Components 2.1
 * Microsoft Data Access Components 2.5
 * Microsoft Data Access Components 2.6
 * Microsoft Data Access Components 2.7

-



This article was previously published under Q191717



SUMMARY
An empty binary field formatted with rules to print text, such as "Yes" for True and "No" for False, will return an error unless nulls are allowed.



Steps to Reproduce Behavior
 Create a new standard EXE project in Visual Basic. Form1 is created by default. In the project, reference Microsoft ActiveX Data Objects 2.0 Library and Microsoft Data Formatting Object Library. Add a DataGrid to the form.  Add the following code and run: Option Explicit

Private rs As ADODB.Recordset Private fmtBooleanData As StdDataFormat

Private Sub Form_Load Dim i As Integer

Set rs = New ADODB.Recordset rs.Fields.Append "Field1", adBSTR, 64 rs.Fields.Append "BooleanField", adBoolean rs.Open

rs.AddNew rs.Fields("Field1").Value = "Field1" rs.Fields("BooleanField").Value = True rs.Update

For i = 1 To 5 rs.AddNew rs.Update Next i     rs.MoveFirst

Set DataGrid1.DataSource = rs

' set up Boolean Formatting Set fmtBooleanData = New StdDataFormat fmtBooleanData.Type = fmtBoolean fmtBooleanData.TrueValue = "Yes" fmtBooleanData.FalseValue = "No" fmtBooleanData.NullValue = ""

Set DataGrid1.Columns(1).DataFormat = fmtBooleanData

End Sub



When the code is run, you will see "Yes" in the 2nd column of the first row, and #ERROR in the rest.

This occurs because OLE DB cannot determine what to return for an empty non-nullable field, and therefore raises an error. Marking the field as nullable by changing the line:

rs.Fields.Append "BooleanField", adBoolean

to:

rs.Fields.Append "BooleanField", adBoolean,, adFldIsNullable

will result in the empty fields returned as NULLs rather than errors.

Keywords: kbhowto kbdatabase kbctrl KB191717

-

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

© Microsoft Corporation. All rights reserved.