Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Rotsjk is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    6

    Code to clear entry in form

    Hi,



    i've build a form/subform with a master/detail data construction. In the subform is an entry of unique numbers. In the vba code i check if the unique code allready exists in the detail table. If not then the entry is correct and additionally i update another table where all possible numbers are registered and i update the status there. So far so good.
    If the entry is a number that is allready used, i want to show a msgbox and then remove that entry before i get the standard message that the entry is in violation with the uniqueness.

    I can't figure out how to do that. The message is no problem, but how to remove that last entry?

    Thanks for help!

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Use the .Undo method of the control?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    You can use the before update event of the control to test your value and use the cancel argument if needed and undo the controls value.

    What is that number for? I would think the additional table wouldn't be necessary.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Rotsjk is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    6
    Quote Originally Posted by Welshgasman View Post
    Use the .Undo method of the control?
    I tried that, and a few other things, but i always get an Run-time Error '-2147352567 (800200009)


  5. #5
    Rotsjk is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    6
    This program is build for paper giftcertificates. I have one table where all the numbers wich exists contains. There is the status of the number on the certificate. So we know wich numbers have been returned, and wich are still outstanding. I use that table to check if one certificate is used twice. The tables i use to register the incoming certificated is called invoice_head and invoice_detail. What i need to do is to remove the entry on the invoice_detail when te status in the extra table (called certificate_status) is allready returned.
    But i geta Run-time Error '-2147352567 (800200009) ​when i try to remove the last entry

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    IMHO, validation is best done in the Before Update event of a form or in the Before Update event of its controls.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    Rotsjk is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    6
    Indeed, the validation is in the before update of the control.

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    It would help to see your code. Those long error numbers, I believe, indicate it is a control error.
    A sample database would be even better.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Rotsjk is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    6
    This is the code: The entry is in the field nummer. When i try this code i get the error on the line me.nummer.undo




    Private Sub Nummer_BeforeUpdate(Cancel As Integer)
    Dim chkBestaan, chkBestaan1, strSQL

    chkBestaan = DLookup("nummer", "bonnen_status", "nummer=" & Me.Nummer & " and soort=" & Me.Soort)
    If chkBestaan > 0 Then


    chkBestaan1 = DLookup("nummer", "bonnen_status", "nummer=" & Me.Nummer & " and soort=" & Me.Soort & " and vastgesteld='N'")
    If chkBestaan1 > 0 Then

    strSQL = "INSERT INTO Bonnen_status_TMP (nummer, soort, datum_in_omloop, datum_retour, status, eigenaar, nieuw) select nummer, soort, datum_in_omloop, datum_retour, status, eigenaar, 'N' FROM bonnen_status WHERE nummer=" & Me.Nummer & " AND soort = " & Me.Soort & ";"

    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True

    strSQL = "UPDATE Bonnen_status SET status='R', vastgesteld='J', datum_retour='" & Forms![invoice_head]!Datum & "' WHERE nummer=" & Me.Nummer & " AND soort = " & Me.Soort & ";"


    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    Else
    MsgBox ("Deze bon heeft al een definitieve status. Strookje opvragen!")

    Me.Nummer.Undo




    End If
    Else


    MsgBox ("Onbekend bonnummer. Controleren!")






    End If
    End Sub

  10. #10
    madpiet is online now Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    So check for the existence of the certificate ID in the table before doing anything. if it exists, return a message and reject the insert.

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    For the benefit of others, here's your code with indents and code tags. Makes for easier reading.
    Code:
    Private Sub Nummer_BeforeUpdate(Cancel As Integer)
        Dim chkBestaan, chkBestaan1, strSQL
    
    
        chkBestaan = DLookup("nummer", "bonnen_status", "nummer=" & Me.Nummer & " and soort=" & Me.Soort)
        If chkBestaan > 0 Then
    
    
            chkBestaan1 = DLookup("nummer", "bonnen_status", "nummer=" & Me.Nummer & " and soort=" & Me.Soort & " and vastgesteld='N'")
            If chkBestaan1 > 0 Then
    
    
                strSQL = "INSERT INTO Bonnen_status_TMP (nummer, soort, datum_in_omloop, datum_retour, status, eigenaar, nieuw) select nummer, soort, datum_in_omloop, datum_retour, status, eigenaar, 'N' FROM bonnen_status WHERE nummer=" & Me.Nummer & " AND soort = " & Me.Soort & ";"
    
    
                DoCmd.SetWarnings False
                DoCmd.RunSQL strSQL
                DoCmd.SetWarnings True
    
    
                strSQL = "UPDATE Bonnen_status SET status='R', vastgesteld='J', datum_retour='" & Forms![invoice_head]!Datum & "' WHERE nummer=" & Me.Nummer & " AND soort = " & Me.Soort & ";"
    
    
                DoCmd.SetWarnings False
                DoCmd.RunSQL strSQL
                DoCmd.SetWarnings True
            Else
                MsgBox ("Deze bon heeft al een definitieve status. Strookje opvragen!")
    
    
                Me.Nummer.Undo
    
    
            End If
        Else
    
    
            MsgBox ("Onbekend bonnummer. Controleren!")
    
    
        End If
        
    End Sub
    Without looking too closely at your code, note that you don't set cancel = true.

    I would also use the db.Execute method with dbFailOnError that will roll back changes if there's an error.

    Code:
    Dim chkBestaan, chkBestaan1, strSQL
    These are all being dimensioned as variants. I would use Dcount rather than Dlookup and dim them as integers and strSql as string.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    Rotsjk is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    6
    Hi moke123,

    i cannot see the code you've posted.
    Thank you!

  13. #13
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    i would not recommend doing anything in a Validation event.
    use the event for it's sole purpose, that is to Validate.

    if you need to Insert/Update, validate it first then
    invoke the Timer Event and do the Inserting/Updating
    on that event.

  14. #14
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    i would not recommend doing anything in a Validation event.
    use the event for it's sole purpose, that is to Validate.

    if you need to Insert/Update, validate it first then
    invoke the Timer Event and do the Inserting/Updating
    on that event.

    also, are you trying to Update (using SQL) the same record
    that is in the subform? if so that is a no, no (the db engine
    will give you warning about conflicting records).

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You can edit your posts you know?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 08-01-2018, 10:02 AM
  2. Replies: 10
    Last Post: 04-01-2018, 07:45 AM
  3. Clear a combo box if the entry is updated
    By CT_AccessHelp in forum Forms
    Replies: 2
    Last Post: 12-10-2013, 12:15 PM
  4. Replies: 2
    Last Post: 04-24-2013, 11:52 AM
  5. Command button code to clear form
    By windwardmi in forum Forms
    Replies: 15
    Last Post: 11-21-2010, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums