Results 1 to 7 of 7
  1. #1
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69

    SQL Delete Recordset

    I have a code which pbaldy helped me to write (previous post), which inserts sequential values into a subform based upon a value in a textbox on the mainform.



    Main form = [frmSampleLogIn01]
    Subform = [subMycoData]
    Link Master/Child = [AccessionNo]
    Mainform textbox control = NoOfSamples

    Original code:

    Code:
    Private Sub NoOfSamples_AfterUpdate()
    'To autopopulate the [SampleNo] field in the subform with
        'sequential numbers up to the value in the [NoOfSamples] field in the main form.
        Dim intSmplNo As Integer
        Dim strSQL2 As String
     
        intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
     
        If Me.Dirty Then Me.Dirty = False
     
        For i = 1 To intSmplNo
            strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
            CurrentDb.Execute strSQL2, dbFailOnError
            Next i
    
        Me.subMycoData.Form.Requery
    
    End Sub
    Now, I am trying to add to the code to check to see if the subform is empty before executing the code above, or else to delete the records in the subform before executing the code.

    Here is what I have come up with so far:

    Code:
    Private Sub NoOfSamples_AfterUpdate()
    Dim dbs As DAO.Database
    Dim rsMycoData As DAO.Recordset
    Dim strSQL1 As String
    
    'Open pointer to current database
    Set dbs = CurrentDb()
    
    'SQL string to select records from [tblMycoData] where the [AccessionNo] & _
    is the same as on the form [frmSampleLogIn01]
    strSQL1 = "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
                Forms!frmSampleLogIn01!AccessionNo
                
    Debug.Print "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
                Forms!frmSampleLogIn01!AccessionNo
                
    'Create recordset based on SQL1
    Set rsMycoData = dbs.OpenRecordset(srtSQL1)
    
    'To check if the recordset on the subform [subMycoData] is empty
    'then insert sequential values into [SampleNo], or else delete
    'recordset before putting in new sequential values into [SampleNo]
    
    'If Me.subMycoData.Form.Recordset.RecordCount = 0 Then
    If rsMycoData.RecordCount = 0 Then
    
        'To autopopulate the [SampleNo] field in the subform with
        'sequential numbers up to the value in the [NoOfSamples] field in the main form.
        Dim intSmplNo As Integer
        Dim strSQL2 As String
     
        intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
     
        If Me.Dirty Then Me.Dirty = False
     
        For i = 1 To intSmplNo
            strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
            CurrentDb.Execute strSQL2, dbFailOnError
            Next i
    
        Me.subMycoData.Form.Requery
     
     Else
     
        'Delete the records on the subform [subMycoData] that are & _
        associated with the [AccessionNo] field that links the main & subforms
        
        Do Until Me.subMycoData.Form.Recordset.EOF
        Me.subMycoData.Form.Recordset.Delete
        Me.subMycoData.Form.Recordset.MoveNext
        Loop
        
        'All records in the subform should be deleted before continuting code below
        
            'To autopopulate the [SampleNo] field in the subform with
            'sequential numbers up to the value in the [NoOfSamples] field in the main form.
     
            If Me.Dirty Then Me.Dirty = False
     
            For i = 1 To intSmplNo
            strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
            CurrentDb.Execute strSQL2, dbFailOnError
            Next i
    
            Me.subMycoData.Form.Requery
     
     End If
     'To close the recordset
     rsMycoData.Close
     
    End Sub
    But, I can't test the rest of my code, since I get:

    Run-time error '3078':
    The Microsoft Access database engine cannot find the input table or query". Make sure it exists and that its name is spelled correctly.

    at the line:
    Code:
    Set rsMycoData = dbs.OpenRecordset(srtSQL1)
    I am guessing that the error has to do with my SQL string:

    Code:
    'SQL string to select records from [tblMycoData] where the [AccessionNo] & _
    is the same as on the form [frmSampleLogIn01]
    strSQL1 = "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
                Forms!frmSampleLogIn01!AccessionNo
    The field tblMycoData.AccessionNo is a number data type.

    I'm new to working with SQL and recordsets in VBA. I'm trying to learn them, so any help is appreciated.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You'll kick yourself:

    strSQL1 = "SELECT..."

    Set rsMycoData = dbs.OpenRecordset(srtSQL1)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    Yup, kicked myself, but glad to know I was very close! I did a little more tweaking to get the code running the way I need (added a vbOkCancel msgbox). The code runs smoothly now.

    SOLVED

    Code:
    Private Sub NoOfSamples_AfterUpdate()
    Dim dbs As DAO.Database
    Dim rsMycoData As DAO.Recordset
    Dim strSQL1 As String
    
    'Open pointer to current database
    Set dbs = CurrentDb()
    
    'SQL string to select records from [tblMycoData] where the [AccessionNo] & _
    is the same as on the form [frmSampleLogIn01]
    strSQL1 = "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
                Forms!frmSampleLogIn01!AccessionNo
                
    'Create recordset based on SQL1
    Set rsMycoData = dbs.OpenRecordset(strSQL1)
    
    'To check if the recordset on the subform [subMycoData] is empty
    'then insert sequential values into [SampleNo], or else delete
    'recordset before putting in new sequential values into [SampleNo]
    
        Dim intSmplNo As Integer
        Dim strSQL2 As String
     
        intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
        
    'If Me.subMycoData.Form.Recordset.RecordCount = 0 Then
    If rsMycoData.RecordCount = 0 Then
    
    'To autopopulate the [SampleNo] field in the subform with
        'sequential numbers up to the value in the [NoOfSamples] field in the main form.
    
     
        If Me.Dirty Then Me.Dirty = False
     
        For i = 1 To intSmplNo
            strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
            CurrentDb.Execute strSQL2, dbFailOnError
            Next i
    
        Me.subMycoData.Form.Requery
     
     Else
        Dim MsgResponse As Integer
        
        MsgResponse = MsgBox("Changing the number of samples will delete" & vbCrLf & " all records on the subform.", vbOKCancel)
        
        If MsgResponse = vbOK Then
            'Delete the records on the subform [subMycoData] that are & _
            associated with the [AccessionNo] field that links the main & subforms
        
            Do Until Me.subMycoData.Form.Recordset.EOF
                Me.subMycoData.Form.Recordset.Delete
                Me.subMycoData.Form.Recordset.MoveNext
            Loop
        
            'All records in the subform should be deleted before continuting code below
        
            If Me.Dirty Then Me.Dirty = False
     
            For i = 1 To intSmplNo
                strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
                CurrentDb.Execute strSQL2, dbFailOnError
                Next i
    
            Me.subMycoData.Form.Requery
        Else
            Exit Sub
        End If
     End If
     'To close the recordset
     rsMycoData.Close
     
    End Sub
    Another question (should I should start a new thread for this quesion?): I noticed that I have redundant code in the procedure above:

    Redundant code highlighted in red:

    Code:
    Private Sub NoOfSamples_AfterUpdate()
    Dim dbs As DAO.Database
    Dim rsMycoData As DAO.Recordset
    Dim strSQL1 As String
    
    'Open pointer to current database
    Set dbs = CurrentDb()
    
    'SQL string to select records from [tblMycoData] where the [AccessionNo] & _
    is the same as on the form [frmSampleLogIn01]
    strSQL1 = "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
                Forms!frmSampleLogIn01!AccessionNo
                
    'Create recordset based on SQL1
    Set rsMycoData = dbs.OpenRecordset(strSQL1)
    
    'To check if the recordset on the subform [subMycoData] is empty
    'then insert sequential values into [SampleNo], or else delete
    'recordset before putting in new sequential values into [SampleNo]
    
        Dim intSmplNo As Integer
        Dim strSQL2 As String
     
        intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
        
    'If Me.subMycoData.Form.Recordset.RecordCount = 0 Then
    If rsMycoData.RecordCount = 0 Then
    
    'To autopopulate the [SampleNo] field in the subform with
        'sequential numbers up to the value in the [NoOfSamples] field in the main form.
    
     
        If Me.Dirty Then Me.Dirty = False
     
        For i = 1 To intSmplNo
            strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo)  Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i  & ")"
            CurrentDb.Execute strSQL2, dbFailOnError
            Next i
    
        Me.subMycoData.Form.Requery
     
     Else
        Dim MsgResponse As Integer
        
        MsgResponse = MsgBox("Changing the number of samples will delete"  & vbCrLf & " all records on the subform.", vbOKCancel)
        
        If MsgResponse = vbOK Then
            'Delete the records on the subform [subMycoData] that are & _
            associated with the [AccessionNo] field that links the main & subforms
        
            Do Until Me.subMycoData.Form.Recordset.EOF
                Me.subMycoData.Form.Recordset.Delete
                Me.subMycoData.Form.Recordset.MoveNext
            Loop
        
            'All records in the subform should be deleted before continuting code below
        
            If Me.Dirty Then Me.Dirty = False
     
            For i = 1 To intSmplNo
                strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo)  Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i  & ")"
                CurrentDb.Execute strSQL2, dbFailOnError
                Next i
    
            Me.subMycoData.Form.Requery
        Else
            Exit Sub
        End If
     End If
     'To close the recordset
     rsMycoData.Close
     
    End Sub
    Would it be proper to put this code into different sub in a module, such as:

    Code:
    Sub AutoPopulate_subMycoData()
    
    'To autopopulate the [SampleNo] field in the subform with
        'sequential numbers up to the value in the [NoOfSamples] field in the main form.
        Dim intSmplNo As Integer
        Dim strSQL2 As String
     
        intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
     
        If Me.Dirty Then Me.Dirty = False
     
        For i = 1 To intSmplNo
            strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
            CurrentDb.Execute strSQL2, dbFailOnError
            Next i
    
        Me.subMycoData.Form.Requery
    End Sub
    and then call this sub in the other procedure when needed, using:

    Code:
    Call AutoPopulate_subMycoData
    The reasoning I have for doing this is to reduce the redundant code (normalize it?) and to call it when it is needed, thus tidying up the first procedure.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You could use a function, but in this instance I wouldn't. I'd change the logic a bit. If I understand the desired flow it's currently:

    Code:
    If there are no records Then
      Add them
    Else there are existing records
      If you want to delete them Then
        Delete old
        Add them
      Else
        Exit Sub
      End If
    End If
    I would do this, which eliminates the duplication:

    Code:
    If there ARE records Then
      If you want to KEEP them Then
        Exit Sub
      End If
    End If
    
    Add them
    Also, more efficient than your loop to delete records would be:
    Code:
    dbs.Execute "DELETE * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _  
                 Forms!frmSampleLogIn01!AccessionNo
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    Thanks again pbaldy. Works well.

    Final Code:

    Code:
    Private Sub NoOfSamples_AfterUpdate()
    Dim dbs As DAO.Database
    Dim rsMycoData As DAO.Recordset
    Dim strSQL1 As String
    
    'Open pointer to current database
    Set dbs = CurrentDb()
    
    'SQL string to select records from [tblMycoData] where the [AccessionNo] & _
    is the same as on the form [frmSampleLogIn01]
    strSQL1 = "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
                Forms!frmSampleLogIn01!AccessionNo
                
    'Create recordset based on SQL1
    Set rsMycoData = dbs.OpenRecordset(strSQL1)
    
    'To check if the recordset on the subform [subMycoData] is empty
    'then insert sequential values into [SampleNo], or else delete
    'recordset before putting in new sequential values into [SampleNo]
    
        Dim intSmplNo As Integer
        Dim strSQL2 As String
     
        intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
        
    'If Me.subMycoData.Form.Recordset.RecordCount = 0 Then
    If rsMycoData.RecordCount <> 0 Then
    
        Dim MsgResponse As Integer
        
        MsgResponse = MsgBox("Changing the number of samples will delete" & vbCrLf & " all records on the subform.", vbOKCancel)
        
        If MsgResponse = vbCancel Then
            Exit Sub
        End If
     
     End If
     
    'To force save the record data on the main form [frmSampleLogIn01]
    If Me.Dirty Then Me.Dirty = False
        
    'To delete all records on the subform [subMycoData]
    dbs.Execute "DELETE * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
                Forms!frmSampleLogIn01!AccessionNo
        
    'To autopopulate the [SampleNo] field in the subform with
    'sequential numbers up to the value in the [NoOfSamples] field in the main form.
    For i = 1 To intSmplNo
        strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
        CurrentDb.Execute strSQL2, dbFailOnError
        Next i
    
    Me.subMycoData.Form.Requery
     
    'To close the recordset
    rsMycoData.Close
     
    End Sub

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Happy to help. By the way, your original error would have been flagged for you if you had Option Explicit at the top of the module:

    http://www.baldyweb.com/OptionExplicit.htm

    You would have gotten a compile error pointing to what was an undeclared variable.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    Cool. I have it set. I can't wait to see what happens while writing my next bit of code

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

Similar Threads

  1. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  2. Recordset
    By Rick West in forum Programming
    Replies: 7
    Last Post: 11-14-2011, 02:40 PM
  3. Trying to Delete record using delete query
    By MooseOTL in forum Access
    Replies: 13
    Last Post: 10-04-2011, 02:30 AM
  4. Replies: 11
    Last Post: 03-30-2011, 01:08 PM
  5. Replies: 1
    Last Post: 11-13-2009, 03:03 AM

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