Link to home
Start Free TrialLog in
Avatar of Powerhousecomputing
Powerhousecomputing

asked on

VBA Code to Click Button

I have a button in my form to Delete Record.  I would like to write some code so that when I click abother button it clicks the Delete Record button - or is there some code I can use to delete the curretn record?
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

in the other button

private sub OtherButton_click()
cmdDeleteButton_click
end sub


cmdDeleteButton is the name of the delete record button
Hi Powerhousecomputing,

Add
Run cmdDelete_Click
 to the end of your code, where CmdDelete is the name of your delete button


Good Luck!

Gary
Avatar of Powerhousecomputing
Powerhousecomputing

ASKER

It doesn't like that coding - my Delete buitton is called DeleteRecord
Run cmdDeleteRecord_click

doesn't like it!



private sub OtherButton_click()
DeleteRecord_click
end sub
Is it possible for it to ask IF I want to delete the rercord first?
still does not like Run cmdDeleteRecord_click
Powerhousecomputing,

why don't you try this


           DeleteRecord_click
Ok - here is my code -

Private Sub Validate_Click()
If DCount("[first name]", "Audition Applications", "[first name]=" & Chr(34) & Me.[first name] & Chr(34) & " and [surname]=" & Chr(34) & Me.[Surname] & Chr(34) & " and [date of birth]=#" & Me.[date of birth] & "#") > 0 Then

MsgBox "Applicant Already Entered - Record will now be DELETED"
Me.Validated_OK_Label.Visible = False
Run cmdDeleteRecord_click
Else

MsgBox "VALIDATE OK"
Me.Validated_OK_Label.Visible = True
DoCmd.RunMacro "Validate Macro"
End If
End Sub
still not working!
instead of Run cmdDeleteRecord_click try Run DeleteRecord_click
can you post your code for DeleteRecord_click

Powerhousecomputingare you reading my posts http:#a22776337http:#a22776466
i have tried all of the above

post the codes for the Delete record button
can I use some code instead to delete current record?
Private Sub Validate_Click()
If DCount("[first name]", "Audition Applications", "[first name]=" & Chr(34) & Me.[first name] & Chr(34) & " and [surname]=" & Chr(34) & Me.[Surname] & Chr(34) & " and [date of birth]=#" & Me.[date of birth] & "#") > 0 Then

MsgBox "Applicant Already Entered - You will now be asked if you wish to DELETE the record - ONLY DELETE IF THIS IS A NEW RECORD"
Me.Validated_OK_Label.Visible = False
DeleteRecord_click
Else

MsgBox "VALIDATE OK"
Me.Validated_OK_Label.Visible = True
DoCmd.RunMacro "Validate Macro"
End If
End Sub
Are you working with a Form/Subform setup? If so, is the button you're trying to "click" on the SAME FORM as your Validate button?
The button is on the same form - it is called DeleteRecord and is a system button
WHY CAN'T you post the codes for the DELETE RECORD BUTTON?

change

private deleterecord_click()

to

PUBLIC deleterecord_click()


with the codes you posted

DELETE IF THIS IS A NEW RECORD"
Me.Validated_OK_Label.Visible = False

DeleteRecord_click  '<<< the word click would have been changed to Proper case  Click if the button name is correct


Else
the Delete record button us an embedded macro apparently!

yes the button name is correct
as you can see this line of code is part of some other code
You need to look in the properties of the button for the macro it runs.
The Macro is an embedded one and is called:  Audition Appilcations:DeleteRecord:On Click
you have to *CONVERT* that embedded macro to vba to be able to access it from the click of another button.
aha!  How do I do that?
ok - I did a Save As

So now presumably I write:

DoCmd.RunMacro (Delete_Record_Macro)

?
or you can try this


docmd.runmacro " Audition Appilcations:DeleteRecord:On Click"




or could be there are some typo error




docmd.runmacro " Audition Applications:DeleteRecord:On Click"







sorry I don't get it now - I have just called the Macro Delete Record Macro which is what is in the control source so surely I have to call it that now on the code?
wait a while...
in the design view of the form, select the delete button hit F4
select the event tab, see what is written on the  On Click

post it here

Delete Record Macro
ok try this


docmd.runmacro "Delete Record Macro"
Yay - it worked!  Not sure why it didn't a minute ago!

Is it possible for it to ask IF I want to delete the rercord first?
try instead of DeleteRecord_click

DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdDeleteRecord
(Is it possible for it to ask IF I want to delete the rercord first?)

just put a msgbox before the  docmd.runmacro "Delete Record Macro" line


if (msgbox("Do you want to delete the record?",vbyesno))=vbyes then

docmd.runmacro "Delete Record Macro"
else

msgbox "Deletion cancelled"

end if
mmm... not sure how that will work - my code is now:

Private Sub Validate_Click()
If DCount("[first name]", "Audition Applications", "[first name]=" & Chr(34) & Me.[first name] & Chr(34) & " and [surname]=" & Chr(34) & Me.[Surname] & Chr(34) & " and [date of birth]=#" & Me.[date of birth] & "#") > 0 Then

MsgBox "Applicant Already Entered - You will now be asked if you wish to DELETE the record - ONLY DELETE IF THIS IS A NEW RECORD"
Me.Validated_OK_Label.Visible = False
If (MsgBox("Are you sure you want to DELETE this record?", vbYesNo)) = vbYes Then
DoCmd.RunMacro "Delete Record Macro"
Else

MsgBox "VALIDATE OK"
Me.Validated_OK_Label.Visible = True
DoCmd.RunMacro "Validate Macro"
End If
End If
End Sub

What is supposed to happen is if it does not validate then it asks if you want to delete, if it does validate than it says OK etc.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good point - that's me trying to be clever!  Thank so much!