5

Here is the situation: There's a SQL Server database with a Microsoft Access front end. There is a table in the SQL Server database with a column called PackID which is defined as VARCHAR(6) NOT NULL (it has to be NOT NULL because it's part of a composite primary key for this table). Empty strings are legitimate in the PackID column. In fact, they occur quite often. When the user enters data in the UI for this table, and tabs through the PackID field, an error message appears: "Cannot insert the value NULL into column 'PackID'; column does not allow nulls. INSERT fails."

Things that have been tried that do not work:

  1. adding a default constraint on this column in the SQL Server database with a value of '' - apparently the default is only used if the column is omitted from the INSERT statement
  2. setting the Default Value of the PackID field in Access to "" - it behaves as though "" is a NULL
  3. calling the following VBA code when user moves off row in UI (Lost Focus event)

    If IsNull(PackID.Value) Then
       PackID.Value = ""
    End If
    

Does anyone know how to force an empty string in Access so it's interpreted as an empty string for SQL Server and not a NULL?

15
  • Does the line IsNull(ComponentPackID.Value) return True? Commented Apr 16, 2015 at 13:38
  • It's probably a thing in Access to make your life "easier" to send NULLs for a blank string, because in the bad old VBA days, there weren't good ways to deal with NULL values. It is hacky but how about a single space, then trim everything back on its way in? Commented Apr 16, 2015 at 13:41
  • 1
    Do you have any control over the SQL server? Is it an option to drop a trigger in there, to convert NULLs to empty strings before insert? (Note: I haven't tested this, but if it isn't an option, then there's no point in seeing if it works.) Commented Apr 16, 2015 at 13:44
  • 1
    Perhaps it would be useful if you update how you link SQL tables (suppose not adp project) - SQL Server Native Client ODBC driver? Commented Apr 16, 2015 at 14:12
  • 1
    Check this, but no help: bytes.com/topic/access/answers/… Commented Apr 16, 2015 at 14:14

2 Answers 2

1

Setting the Default Value in the text box Properties for PackID in Access to this

=" "

worked. The space between the double quotes is very important. Leaving the space out causes the insert to fail. In SQL Server LEN(PackID) returns 0. For instance:

SELECT LEN(''), LEN(' ');

both return 0. It appears as though SQL Server treats both of these as empty strings.

Sign up to request clarification or add additional context in comments.

1 Comment

So for me it is clear: never create app in Access with SQL Server database... Sadly they have dropped ADP support in Access 2013.
0

An alternative solution to this problem (no Default Value in text box Properties for PackID).

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(PackID.Value) Or PackID.Value = "" Then
        PackID.Value = " "
    End If
End Sub

This still uses the concept of passing SQL Server " " (read this as quote space quote) as the field's value. On the UI side, this is an alternative to setting a default value for the field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.