Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Visual Basic & SQL Server 2000

Posted on 2002-06-27
10
Medium Priority
?
168 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:geo90
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:Benjy
ID: 7113909
How are you adding new rows in ado? are you inserting values while adding the rows?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7113913
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7114709
Regarding Tim's comment: If i am not wrong, using addnew method doesn't populates default values.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:geo90
ID: 7115166
I'm using ado with the addnew method.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7115183
Then, use Tim's code.
0
 

Author Comment

by:geo90
ID: 7116254
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
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 7121229
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
 

Author Comment

by:geo90
ID: 7125794
Well that didn't help much...
I knew it wasn't working :>)
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7126305
'C' geo90, don't shoot the messenger. We cannot change the behaviour of some things whether we like it or not.
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7127099
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

618 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