Results 1 to 7 of 7

Thread: [RESOLVED] One to One Relationship in VB2005

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Resolved [RESOLVED] One to One Relationship in VB2005

    Hi,
    I have three tables that have a relationship one-one (using MSAccess). However, in VB2005 when I add a record in the first table the other tables does not generate a record as it do when the relationship is one-many. I'm not using datagrids. I'm using table1 in top of a form and the table2/table3 are in the same form as table1 but in tabs control.
    Should i do some coding to manually relate table1 with the others....I'm really lost with this one-one relationship using Access and VB2005.

    I have setup the database like this sample:

    Table1:
    table1ID - PK - Autonumber
    Names - Text
    LastName - Text

    Table2:
    table2ID - PK - Autonumber
    table1ID - LongInteger and related one-one with table1(table1ID), Also Yes/No Duplicates.
    Address - Text
    State - Text

    Thanks,
    2005

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: One to One Relationship in VB2005

    if you tell it to insert into table 1, why would you expect it to insert into table 2? you insert into table1, get the id, then use that to insert into table2....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: One to One Relationship in VB2005

    So, once i get the ID from the table1 should i get that ID and either by code or manually put it in the table2(table1ID)? Does the One to One relationship setup from the first post is correct?

    Best Regards,
    2005

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

    Re: One to One Relationship in VB2005

    You DB relationship is fine. After you insert the parent record you need to perform a query to get the last ID generated, put it into the child record and then insert that too. This sort of stuff is easier with a real database, e.g. SQL Server, as the Jet OLEDB provider doesn't support multiple SQL statements in a single command. With multiple statements you can combine the first INSERT and the SELECT into a single command, which makes things much cleaner.
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: One to One Relationship in VB2005

    ok, then i will create the DB using SQLExpress. I have no problem either on creating the db using SQL or conecting to it. But i might have some problems on combining the statements into a single command as you mentioned above. Any sample to read about this will be helpfull, however i will do some research.

    Thank you very much,
    2005

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

    Re: One to One Relationship in VB2005

    You can combine multiple inline SQL statements for SQL Server simply by separating them with a colon, e.g.
    vb.net Code:
    1. myCommand.CommandText = "SELECT * FROM Table1; SELECT * FROM Table2"
    In your case you'd have an INSERT statement followed by a SELECT statement. The query is going to get the value of SCOPE_IDENTITY() and either populate a row field or an output parameter. If you create a typed DataSet the wizard will offer to generate such SQL for you, so you could either do that for real or maybe just to see what the SQL looks like.

    Of course, you can also use stored procedures with multiple statements in them.
    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
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: One to One Relationship in VB2005

    Thank you for the information. Now i have good info to start again.

    Best Regards,
    2005

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