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?
Hi Powerhousecomputing,
Add
Run cmdDelete_Click
to the end of your code, where CmdDelete is the name of your delete button
Good Luck!
Gary
Add
Run cmdDelete_Click
to the end of your code, where CmdDelete is the name of your delete button
Good Luck!
Gary
ASKER
It doesn't like that coding - my Delete buitton is called DeleteRecord
ASKER
Run cmdDeleteRecord_click
doesn't like it!
doesn't like it!
private sub OtherButton_click()
DeleteRecord_click
end sub
ASKER
Is it possible for it to ask IF I want to delete the rercord first?
ASKER
still does not like Run cmdDeleteRecord_click
Powerhousecomputing,
why don't you try this
DeleteRecord_click
why don't you try this
DeleteRecord_click
ASKER
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.Visi ble = False
Run cmdDeleteRecord_click
Else
MsgBox "VALIDATE OK"
Me.Validated_OK_Label.Visi ble = True
DoCmd.RunMacro "Validate Macro"
End If
End Sub
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.Visi
Run cmdDeleteRecord_click
Else
MsgBox "VALIDATE OK"
Me.Validated_OK_Label.Visi
DoCmd.RunMacro "Validate Macro"
End If
End Sub
ASKER
still not working!
instead of Run cmdDeleteRecord_click try Run DeleteRecord_click
ASKER
nope!
can you post your code for DeleteRecord_click
ASKER
i have tried all of the above
post the codes for the Delete record button
ASKER
can I use some code instead to delete current record?
ASKER
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.Visi ble = False
DeleteRecord_click
Else
MsgBox "VALIDATE OK"
Me.Validated_OK_Label.Visi ble = True
DoCmd.RunMacro "Validate Macro"
End If
End Sub
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.Visi
DeleteRecord_click
Else
MsgBox "VALIDATE OK"
Me.Validated_OK_Label.Visi
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?
ASKER
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()
change
private deleterecord_click()
to
PUBLIC deleterecord_click()
with the codes you posted
DELETE IF THIS IS A NEW RECORD"
Me.Validated_OK_Label.Visi
DeleteRecord_click '<<< the word click would have been changed to Proper case Click if the button name is correct
Else
ASKER
the Delete record button us an embedded macro apparently!
yes the button name is correct
yes the button name is correct
ASKER
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.
ASKER
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.
ASKER
aha! How do I do that?
ASKER
ok - I did a Save As
So now presumably I write:
DoCmd.RunMacro (Delete_Record_Macro)
?
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"
docmd.runmacro " Audition Appilcations:DeleteRecord:
or could be there are some typo error
docmd.runmacro " Audition Applications:DeleteRecord:
ASKER
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
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
ASKER
Delete Record Macro
ok try this
docmd.runmacro "Delete Record Macro"
docmd.runmacro "Delete Record Macro"
ASKER
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?
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
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
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
ASKER
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.Visi ble = 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.Visi ble = 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.
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.Visi
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.Visi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good point - that's me trying to be clever! Thank so much!
private sub OtherButton_click()
cmdDeleteButton_click
end sub
cmdDeleteButton is the name of the delete record button