Results 1 to 15 of 15

Thread: [RESOLVED] VB and Databases

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Resolved [RESOLVED] VB and Databases

    hello people,
    Could someone give me a link or set of links that provide tutorials on how to search for records, edit records, add records, and delete records through a form in VB 2012?
    All help appreciated.
    Thanks

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

    Re: VB and Databases

    Follow the Data Walkthroughs link in my signature below. There's a Forms Over Data walkthrough that's probably the first one you should check out.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB and Databases

    Quote Originally Posted by jmcilhinney View Post
    There's a Forms Over Data walkthrough that's probably the first one you should check out.
    Hmmm... looks like that one's gone nowadays but there are plenty more relevant ones there.
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: VB and Databases

    thanks for the walkthroughs, I solved most of my problems using this handy toolbar
    Name:  toolbar.JPG
Views: 295
Size:  10.3 KB

    The table I'm working with will have no records at first, so the user will have to create a new one.
    The problem:
    Complaint ID is the primary key of this table. When I create my first record, the first Complaint ID value starts with -1. How can I change this to zero?
    Name:  toolbarerror.JPG
Views: 373
Size:  18.0 KB
    thanks

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB and Databases

    Quote Originally Posted by bruel1999 View Post
    When I create my first record, the first Complaint ID value starts with -1. How can I change this to zero?
    It sounds like you are displaying the record index instead of the number of the record.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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

    Re: VB and Databases

    Quote Originally Posted by bruel1999 View Post
    thanks for the walkthroughs, I solved most of my problems using this handy toolbar
    Name:  toolbar.JPG
Views: 295
Size:  10.3 KB

    The table I'm working with will have no records at first, so the user will have to create a new one.
    The problem:
    Complaint ID is the primary key of this table. When I create my first record, the first Complaint ID value starts with -1. How can I change this to zero?
    Name:  toolbarerror.JPG
Views: 373
Size:  18.0 KB
    thanks
    Bad idea. What happens is that the underlying DataTable generates a temporary ID for the record and then, when you save the changes to the database, the database generates a final ID. The reason that the DataTable uses negative numbers is to ensure that they will never be the same as the IDs for records that are already in the database.

    I know that SQL Server starts at 1 by default and Access may be the same or it may start at 0. Those values can be changed, as can the ones used by a DataTable, but I would recommend against it.

    If you're using auto-generated IDs then the point is that the values are guaranteed to be unique without you're having to think about it. What the actual values are should be of no consequence and often shouldn't even be displayed to the user. They are primarily, if not only, for internal identification.
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: VB and Databases

    Quote Originally Posted by jmcilhinney View Post
    Bad idea. What happens is that the underlying DataTable generates a temporary ID for the record and then, when you save the changes to the database, the database generates a final ID. The reason that the DataTable uses negative numbers is to ensure that they will never be the same as the IDs for records that are already in the database.

    I know that SQL Server starts at 1 by default and Access may be the same or it may start at 0. Those values can be changed, as can the ones used by a DataTable, but I would recommend against it.

    If you're using auto-generated IDs then the point is that the values are guaranteed to be unique without you're having to think about it. What the actual values are should be of no consequence and often shouldn't even be displayed to the user. They are primarily, if not only, for internal identification.
    Yes I noticed that in the actual database, the values were 1, 2, and so on. What do I do if I want to show the user the correct value of Complaint ID?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB and Databases

    Only show it after it has been added to the database... because before that, you cannot tell what the value will be (as another record could be added by another program/user before you add one).

    However, is there actually a reason to show it to them? In the vast majority of cases there is no need at all (it just 'feels wrong' to hide something), and it is also likely to just confuse the user rather than give them any benefit.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: VB and Databases

    Don't show it at all. The primary key has to be unique, and it is likely that the complaint ID has to be unique, so it does make sense that the primary key be teh complaint ID, but that will end up limiting you down the road. For example, if you make the primary key an identity field such that the DB increments that number, and you use that PK as the complaint ID, then there are a bunch of things you will not be able to do with the complaint ID. You may not want to do those things now, and you may never want to do those things, but by making the complaint ID the PK and making it an identity field, you wouldn't be able to. Some of the things you wouldn't be able to do would be:

    1) Resurrect a complaint that was deleted, but needs to be restored with an existing (but currently not used) number.
    2) Change the format of the complaint ID from a number to something more descriptive such as YY-NNNN (so that the year is part of the number), or anything of the sort.

    Of course, you could make the complaint ID the PK and not make it an identity, but then you'd have to determine and assign the new number when you create the record, which can be a bit of a challenge in a multi-user environment.
    My usual boring signature: Nothing

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

    Re: VB and Databases

    In your case, do you have to give the user an ID for the record? If so, is there an reason that they have to have that ID before the record is saved? The record doesn't actually exist until it's saved so, while some people like to be able to assign a number first, it doesn't really make sense from a programming perspective. That's not the only perspective you have to consider of course, but it is an important one. What if you do generate the number up front and then the user doesn't save the record? Does that value go by the wayside or do you reuse it later? If you reuse it, what mechanism do you use to prevent the value being reused between when it's generated and when the user discards the record but allow it to be reused after that? Stuff like that doesn't happen by magic. Anything is possible but you have to have a clear idea of exactly what needs to happen under all possible circumstances and then you can implement the system accordingly.
    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: VB and Databases

    1) Resurrect a complaint that was deleted, but needs to be restored with an existing (but currently not used) number.
    Sorry for not giving all the necessary details, but the user will not have access to the delete button.


    Quote Originally Posted by jmcilhinney View Post
    In your case, do you have to give the user an ID for the record? If so, is there an reason that they have to have that ID before the record is saved?
    Yes, I have to give the user the ID for reference. And no, no such reason. Thanks for the advice. I'll give em the ID after they save the record.

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

    Re: VB and Databases

    Quote Originally Posted by bruel1999 View Post
    Yes, I have to give the user the ID for reference. And no, no such reason. Thanks for the advice. I'll give em the ID after they save the record.
    The question then becomes, how will you get the ID when the record is saved? The fact that you're using a BindingNavigator suggests that you have a DataTable under the hood, so you you should probably be using a data adapter or table adapter to save your changes. Is that the case? What database are you using?
    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

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: VB and Databases

    Yes, I'm using a table adapter. I don't get what you mean exactly, but it's just a local .accdb file

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

    Re: VB and Databases

    Quote Originally Posted by bruel1999 View Post
    Yes, I'm using a table adapter. I don't get what you mean exactly, but it's just a local .accdb file
    In that case, follow the CodeBank link in my signature below and check out my thread on Retrieving AutoNumber Values From Access (or something like that).
    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

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: VB and Databases

    thanks!

Tags for this Thread

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