Solved

VBA Code to Click Button

Posted on 2008-10-22
40
1,121 Views
Last Modified: 2010-08-05
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?
0
Comment
Question by:Powerhousecomputing
  • 21
  • 13
  • 5
  • +1
40 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22776265
in the other button

private sub OtherButton_click()
cmdDeleteButton_click
end sub


cmdDeleteButton is the name of the delete record button
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 22776301
Hi Powerhousecomputing,

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


Good Luck!

Gary
0
 

Author Comment

by:Powerhousecomputing
ID: 22776306
It doesn't like that coding - my Delete buitton is called DeleteRecord
0
 

Author Comment

by:Powerhousecomputing
ID: 22776312
Run cmdDeleteRecord_click

doesn't like it!
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22776337



private sub OtherButton_click()
DeleteRecord_click
end sub
0
 

Author Comment

by:Powerhousecomputing
ID: 22776366
Is it possible for it to ask IF I want to delete the rercord first?
0
 

Author Comment

by:Powerhousecomputing
ID: 22776413
still does not like Run cmdDeleteRecord_click
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22776466
Powerhousecomputing,

why don't you try this


           DeleteRecord_click
0
 

Author Comment

by:Powerhousecomputing
ID: 22776467
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
0
 

Author Comment

by:Powerhousecomputing
ID: 22776488
still not working!
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 22776515
instead of Run cmdDeleteRecord_click try Run DeleteRecord_click
0
 

Author Comment

by:Powerhousecomputing
ID: 22776553
nope!
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 22776601
can you post your code for DeleteRecord_click
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22776615

Powerhousecomputingare you reading my posts http:#a22776337http:#a22776466
0
 

Author Comment

by:Powerhousecomputing
ID: 22776631
i have tried all of the above
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22776691

post the codes for the Delete record button
0
 

Author Comment

by:Powerhousecomputing
ID: 22776698
can I use some code instead to delete current record?
0
 

Author Comment

by:Powerhousecomputing
ID: 22776711
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
0
 
LVL 84
ID: 22776715
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?
0
 

Author Comment

by:Powerhousecomputing
ID: 22776738
The button is on the same form - it is called DeleteRecord and is a system button
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22776753
WHY CAN'T you post the codes for the DELETE RECORD BUTTON?

change

private deleterecord_click()

to

PUBLIC deleterecord_click()
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22776780


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
0
 

Author Comment

by:Powerhousecomputing
ID: 22776896
the Delete record button us an embedded macro apparently!

yes the button name is correct
0
 

Author Comment

by:Powerhousecomputing
ID: 22776906
as you can see this line of code is part of some other code
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 22776931
You need to look in the properties of the button for the macro it runs.
0
 

Author Comment

by:Powerhousecomputing
ID: 22776968
The Macro is an embedded one and is called:  Audition Appilcations:DeleteRecord:On Click
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22777006
you have to *CONVERT* that embedded macro to vba to be able to access it from the click of another button.
0
 

Author Comment

by:Powerhousecomputing
ID: 22777019
aha!  How do I do that?
0
 

Author Comment

by:Powerhousecomputing
ID: 22777054
ok - I did a Save As

So now presumably I write:

DoCmd.RunMacro (Delete_Record_Macro)

?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22777063
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"







0
 

Author Comment

by:Powerhousecomputing
ID: 22777085
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?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22777178
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

0
 

Author Comment

by:Powerhousecomputing
ID: 22777301
Delete Record Macro
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22777362
ok try this


docmd.runmacro "Delete Record Macro"
0
 

Author Comment

by:Powerhousecomputing
ID: 22777389
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?
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 22777390
try instead of DeleteRecord_click

DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdDeleteRecord
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 22777456
(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
0
 

Author Comment

by:Powerhousecomputing
ID: 22777514
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.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 22777582
Powerhousecomputing,

when you insert lines of codes, make sure you don't omit anything
0
 

Author Comment

by:Powerhousecomputing
ID: 22777637
Good point - that's me trying to be clever!  Thank so much!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now