Results 1 to 5 of 5
  1. #1
    yrstruly is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2024
    Posts
    122

    Delete Button

    Im trying to implement a delete button. Current button for delete is only deleting the records displaying on the form. I need to be able to press delete and insert the row id to delete that row.


    Sample Data in Table:
    HTML Code:
    SampleID	ClientID	SampleType	Cultivar	Block	Virus	SampleReceivedBy	Retest	Comments1	{1549E7C7-EF2C-4966-A291-B05DDBBC2474}						0	2	{9E587E7C-3C6B-11D0-B8BF-444553540000}						0	3	{939E41F1-5C2F-11D2-BE66-0000E8D5A293}						0	4	{77A26E81-842E-4A98-9CC8-F3AD9EA1FE72}	Root	Almond		PDV		0	5	{1549E7C7-EF2C-4966-A291-B05DDBBC2474}	Leaf	AR0512	B	PDV		0	test6	{787572EF-4BCC-4480-9D8F-28CE4475F034}	Bud	GR0286	250A	PDV	Admin	0	nh7	{AFF4CE04-E0B8-46B1-97CF-919C041BFC69}	Fruit	GR0482	41	ApMV	Michelle R	0	b8	{939E41F1-5C2F-11D2-BE66-0000E8D5A293}	Fruit	GR0669	B	ApMV		0
    Code:
    Private Sub cmdDelete_Click()    Dim strSQL As String
        
        ' Check if SampleID is entered
        If IsNull(Me.SampleID) Or Me.SampleID = "" Then
            MsgBox "No SampleID provided. Please specify a record to delete.", vbExclamation
            Exit Sub
        End If
        
        ' Single-line MsgBox for confirmation:
        If MsgBox("Are you sure you want to delete record with SampleID = " & Me.SampleID & "?", _
                  vbYesNo + vbQuestion, "Confirm Delete") = vbYes Then
                  
            ' If SampleID is numeric, do NOT use quotes:
            strSQL = "DELETE FROM tblSamples_ELISA_KERNVRUGTE WHERE SampleID = " & Me.SampleID
            
            ' If SampleID is text, you MUST use quotes:
            ' strSQL = "DELETE FROM tblSamples_ELISA_KERNVRUGTE WHERE SampleID = '" & Me.SampleID & "'"
            
            CurrentDb.Execute strSQL, dbFailOnError
            
            MsgBox "Record deleted successfully.", vbInformation
            
            ' Clear the SampleID and requery
            Me.SampleID = Null
            Me.Requery
        End If
    End Sub





    This code does not even open anything:
    Code:
    Private Sub cmdDelete_Click()    Dim strSQL As String
        Dim lngRecordsAffected As Long
        
        If IsNull(Me.txtDeleteID) Or Me.txtDeleteID = "" Then
            MsgBox "No SampleID provided. Please specify a record to delete.", vbExclamation
            Exit Sub
        End If
        
        If MsgBox("Are you sure you want to delete record with SampleID = " & Me.txtDeleteID & "?", vbYesNo + vbQuestion, "Confirm Delete") = vbYes Then
            
            ' If SampleID is numeric:
            strSQL = "DELETE FROM tblSamples_ELISA_KERNVRUGTE WHERE SampleID = " & Me.txtDeleteID
            
            ' If SampleID is text, comment the above and use:
            ' strSQL = "DELETE FROM tblSamples_ELISA_KERNVRUGTE WHERE SampleID = '" & Me.txtDeleteID & "'"
            
            CurrentDb.Execute strSQL, dbFailOnError
            lngRecordsAffected = CurrentDb.RecordsAffected
            
            If lngRecordsAffected > 0 Then
                MsgBox "Record deleted successfully.", vbInformation
            Else
                MsgBox "No record found with SampleID = " & Me.txtDeleteID, vbExclamation
            End If
            
            Me.txtDeleteID = Null
            Me.Requery
        End If
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Have you looked up Delete sql syntax?
    Have you tried that sql in the immediate window?
    Have you bothered to walk your code, line by line?
    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
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    in form design, select button object, put button on form
    select record operations
    select delete record
    done.

  4. #4
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Looks like you want them to enter the ID and then have it delete it on the form? In your delete line, your variable starts with "txt" txtDeleteID, so is it text or numeric ID?

    strSQL = "DELETE FROM tblSamples_ELISA_KERNVRUGTE WHERE SampleID = " & Me.txtDeleteID

    Might need for text:
    strSQL = "DELETE FROM tblSamples_ELISA_KERNVRUGTE WHERE SampleID = """ & Me.txtDeleteID & """" (3 " before, 4 after)

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I would use a txt pefix to indicate a textbox control. Having an ID in the name would be the autonumber field.
    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

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Delete Button vs. Delete Query
    By JennyL in forum Access
    Replies: 4
    Last Post: 02-11-2017, 10:11 PM
  2. Replies: 13
    Last Post: 05-06-2014, 12:42 PM
  3. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  4. Trying to Delete record using delete query
    By MooseOTL in forum Access
    Replies: 13
    Last Post: 10-04-2011, 02:30 AM
  5. Replies: 11
    Last Post: 03-30-2011, 01:08 PM

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
  •  
Other Forums: Microsoft Office Forums