Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32
  1. #16
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Then this is very mysterious. I must confess I did not originate this code, although I have since done some modifications but only to the stuff that isn't relevant. The core code I posted is as was given to me.



    Not sure it matters but the variable N does not need to be declared in header, declare it in the Sub. Another apology for that, I missed copy/paste of the Dim line - but it was not in the header in my example. I really can't see anything else in my code that pertains.

    If you want to provide db for analysis, I will look at.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  2. #17
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Maybe this will help?

    Code:
    Private Sub frmEntrySub_Exit(Cancel As Integer)
    'sets module variables starting values for use in deleting selected records
    intHeight = Me.frmEntrySub.Form.SelHeight
    intTop = Me.frmEntrySub.Form.SelTop
    End Sub
    
    
    Private Sub butDelete_Click()
    
    
    Dim N As Integer        'counter for Delete function
    
    
    With Me.frmEntrySub.Form.RecordsetClone
    If .RecordCount < 1 Then
        MsgBox "No records have been saved.  Delete action cancelled.", , "DeleteRecord Error"
    ElseIf intHeight < 1 Then
        MsgBox "No records have been selected.  Delete action cancelled.", , "DeleteRecord Error"
    ElseIf MsgBox("This action may delete any saved data.  Proceed?", vbExclamation + vbOKCancel, "Delete Record?") = vbOK Then
       For N = 1 To intHeight
          .AbsolutePosition = intTop - 1    'AbsolutePosition property is 0 based counter so must -1 to get position within the recordset
          .MoveFirst
          CurrentDb.Execute "DELETE FROM EntryFormTable WHERE ID=" & !ID
          Me.frmEntrySub.Requery
       Next
    End If
    End With
    End Sub
    I would provide DB but it has thousands of company info records so :\ Making a copy and taking that out would leave nothing hehe.

  3. #18
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Here's something interesting:

    Click image for larger version. 

Name:	err7.jpg 
Views:	10 
Size:	188.0 KB 
ID:	17113

    I selected about 4 or 5 records. N is being read as 2, therefore the loop is done after 1 pass. That is why it is only deleting one record regardless of my selection. I should mention that it is deleting the top record on my list.

  4. #19
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    What are intHeight and intTop set to?

    If removing only confidential data (name, address, phone, ssn, email) leaves nothing, then dummy records are okay.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #20
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    0 on both even after I select multiple records

  6. #21
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Where is the 'Delete' button? My arrangement requires Delete button on main form. The intHeight and intTop variables are set when the subform is exited when clicking the button on main form.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #22
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Click image for larger version. 

Name:	form2.jpg 
Views:	10 
Size:	140.4 KB 
ID:	17115
    On the main form.

  8. #23
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Then all looks good. I can't help any more unless I can review and debug your file.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #24
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Scratch that. I did some debugging and I THINK .MoveFirst is not doing what it is supposed to. The ID is always the top record regardless of where my first selection is.

  10. #25
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Click image for larger version. 

