Alpha Video Training
Results 1 to 4 of 4

Thread: a5 grid component - insert record to two linked tables

  1. #1
    Member
    Real Name
    Bruce Zweig
    Join Date
    Jun 2010
    Location
    San Francisco, USA
    Posts
    7

    Default a5 grid component - insert record to two linked tables

    SUMMARY: Access lets you join two tables into an updateable (and insertable) view without any code. That's right, Access will let you insert two linked records into two linked tables without any code. The A5 grid control doesn't do this, and it's buggy if you try to import the access view. Here's an incredibly simple database that unambiguously illustrates this issue. What's the best way to handle this?

    DETAIL:
    I have a database in MSAccess with two linked tables, linked by a left join. For discussion purposes, I call this the 'Men's club' database, and it has two tables : tblMen and tblWives. The database records the members and, if they are married, their wives. I use two linked tables, one for the members, and another for their wives. My real app is more complex, but I've simplified it in order to unambiguously encapsulate the joined table issue.

    Each member of the Men's Club has a record in tblMen (sorry ladies, I'm afraid they are all men), with fields:
    mID integer autoincrement
    mFirstname text
    mLastname text
    mWifeID integer ' external link to wife's record, if present

    tblWives has 3 fields:
    wID integer autoincrement
    wFirstname text
    wLastname text
    (again my apologies to domestic partners who don't fit this oversimplified model)


    I first created a view in MSAccess with a left join of tblMen and tblWives where tblMen.mWifeID = tblWives.wID. I immediately got a nice form where the member's name and his wife's name are all on one line. If I enter a member's name, but I do not enter a wife's name, then just a tblMen record is created. However, if I also enter his wife's name in the view's wFirstname and wLastname fields, then Access creates both a tblMen record for the man and a tblWives record for his wife, and stores the newly created Wives.wID in the man's mWifeID field. Access does this without any code. I'm not always a Microsoft fan, but this seems pretty intelligent on their part.

    If I try to do this in an Alpha 5 grid control defined by the same SQL left outer join, it doesn't work. Alpha 5 will not create the wife record, so it either gives an error or just ignores the wife data, depending on which grid template I try.

    If I do this with the Alpha 5 template for read-only grid with updateable detail, and base the grid's query on the joined MSAccess view, then A5 almost works. Basically, it inserts the data OK, but you have to close the grid and reopen it to see the data.

    (After I save the new record, the new record is not displayed in the Alpha 5 grid. Instead, a random previous record is repeated in the last row of the grid. Also, in this instance, the alpha 5 insert causes the access view to add a wife record every time, even if no wife data is entered. And, there is another problem if there is no wife data -- it gives an error message, but again, if you open and close the grid then you do get to see the data).

    Anyhow, my questions are these:
    (1) what's the best way to get the grid component to do this -- i.e., update two tables from one line in the grid? It looks like the grid based on a sql query can't do this without code. I realize that inserts into 2 tables are not supported in the most rigorous interpretation of SQL (there is an INSTEAD OF clause), but Access seems to be designed to let you do this without writing any code. More specifically, what's the easiest way to get the wives' record inserted in A5?

    (2) Is there any way to get the grid to correctly display after insert into an Access view? The view based grid seems to update the database OK, but you have to close the grid and repopen it to get it to display correctly. If this worked, I'd be able to import my codeless two table Access view into an A5 grid control and take the rest of the day off. This would be nice ;-)

    I've seen a few other posts on this topic, but never any clear resolution. This example is quite obvious and trivially replicable, and I'd be glad to upload the test tables and grid components I've based it on.

    Thanks,

    Bruce
    Last edited by bruce2g; 10-29-2013 at 01:32 AM. Reason: typos, added summary

  2. #2
    Member mvaughn's Avatar
    Real Name
    Michael Vaughn
    Join Date
    Aug 2012
    Location
    Longview, Texas, USA
    Posts
    703

    Default Re: a5 grid component - insert record to two linked tables

    Could you provide a copy of your grid's SQL Select Statement (Query AlphaDAO)?

  3. #3
    Member
    Real Name
    Bruce Zweig
    Join Date
    Jun 2010
    Location
    San Francisco, USA
    Posts
    7

    Default Re: a5 grid component - insert record to two linked tables

    I've tried it two ways. The first bases the A5 grid control on an Alpha DAO SQL Select statement:
    SELECT *
    FROM Men Men
    LEFT OUTER JOIN Wives Wives
    ON Men.mWifeID = Wives.wID

    mID is the primary key of table Men (autoincrement), and wID is the primary key of table wives.

    -- this does not work: if you add a record, and type in the man's name and his wife's name, only the man's name is saved, and no wife record is created.

    To get it to work, I instead created a view within Access, called linkMenWivesOnmWifeID_wID, and the access genie created the following SQL:
    SELECT Men.*, Wives.*
    FROM Men LEFT JOIN Wives ON Men.mWifeID = Wives.wID;

    This time, I used the A5 flying start genie to create my grid from scratch, and it did work (pretty much). Something about the flying start removed the record display problems. I notice that now, in detail view, on a new record, the wID field says <Auto> where before it was just an empty text box.

    The only problem remaining is that a wife record is always created, even if the wife's name is left blank. (I suppose I could create a SQL query to delete the blank wife records and null out the link, and run it when necessary for the time being).

    This really helps, though. I wish I could figure out how the wID got changed to <Auto> -- all the properties for the wID field look the same in both the version with <auto> in the wife table's ID and the version where it was just a text field.

    Bruce
    Last edited by bruce2g; 10-29-2013 at 09:44 PM.

  4. #4
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: a5 grid component - insert record to two linked tables

    hello

    this is the way alpha works, as i understands it. a table needs to be created then only the auto increment number is assigned and any relationship based on that will exist after that.
    i know you gave simplified version of your problem using men and wives.
    on the primary table you have an id field which references the secondary table auto increment field so whenever a record to the primary table is created automatically a blank record is added to the secondary table. in your case if you want to create a primary table and if there is any dependent record on that primary table then after saving the primary table you can create one or skip if there is none.
    to think in alpha terms, using your simplified example ( i am using mysql vocabulary)
    id integer auto increment primary key
    last_name varchar(20)
    first_name varchar(20)
    married tinyint
    primary table.

    id integer auto increment primary key
    last_name varchar(20)
    first_name varchar(20)
    primary_record_id int
    secondary table.

    note i have the foreign key in the secondary dependent table.
    now create a dialog(which i like ) or grid if you must. then after you save the record, the primary key of the last inserted record is available in the e object. use that to create the secondary record if the condition married is true, if not skip.
    whether you use dialog or grid the dependent part can be hidden unless the married is true.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

Similar Threads

  1. Insert From 1 Grid into 1+ tables
    By MarionT in forum Application Server Version 11 - Web/Browser Applications
    Replies: 1
    Last Post: 02-25-2012, 06:12 PM
  2. Insert into SQL linked tables?
    By eskimoavenue in forum Application Server Version 11 - Web/Browser Applications
    Replies: 3
    Last Post: 01-18-2012, 09:41 AM
  3. Insert Grid Component Code not firing....
    By omagarc in forum Application Server Version 10 - Web/Browser Applications
    Replies: 6
    Last Post: 07-26-2011, 11:24 PM
  4. Back to insert record into My_SQL via Dialog Component
    By Dan Blank in forum Application Server Version 9 - Web/Browser Applications
    Replies: 8
    Last Post: 03-04-2009, 11:58 AM
  5. Insert Record into MySQL via Dialog Component
    By Dan Blank in forum Application Server Version 9 - Web/Browser Applications
    Replies: 4
    Last Post: 02-25-2009, 03:39 PM

Bookmarks

Posting Permissions

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