Solved

ADO: Field cannot be a zero-length string

Posted on 2001-07-11
9
871 Views
Last Modified: 2007-11-27
I'm using ADO to create a recordset (adoTemp) from my existed database (called myTable). Anytime adding a null value to one of my string Field (DeviceName), I got an runtime error:

-214721887 : Field 'myTable.DeviceName' cannot be a zero length.


Here is my code:
Command1_Click()
   Dim adoTemp as ADODB.Recordset
   Dim strSQL as String
   Dim intDeviceID as Integer
   Dim strDeviceName as String

   strSQL = "Select * From myTable"
   set adoTemp = new ADODB.Recordset
   adoTemp.Open strSQL, myConnection, adOpenKeyset, adLockOptimistic, adCmdText

   .........
   intDeviceID = 1
   strDeviceName = ""

   adoTemp.AddNew
   adoTemp("DeviceID") =intDeviceID
   adoTemp("DeviceName") = strDeviceName
   adoTemp.Update
End Sub


Could anyone helps me solve this problem? [DeviceName] sometimes has a null value


Thanks in advance
nguyenn
0
Comment
Question by:nguyenn
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Expert Comment

by:John844
ID: 6274479
null and zero length string are two different things
database can allow nulls without allowing zero length strings for this field.
0
 
LVL 7

Expert Comment

by:John844
ID: 6274484
try changing
  adoTemp("DeviceName") = strDeviceName

to
  if strDeviceName = "" then
    'do nothing so null will go into field
  else
    adoTemp("DeviceName") = strDeviceName
  end if
0
 
LVL 7

Accepted Solution

by:
John844 earned 100 total points
ID: 6274494
if you need to update the field at some point, use similar logic to update field to vbNull if the string is ""
0
 
LVL 1

Author Comment

by:nguyenn
ID: 6274500
Hi John

But it doesnt solve problem when I update [DeviceName] to a null value
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:John844
ID: 6274511
anothir way to allow zero length string is to go into design mode of your table.  click on the field in question.  at the bottom left corner of your window there will be a item "Allow Zero Length" = No.  Change this to Yes.  This will allow your existing code to be used, but it will not put a null into the field, it will be a zero length string like "".

Hope some of this helps you solve your current problem.
John
0
 
LVL 1

Author Comment

by:nguyenn
ID: 6274512
Thanks a bunch John, it helps me out

Have a nice day :)
0
 
LVL 20

Expert Comment

by:hes
ID: 6274518
How is DeviceName defined in the database table. Is it defined to allow zero length.
0
 
LVL 1

Author Comment

by:nguyenn
ID: 6274623
Hi Hes,

You're right, I didnt set allow zero lenght to Yes in the database.

Thanks for mention it
nguyenn
0
 
LVL 20

Expert Comment

by:hes
ID: 6274908
Glad you got it solved,
John and I were typing that at the same time :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now