Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2012
    Posts
    126

    Unanswered: Need vb guidance on a nested IF

    Here is my current code (i know it won't work currently)

    Private Sub cmdClosefrmAddNewItems_Click()
    On Error GoTo err_cmdClosefrmAddNewItems_Click
    If MsgBox("Did you update data?", vbYesNoCancel, "Close New Items Form") = vbYes Then

    DoCmd.Close acForm, frmaddnewitems
    DoCmd.Close acForm, frmproducts

    Else
    vbNo MsgBox("Are you sure you want to close without updating?", vbYesNo, "Close without Saving") = vbYes

    End If

    Exit_cmdClosefrmAddNewItems_Click:
    Exit Sub
    err_cmdClosefrmAddNewItems_Click:
    MsgBox Err.Description
    Resume Exit_cmdClosefrmAddNewItems_Click
    End Sub

    I want a message box that if yes it closes the two forms if no it pops another message box confirming they want to close without updating the information (theres an append query behind the code). If they say yes then close if not end If. Also not sure how to handle the cancel argument of the first msgbox. Yes = close forms, No = msgbox that says are you sure yes/no, and cancel = ends code.

  2. #2
    Join Date
    Aug 2012
    Posts
    126
    I think im getting close

    Private Sub cmdClosefrmAddNewItems_Click()
    On Error GoTo err_cmdClosefrmAddNewItems_Click
    If MsgBox("Did you update data?", vbYesNo, "Close New Items Form") = vbYes Then

    DoCmd.Close acForm, frmaddnewitems
    DoCmd.Close acForm, frmproducts

    Else
    If MsgBox("Are you sure you want to close without updating?", vbYesNo, "Close without Saving") = vbYes Then
    DoCmd.Close acForm, frmaddnewitems
    DoCmd.Close acForm, frmproducts
    Exit Sub

    End If
    End If

    Exit Sub
    Exit_cmdClosefrmAddNewItems_Click:
    Exit Sub
    err_cmdClosefrmAddNewItems_Click:
    MsgBox Err.Description
    Resume Exit_cmdClosefrmAddNewItems_Click
    End Sub

    I took out the vbyesnocancel and made it a vbyesno.

    When I compile it works but running the from the button it gives error:

    This action requires an object name argument. Which i thought i put in there?

  3. #3
    Join Date
    Sep 2006
    Location
    Surrey, UK
    Posts
    995
    Provided Answers: 2
    Code:
    Private Sub cmdClosefrmAddNewItems_Click()
    On Error GoTo err_cmdClosefrmAddNewItems_Click
    dim intResp As Integer
    
    intResp = MsgBox("Did you update data?", vbYesNoCancel, "Close New Items Form")
    
    If intResp = vbYes Then
    
    DoCmd.Close acForm, frmaddnewitems
    DoCmd.Close acForm, frmproducts
    
    ElseIf intResp = vbCancel Then
    
    Exit Sub
    
    Else
    
    If MsgBox("Are you sure you want to close without updating?", vbYesNo, "Close without Saving") = vbYes Then
    
    DoCmd.Close acForm, frmaddnewitems
    DoCmd.Close acForm, frmproducts
    
    End If
    
    Exit_cmdClosefrmAddNewItems_Click:
    Exit Sub
    err_cmdClosefrmAddNewItems_Click:
    MsgBox Err.Description
    Resume Exit_cmdClosefrmAddNewItems_Click
    End Sub
    10% of magic is knowing something that no-one else does. The rest is misdirection.

  4. #4
    Join Date
    Aug 2012
    Posts
    126
    Thank you for the quick reply! Would I need to add another End If? Like this?

    Private Sub cmdClosefrmAddNewItems_Click()
    On Error GoTo err_cmdClosefrmAddNewItems_Click
    Dim intResp As Integer

    intResp = MsgBox("Did you update data?", vbYesNoCancel, "Close New Items Form")

    If intResp = vbYes Then

    DoCmd.Close acForm, frmaddnewitems
    DoCmd.Close acForm, frmproducts

    ElseIf intResp = vbCancel Then

    Exit Sub

    Else

    If MsgBox("Are you sure you want to close without updating?", vbYesNo, "Close without Saving") = vbYes Then

    DoCmd.Close acForm, frmaddnewitems
    DoCmd.Close acForm, frmproducts

    End If
    End If

    Exit_cmdClosefrmAddNewItems_Click:
    Exit Sub
    err_cmdClosefrmAddNewItems_Click:
    MsgBox Err.Description
    Resume Exit_cmdClosefrmAddNewItems_Click
    End Sub

  5. #5
    Join Date
    Aug 2012
    Posts
    126
    Added the second End if and its working great! Thanks a ton weejas!

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    if you are requesting help with code, it helps if you enclose the code with [ c o d e ] and [ / c o d e ] tags, without the spaces
    eg:-
    Code:
    Private Sub cmdClosefrmAddNewItems_Click()
    On Error GoTo err_cmdClosefrmAddNewItems_Click
    Dim intResp As Integer
    
    intResp = MsgBox("Did you update data?", vbYesNoCancel, "Close New Items Form")
    
    If intResp = vbYes Then
      DoCmd.Close acForm, frmaddnewitems
      DoCmd.Close acForm, frmproducts
    ElseIf intResp = vbCancel Then
      Exit Sub
    Else
      If MsgBox("Are you sure you want to close without updating?", vbYesNo, "Close without Saving") = vbYes Then
        DoCmd.Close acForm, frmaddnewitems
        DoCmd.Close acForm, frmproducts
      End If
    End If
    
    Exit_cmdClosefrmAddNewItems_Click:
    Exit Sub
    err_cmdClosefrmAddNewItems_Click:
    MsgBox Err.Description
    Resume Exit_cmdClosefrmAddNewItems_Click
    End Sub
    mind you you will also find it easier to read your own code if you ident it. in the VB/VBA world thats usually two character is for each loop/if and so on.
    eg
    Code:
    if myvalue = 1 then
      'do something
    elseif anothervalue = 72 then
      'do something else
    else
      if avalue = "a" then
        'do somethign else
      else
        if bvalue = "b" then
          'do somethign else
        endif
      endif
    endif
    also stuff some comments into your code if the meaning isn't totally clear as to why this code should run and on what conditions it should run. there's no point in assigning a comment on every line of code or if its bleeding obvious what is going on (but bear in mind bleeding obvious right now may not be so bleedingly obvious in a year or too for you, or even next week to someone else )

    There are some plugins which will make editing and managing code a bit easier
    tools such as VBA MZ-tools
    Last edited by healdem; 09-24-12 at 13:50.
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Aug 2012
    Posts
    126
    sweet thanks Healdem. I will enclose with code in the future. And I agree makes it much easier to read.

    I do add comments to the code as I implement it. I just thought it would clutter up the forum. I can add them in the future though.

Posting Permissions

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