Results 1 to 4 of 4

Thread: Cancel INSERT in a BeforeInsert Trigger

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5

    Unhappy Cancel INSERT in a BeforeInsert Trigger

    Hi all,

    I have the following code in the beforeinsert trigger of a company table:

    Code:
    // Trigger onbeforeinsert for table company
    if reccount() <= 1
    	return .t.
    else
    	messagebox("Only one company can be created!")
    	delete	
    	return .f.
    endif
    What I want is for a new record not to be created if one already exists. In the above code the message gets displayed but a new record still get inserted which i have to delete manually as the above code does not work.

    The insert button of a form section is used to create the new record.
    How do i fix this?

    Best Regards

  2. #2
    Lianja Development Team barrymavin's Avatar
    Join Date
    Feb 2012
    Location
    UK, USA, Thailand
    Posts
    7,180
    Blog Entries
    22
    The "beforeinsert" trigger is called before the record is written into the table so there should be no need for the "delete" command. What you may need to do is reposition on the current record as the buffer contains the new data to be inserted.

    Code:
    // Trigger onbeforeinsert for table company
    if reccount() <= 1
            return .t.
    else
            messagebox("Only one company can be created!")
            goto recno()
            return .f.
    endif
    Principal developer of Lianja, Recital and other products

    Follow me on:

    Twitter: http://twitter.com/lianjaInc
    Facebook: http://www.facebook.com/LianjaInc
    LinkedIn: http://www.linkedin.com/in/barrymavin

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks for your reply Barry. I changed my code as you suggested but a new blank record still gets displayed. If i hit the previous button I get the current record due to the recno() function. But it's still not clean enough. How do I get it to NOT display the new record in the first place, or if that's not possible to refresh the data after calling the recno() function?

    Best Regards

  4. #4
    Lianja Development Team barrymavin's Avatar
    Join Date
    Feb 2012
    Location
    UK, USA, Thailand
    Posts
    7,180
    Blog Entries
    22
    Yes. Supposing your form section is called "section1", then this should work for you.

    Code:
    Lianja.getElementByID("section1").refresh()
    Principal developer of Lianja, Recital and other products

    Follow me on:

    Twitter: http://twitter.com/lianjaInc
    Facebook: http://www.facebook.com/LianjaInc
    LinkedIn: http://www.linkedin.com/in/barrymavin

Bookmarks

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
  •  
Journey into the Cloud
Join us