Name:	err8.jpg 
Views:	10 
Size:	180.5 KB 
ID:	17118

    intTop and intHeight are storing correctly. The first record ID in this list was 31, when it should be looking at ID 34 as the first.

  11. #26
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    The code uses Me.frmEntrySub.Form.Recordset instead of Me.frmEntrySub.Form.RecordsetClone. However, even after change code still won't work.

    This is so frustrating. Cannot get the recordset to stay open past first iteration. I even tried declaring a recordset object without success.

    If you want to view and test my code, can download db from https://app.box.com/shared/r8nea07sng

    Hold down shift key while opening. Reset table links then open Login form and enter your initials (always start with the Login form but should not see it again). From Menu select Manage Samples. Select record 2014A-1181 in the datasheet list then click Edit Sample Information. The subform is in lower right. Explore code behind the Remove Tests button.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #27
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    It's not a huge deal right now. I'll just stick to my single record deletion function.

    Something else I want to automate though is the resetting of the ID autonumber. I found out that when you delete your entire table and Compact and Repair your database, your ID starts over from 1. How do I attach this procedure to a button?

  13. #28
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can compact a DB by opening it and employing the "Compact" switch.

    C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE "C:\FolderName\FileName.accdb" /compact

    Good luck with your project.

  14. #29
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    CurrentDb.Execute "DELETE FROM tablename"

    Bing: Access VBA compact and repair

    http://www.access-programmers.co.uk/...d.php?t=160025
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #30
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Yeah it's something I need to code and avoid using pathways because I'm not the only person who will be using this file, and I don't want to have to set up everyone's path on their computer.

    Another thing is if I have a part entered with up to two alts beneath it, I want the Edit function to load them into the right fields. So I want the actual part to be loaded into the main fields on the left, the \ALT beneath it to be loaded into AltA fields, and the second \ALT (if its there) to be loaded into AltB fields.

    Here is my edit code right now (only meant to edit one entry at a time upon selection and load into leftmost fields). Edit handles the button and entry loading, Add has an If to handle changes:

    Code:
    Private Sub butEdit_Click()
    
    
    'check for data existence
        If Not (Me.frmEntrySub.Form.Recordset.EOF And Me.frmEntrySub.Form.Recordset.BOF) Then
            'Load existing entry into form
            With Me.frmEntrySub
                Me.txtAssembly = !Assembly
                Me.txtComponent = !Component
                Me.txtDescription = !Description
                Me.numAssemblyQty = !AssemblyQty
                Me.txtUOM = !UOM
                Me.txtAlternateA = !AlternateA
                Me.txtAlternateB = !AlternateB
                Me.numQtyA = !QtyA
                Me.numQtyB = !QtyB
                Me.numQtyC = !QtyC
                Me.dbPriceA = !UnitPriceA
                Me.dbPriceB = !UnitPriceB
                Me.dbPriceC = !UnitPriceC
    'store ID in tag in case of change
                Me.ID.Tag = !ID
    'change Add button caption
                Me.butAdd.Caption = "Update"
    'disable Edit button
                Me.butEdit.Enabled = False
            End With
        End If
        
    End Sub
    
    Private Sub butAdd_Click()
    'Add entry to list
    
    
    'Insert vs Update options
    
    
    'If the part is not in the list
        If Me.ID.Tag & "" = "" Then
    
    
    'add the part to the list
        
        'Add main part
        CurrentDb.Execute "INSERT INTO EntryFormTable(Assembly, Component, Description, AssemblyQty, UOM, QtyA, QtyB, QtyC, UnitPriceA, UnitPriceB, UnitPriceC) " & _
        " VALUES ('" & Me.txtAssembly & "','" & Me.txtComponent & "','" & Me.txtDescription & "','" & Me.numAssemblyQty & "','" & Me.txtUOM & "','" & Me.numQtyA & "','" & _
        Me.numQtyB & "','" & Me.numQtyC & "','" & Me.dbPriceA & "','" & Me.dbPriceB & "','" & Me.dbPriceC & "')"
        
        
        'If there is an AlternateA, Add AlternateA
        If Not (Me.txtAlternateA & "" = "") Then
        CurrentDb.Execute "INSERT INTO EntryFormTable(Component, Description, UOM) " & _
        " VALUES ('" & Me.txtAlternateA & "','" & Me.txtAltADescription & " \ALT" & "','" & Me.txtAltAUOM & "')"
        End If
        
        'If there is an AlternateB, Add AlternateB
        If Not (Me.txtAlternateB & "" = "") Then
        CurrentDb.Execute "INSERT INTO EntryFormTable(Component, Description, UOM) " & _
        " VALUES ('" & Me.txtAlternateB & "','" & Me.txtAltBDescription & " \ALT" & "','" & Me.txtAltBUOM & "')"
        End If
        
    'update an already existing part
        Else
        CurrentDb.Execute "UPDATE EntryFormTable " & _
            " SET Assembly='" & Me.txtAssembly & "'" & _
            ", Component='" & Me.txtComponent & "'" & _
            ", Description='" & Me.txtDescription & "'" & _
            ", AssemblyQty='" & Me.numAssemblyQty & "'" & _
            ", UOM='" & Me.txtUOM & "'" & _
            ", QtyA='" & Me.numQtyA & "'" & _
            ", QtyB='" & Me.numQtyB & "'" & _
            ", QtyC='" & Me.numQtyC & "'" & _
            ", PriceA='" & Me.dbPriceA & "'" & _
            ", PriceB='" & Me.dbPriceB & "'" & _
            ", PriceC='" & Me.dbPriceC & "'" & _
            " WHERE ID='" & Me.frmEntrySub!ID & "'"
            
            CurrentDb.Execute "UPDATE EntryFormTable " & _
            " SET AlternateA='" & Me.txtAlternateA & "'" & _
            ", Description='" & Me.txtAltADescription & "'" & _
            ", UOM='" & Me.txtAltAUOM & "'" & _
            " WHERE ID='" & Me.frmEntrySub!ID & "'"
            
            CurrentDb.Execute "UPDATE EntryFormTable " & _
            " SET AlternateB='" & Me.txtAlternateB & "'" & _
            ", Description='" & Me.txtAltBDescription & "'" & _
            ", UOM='" & Me.txtAltBUOM & "'" & _
            " WHERE ID='" & Me.frmEntrySub!ID & "'"
        End If
        
    'Clear form after add/update
        butClear_Click
    
    
    'Refresh form
        frmEntrySub.Form.Requery
    
    
    End Sub

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 07-03-2013, 10:02 AM
  2. delete multiple records at once
    By akrylik in forum Access
    Replies: 5
    Last Post: 04-22-2012, 08:13 AM
  3. Replies: 5
    Last Post: 01-24-2012, 06:19 PM
  4. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  5. Multiple Subform records
    By Lupson2011 in forum Access
    Replies: 2
    Last Post: 08-24-2011, 08:49 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