Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

PRB: ADO Recordset AddNew and Update Methods Ignore ANSI PADDING OFF Setting


View products that this article applies to.

This article was previously published under Q293873

↑ Back to the top


Symptoms

When you update tables in SQL Server 7.0, the AddNew and Update methods of the Microsoft ActiveX Data Objects (ADO) Recordset object ignore the ANSI PADDING OFF setting. These methods do not trim the trailing spaces or blanks, even if the table is created with SET ANSI_PADDING OFF.

In addition, it does not matter if the USE Ansi NULLS, paddings and warnings option is turned on or off in the Data Source Name (DSN).

↑ Back to the top


More information

Steps to Reproduce Behavior

  1. Create a test table named testpadoff with ANSI_PADDING OFF as follows:
    SET ANSI_PADDING OFF
    CREATE TABLE testpadoff
    (UserName varchar(10))
    					
  2. Create a Standard EXE project in Visual Basic.
  3. Add a reference to ActiveX Data Object Library.
  4. Add the following code to the Form_Load event.

    Note You must change User ID =<UID> and password =<strong password> to the correct values before you run this code. Make sure that <UID> has the appropriate permissions to perform this operation on the database.
    Dim adConn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim strSQL As String
    Dim strName As String
    
    adConn.Open "Data Source=(local);Initial Catalog=Test;User ID=<UID>;Password=<strong password>"
    '  Inserting through the connection object always removes padding because
    '  the table was created with ANSI_PADDING off. 
    strSQL = "insert into TestPadOff values('" & "John  " & "')"
    adConn.Execute strSQL
    
    '  Inserting through the Recordset object always preserves padding,
    '  regardless of the 'SET ANSI_PADDING OFF' setting on the Connection object.
    adConn.Execute "set ansi_padding off"
    'rs.cursorlocation = adUseClient
    rs.Open "select * from TestPadOff", adConn, adOpenDynamic, adLockOptimistic
    
    rs.AddNew
    rs.Fields(0).Value = "Mike   "
    rs.Update
    
    rs.MoveLast
    strName = rs.Fields(0).Value
    Debug.Print rs.Fields(0).Value & " " & CStr(Len(strName))
    rs.Close
    adConn.Close
    
    Set rs = Nothing
    Set adConn = Nothing
    End Sub
    					
  5. Run the following query in SQL Server Query Analyzer.
    select '<' + UserName + '>' from testpadoff
    							
    The padding is preserved for the record that is added using the Recordset object.
  6. Uncomment the following line:
    rs.CursorLocation = adUseClient
    					
  7. Run the project.
  8. Run the following query in SQL Server Query Analyzer:
    select '<' + UserName + '>' from testpadoff
    							
    The padding is not preserved when you use client-side cursor.

↑ Back to the top


References

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
154886� INF: Behavior of ANSI_PADDING

↑ Back to the top


Keywords: KB293873, kbprb

↑ Back to the top

Article Info
Article ID : 293873
Revision : 5
Created on : 12/3/2003
Published on : 12/3/2003
Exists online : False
Views : 377