Results 1 to 4 of 4

Thread: Populating a Table programmatically

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    2

    Populating a Table programmatically

    Hi.

    I'm working in VB 2008 Express.

    I have an MS Access DB with a Table setup with the columns that I need.

    I have it linked into a form, with the requisite BindingSource, TableAdapter, BindingNavigator, etc.

    On the form, I also have several textboxes linked back to the table. I can populate the table via the + (add new) button in the BindingNavigator and then filling in the textboxes (and saving of course).

    Is there a way that I can put a button on the form and populate the table automatically? I want to do this via a textbox to control the number of records added - that way, I can put in an 8 and have 8 blank records added to the table via this one click (and don't have to enter the default info in for the 8 records one at a time).

    I've attempted to do this by calling the BindingNavigatorAddNewItem_Click subroutine, filling in the textboxes and then calling MapBindingNavigatorSaveItem_Click subroutine, but this does not work.

    Any suggestions?

    Thanks,
    Ken

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populating a Table programmatically

    If you want to add N rows then you simply create N DataRows and add them to the DataTable. That would involve calling NewRow and Rows.Add N times. You won't be able to add a row with no data if any of your columns are non-nullable though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    2

    Re: Populating a Table programmatically

    Quote Originally Posted by jmcilhinney View Post
    If you want to add N rows then you simply create N DataRows and add them to the DataTable. That would involve calling NewRow and Rows.Add N times. You won't be able to add a row with no data if any of your columns are non-nullable though.
    I can see data being added (after I save) using MapTableAdapter.Insert. Is this fine to use or is there a more elegant and/or proper way to populate the table? (i.e., does this work, or should I be looking toward what you describe above?)

    Thanks,
    Ken

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populating a Table programmatically

    Once the DataTable ha sbeen populated you should be calling Update on the TableAdapter and saving the whole table in one go.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width