Visual Basic & SQL Server 2000

I have set a default value for a field in a table and when inserting new rows in vb with ado the default values never get populated?

When adding rows through the ent. manager the default values get populated.

Thanks,
geo90
geo90Asked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
geo90, it would and does, however you have to understand what is actually happening when you use .addnew etc; VB builds an implicit SQL statement from the fields collection which uses all of the fields in the collection and the default value of the datatype for each field (not the default for the field itself).

So if you had three fields but only set values for two of them:

Field            Field1 Field2 Field3
Default          'AA'   'BB'   'CC'
Datatype Default ''     ''     ''

With adoRecordset
  .AddNew
  .Fields("Field1") = "Hello"
  .Fields("Field2") = "Goodbye"
  .Update
End With

The statement that would be built behind the scenes is:

Insert Into MyTable (Field1,Field2,Field3) Values('Hello','Goodbye','')

Which is the default value for the string datatype.
0
 
BenjyCommented:
How are you adding new rows in ado? are you inserting values while adding the rows?
0
 
TimCotteeHead of Software ServicesCommented:
How do you insert the new records? If you use an insert statement, make sure that you do not specify the fields for which you need to use the defaults.

adoConnection.Execute "Insert Into MyTable Fields(MyField1,MyField2) Values ('hello','goodbye')"

If you have additional fields with defaults then these should be populated.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Richie_SimonettiIT OperationsCommented:
Regarding Tim's comment: If i am not wrong, using addnew method doesn't populates default values.
0
 
geo90Author Commented:
I'm using ado with the addnew method.
0
 
Richie_SimonettiIT OperationsCommented:
Then, use Tim's code.
0
 
geo90Author Commented:
Well creating sql strings is a messy deal when creating them dynamically.  It seems that the database would take care of the default values, not the method that adds the row. Hmmmmmm
0
 
geo90Author Commented:
Well that didn't help much...
I knew it wasn't working :>)
0
 
TimCotteeHead of Software ServicesCommented:
'C' geo90, don't shoot the messenger. We cannot change the behaviour of some things whether we like it or not.
0
 
MindphaserCommented:
geo90

Please read our help about questions at http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp . TimCotte's answer might not be what you expected, nevertheless it is true and therefore a at least GOOD answer. He even showed you ways how to solve your problem with other means and that's usually an 'A' grade!

If it's not possible he can't change the world, we have to accept these facts also ... so please accept my decision to chang ethe grade to 'A' here.

** Mindphaser - Community Support Moderator **
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.