Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jan 2002
    Posts
    1,615
    Thanks
    0
    Thanked 1 Time in 1 Post

    Way to run a command from a command (2002)

    Hi,

    I need to know if there is a way to run a command from another command. I have a form where the user clicks cmdClose to exit the form. The are prompted as to whehter they want to save the form. If they select Yes I'd like the code that is in the onclick event for cmdsave to run. Is that possible?

    Thanks,
    Leesha

  2. #2
    Silver Lounger
    Join Date
    Jan 2002
    Posts
    1,615
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Way to run a command from a command (2002)

    Hi Jerry!

    I have the message box all set and it runs fine. What I'd like it to do is not simply "save" the record but also run the code that is on the click event the cmdSave control. There is a lot of code that searches the form to be sure everything is correct before the record is saved. My goal is to be sure the user doesn't close the form without saving the record. I'm trying to avoid have to retest the code behind another command button and wondered if there was a way to run a command button from another one?
    Thanks!
    Leesha

  3. #3
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Way to run a command from a command (2002)

    <P ID="edit" class=small>(Edited by Jezza on 11-May-07 15:38. To put docmd.close in code)</P>Hi Leesha

    Look up Msgbox function in help, it is similar to this
    <pre>Private Sub cmdClose_Click()

    Dim meSave As Integer

    meSave = MsgBox("Do you wish to Close?", vbYesNo, "Close Form?")

    If meSave = vbYes Then
    DoCmd.Save
    DoCmd.Close
    Else
    ' Do Nothing and close
    DoCmd.Close
    End If
    End Sub
    </pre>


    Is this what you need (I haven't checked the code as I wrote it on the fly)
    Jerry

  4. #4
    4 Star Lounger SteveH's Avatar
    Join Date
    Jan 2001
    Location
    Chelsea, Gtr London, United Kingdom
    Posts
    587
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Way to run a command from a command (2002)

    Call cmdSave_Click

    in your code for the cmdClose button should do the trick.
    Steve H
    IT Lecturer/Access Developer
    O2K SR3/O2010; Win7Pro

  5. #5
    Silver Lounger
    Join Date
    Jan 2002
    Posts
    1,615
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Way to run a command from a command (2002)

    That worked great! Now, I'd like to have it only call the command if the form hasn't been saved before the user tries to exit the form. I tried the following code. It "seems" to work fine if the record wasn't saved however if the record was saved, nothing happens.

    If Me.Dirty Then
    If MsgBox("Do you wish to save this record?", _
    vbYesNo + vbQuestion) = vbYes Then
    Call cmdSave_Click
    Exit Sub

    Else: DoCmd.Close
    End If
    End If

  6. #6
    4 Star Lounger
    Join Date
    Dec 2002
    Location
    Kitchener, Ontario, Canada
    Posts
    421
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Way to run a command from a command (2002)

    The code will only run if the record is dirty. If the record is already saved, then by definition the code won't run since Me isn't dirty. Try moving your Else after the first End If. Or if you want them to close if they choose no for the message box, add another Else: Docmd.Close in between the 2 end Ifs. Right now, your outside If clause is saying do nothing if Me isn't dirty.

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Way to run a command from a command (2002)

    Leesha,

    The preferred way of doing this is to put the code you need to run in a separate routine and then call it from both the button click event and wherever else you need to call it., although It isn't illegal to call a button click routine directly. In response to your remark, if the record has been saved, it won't be dirty and if it isn't dirty, it won't be saved again regardless.
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •