Question : Blank characters being inserted into SQL Table via ADODB

Overall task:  I am reading an Excel spreadsheet and populating a SQL database with the information read from the spreadsheet.  The following code snippet shows how I am writing to a field called "FirstName".

    ' declare a recordset
    Dim myTableRS As ADODB.Recordset
    ' make recordset
    Set myTableRS = New ADODB.Recordset
    ' open recordset using a table in the database, and the connection
    myTableRS.Open "tblContactInformation", g_adoCon, adOpenDynamic, adLockPessimistic
    myTableRS.AddNew
    str = g_WS.Cells(4, 2)
    myTableRS.Fields("FirstName") = StrValue(str)
    myTableRS.Update

The function StrValue is shown below, but most importantly it Trims what has been read from the Excel spreadsheet.

Function StrValue(str As String) As String
    str = Trim(str)
    If (Len(str) = 0) Then
        str = "????"
    End If
    StrValue = str
End Function

The value returned by StrValue is correct, but once it is assigned to Fields("FirstName") there are extra blanks on the end of the field.  "FirstName" is declared as a nvarchar(40) field which does not accept nulls.

Thanks for your help.

Answer : Blank characters being inserted into SQL Table via ADODB

>>This causes each field to be blank filled. <<
Actually that is not what it states.  Here it is again:

The SET ANSI_PADDING setting does not affect the nchar, nvarchar, ntext, text, image, and large value. They always display the SET ANSI_PADDING ON behavior. This means trailing spaces and zeros are not trimmed.

So if there are trailing spaces it is coming from your code.

But don't take my word for it, test the following out in SSMS:
1:
2:
3:
4:
5:
INSERT tblContactInformation(FirstName) VALUES ('SheahaST')

SELECT	'|' + FirstName + '|'
FROM	tblContactInformation
WHERE	FirstName = 'SheahaST'
Random Solutions  
 
programming4us programming4us