Minggu, 22 April 2018

Re: [MS_AccessPros] Re: Cannot delete a record in a subform

 

hi Art,

the problem with using DoCmd is that it acts on the active form, which would be the main form since that is where you are when you click the button.  Theoretically, you can change focus to the subform and then use DoCmd, but more precise would be to use SQL to delete the record in the subform, then requery the subform.  However, I will add that if I needed to do this, I would change the subform to be a continuous form and put the button to delete on that form.  You can, btw, add a button with "-" to a datasheet view in the detail section. It would show on every record, of course.

'~~~~~~~~~~~~~~~~~~
dim nMyID as long _
, nCount as long

with me.subform_controlname.form
   if .NewRecord then
      MsgBox "Not on a record",,"Can't delete"
      exit sub  
   end if
   nMyID = !ID_controlname
end with

sSQL = "DELETE t.* FROM [MyTablename] as t WHERE t.[myIDfieldname]=" & nMyID 

with CurrentDb
   .execute sSQL
   nCount  = .Recordcount 
end with

if Not nCount > 0 then
   MsBox "Problem deleting record",,"Error"
else
   me.subform_controlname.requery
   MsgBox nCount & " record" & iif(NCount<>1, "s", "") & " deleted"
end if
'~~~~~~~~~~~~~~~~~~

WHERE
MyTablename is the name of the table you want to delete a record from
myIDfieldname is the numeric primary key of MyTablename -- if it is text, then add delimiters

this is aircode, but hopefully you get the idea ~

have an awesome day,
crystal 

Wrapper Function that Calls Excel for Query in Access
https://www.youtube.com/watch?v=P60_G9L5B1w


On Friday, April 20, 2018, 3:40:05 PM CDT, wrmosca@comcast.net [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:




Hi Art - If that command button is on the main form, you can't run the commands that you have. It would be looking at the records on the main form, not the sub.

I don't have Access available to me right now. Maybe someone can step in and help you with code that refers to the sub form.

Bill


---In MS_Access_Professionals@yahoogroups.com, <dbalorenzini@yahoo.com> wrote :

I can't seem to delete a record in a subform. I have subform which is a continuous form called sfrmDocumentList. On it i have a command button that is used to delete the record that it is on. Here is the code:

Private Sub cmdDeleteIndex_Click()

If MsgBox("Are you sure you want to delete this record?", vbQuestion + vbYesNo, "Delete Current Record ?") = vbYes Then

        If Me.NewRecord Then

            DoCmd.RunCommand acCmdUndo

          Else

            DoCmd.SetWarnings False

            DoCmd.RunCommand acCmdSelectRecord

            DoCmd.RunCommand acCmdDeleteRecord

            DoCmd.SetWarnings True

        End If

    End If

End Sub


Error: The command or action 'DeleteRecord' isn't avialble now.

I am not sure why it is not working.
Any ideas would be great.

Thank you,

Art Lorenzini
Sioux Falls, SD




__._,_.___

Posted by: Crystal <strive4peace2008@yahoo.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (5)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.


.

__,_._,___

Tidak ada komentar:

Posting Komentar