Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancel Button

Status
Not open for further replies.

jdwm2310

Technical User
Jul 26, 2001
396
US
Hi Guys,

I would like to create a cancel button not a close button. Where the user clicks on cancel from the dialog box and it will take them back to the form.

Any suggestions will be greatly appreciated..thanks
 
I'm assuming you don't want the dialog box form to close when the user presses the cancel button. If so, you can setfocus to the form and, optionally, change the visible property of the dialog box. For example,

me.visible = false
Forms!frmMain.SetFocus
 
I don't know what to write in the vbCancel line, please look at my code hopefully you can help me..thanks

[tt]Private Sub Save_Click()
On Error GoTo Err_Save_Click

If MsgBox("Would like to close this ticket now?", vbYesNoCancel + vbQuestion) = vbYes Then

If IsNull(Me.Answer_Comments) Or IsNull(Me.RequestedQuestion) Then
MsgBox "You must provide a question and an answer before closing the ticket"
GoTo Exit_Save_Click
End If
End If
If IsNull(Me.Call_Source) Then
MsgBox "You must provide a Call Source"
Me.Call_Source.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.Area) Then
MsgBox "You must provide an Area"
Me.Area.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.Caller_Status) Then
MsgBox "You must provide a Caller Status"
Me.Caller_Status.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.CallerName) Then
MsgBox "You must provide a Caller Name"
Me.CallerName.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.CostCenter) Then
MsgBox "You must provide a Cost Center"
Me.CostCenter.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.EMPID) Then
MsgBox "You must provide a Employee ID"
Me.EMPID.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.JobTitle) Then
MsgBox "You must provide a Job Title"
Me.JobTitle.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.Telephone) Then
MsgBox "You must provide a Telephone Number"
Me.Telephone.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If
If IsNull(Me.CompanyName) Then
MsgBox "You must provide a Company Name"
Me.CompanyName.BackColor = RGB(255, 255, 0)
GoTo Exit_Save_Click
End If


Me.Date_Closed = Now()
Me.TicketStatus = "Closed"
Me.Lock = True

DoCmd.Close acForm, "HelpDeskCalls", acSaveYes
DoCmd.OpenForm "TicketOption"
MsgBox "The ticket is saved", vbInformation

If vbNo Then
MsgBox "The status of this ticket is Pending, please return to this ticket later.", vbInformation

Me.TicketStatus = "Pending"
DoCmd.Close acForm, "HelpDeskCalls", acSaveYes
DoCmd.OpenForm "TicketOption"
MsgBox "The ticket is saved", vbInformation
End If

If vbCancel Then


Exit_Save_Click:
Exit Sub

Err_Save_Click:
MsgBox Err.Description
Resume Exit_Save_Click
End Sub[/tt]
 
I reformatted your code so I could visually see what was going on. I think this is what you want. However, look at the second example I provided. What it does is display all of the fields the user failed to enter and turns them all red. Nothing is more frustrating then to press go and the program tells you that you failed to enter a value in a field. So you fill it in, then press go again and it tells you of another field you failed to enter. An so on. The second example does it all at once. I think the users will like it better.

But, here's version 1
Code:
Private Sub Save_Click()

'********************************
'*  Declaration Specifications  *
'********************************

    Dim varResponse As Variant

    On Error GoTo Err_Save_Click

'*********************
'*  Display message  *
'*********************

    varResponse = MsgBox("Would like to close this ticket now?", vbYesNoCancel + vbQuestion)
    
    If (varResponse = vbYes) Then
        
        If IsNull(Me.Answer_Comments) Or IsNull(Me.RequestedQuestion) Then
            MsgBox "You must provide a question and an answer before closing the ticket"
            GoTo Exit_Save_Click
        End If

        
        If IsNull(Me.Call_Source) Then
            MsgBox "You must provide a Call Source"
            Me.Call_Source.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.Area) Then
            MsgBox "You must provide an Area"
            Me.Area.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.Caller_Status) Then
            MsgBox "You must provide a Caller Status"
            Me.Caller_Status.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.CallerName) Then
            MsgBox "You must provide a Caller Name"
            Me.CallerName.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.CostCenter) Then
            MsgBox "You must provide a Cost Center"
            Me.CostCenter.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.EMPID) Then
            MsgBox "You must provide a Employee ID"
            Me.EMPID.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.JobTitle) Then
            MsgBox "You must provide a Job Title"
            Me.JobTitle.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.Telephone) Then
            MsgBox "You must provide a Telephone Number"
            Me.Telephone.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
            
        If IsNull(Me.CompanyName) Then
            MsgBox "You must provide a Company Name"
            Me.CompanyName.BackColor = RGB(255, 255, 0)
            GoTo Exit_Save_Click
        End If
        
            
        Me.Date_Closed = Now()
        Me.TicketStatus = "Closed"
        Me.Lock = True
            
        DoCmd.Close acForm, "HelpDeskCalls", acSaveYes
        DoCmd.OpenForm "TicketOption"
        MsgBox "The ticket is saved", vbInformation
        
    ElseIf (varResponse = vbNo) Then
    
        MsgBox "The status of this ticket is Pending, please return to this ticket later.", vbInformation
                
        Me.TicketStatus = "Pending"
        DoCmd.Close acForm, "HelpDeskCalls", acSaveYes
        DoCmd.OpenForm "TicketOption"
        MsgBox "The ticket is saved", vbInformation
    
        
    ElseIf (varResponse = vbCancel) Then
        
        Forms!frmMain.SetFocus
        
    End If
    
'********************
'*  Exit Procedure  *
'********************

Exit_Save_Click:
    
    Exit Sub
    
'****************************
'*  Error Recovery Section  *
'****************************

Err_Save_Click:
    
    MsgBox Err.Description
    Resume Exit_Save_Click

End Sub

Here's version 2
Code:
Private Sub Save_Click()

'********************************
'*  Declaration Specifications  *
'********************************

    Dim strMsg As String
    Dim varResponse As Variant

    On Error GoTo Err_Save_Click

'************************************
'*  Assume everything is filled in  *
'************************************

    strMsg = vbNullString
    
    Me.Call_Source.BackColor = 0            'Background color of 0
    Me.Area.BackColor = 0
    Me.Caller_Status.BackColor = 0
    Me.CallerName.BackColor = 0
    Me.CostCenter.BackColor = 0
    Me.EMPID.BackColor = 0
    Me.JobTitle.BackColor = 0
    Me.Telephone.BackColor = 0
    Me.CompanyName.BackColor = 0
    
'*********************
'*  Display message  *
'*********************

    varResponse = MsgBox("Would like to close this ticket now?", vbYesNoCancel + vbQuestion)
    
    If (varResponse = vbYes) Then
        
        If IsNull(Me.Answer_Comments) Or IsNull(Me.RequestedQuestion) Then
            MsgBox "You must provide a question and an answer before closing the ticket"
            GoTo Exit_Save_Click
        End If

        
        If IsNull(Me.Call_Source) Then
            strMsg = "You must provide a Call Source" & vbCrLf
            Me.Call_Source.BackColor = 255
        End If
            
        If IsNull(Me.Area) Then
            strMsg = strMsg & "You must provide an Area" & vbCrLf
            Me.Area.BackColor = 255
        End If
            
        If IsNull(Me.Caller_Status) Then
            strMsg = strMsg & "You must provide a Caller Status" & vbCrLf
            Me.Caller_Status.BackColor = 255
        End If
            
        If IsNull(Me.CallerName) Then
            strMsg = strMsg & "You must provide a Caller Name" & vbCrLf
            Me.CallerName.BackColor = 255
        End If
            
        If IsNull(Me.CostCenter) Then
            strMsg = strMsg & "You must provide a Cost Center" & vbCrLf
            Me.CostCenter.BackColor = 255
        End If
            
        If IsNull(Me.EMPID) Then
            strMsg = strMsg & "You must provide a Employee ID" & vbCrLf
            Me.EMPID.BackColor = 255
        End If
            
        If IsNull(Me.JobTitle) Then
            strMsg = strMsg & "You must provide a Job Title" & vbCrLf
            Me.JobTitle.BackColor = 255
        End If
            
        If IsNull(Me.Telephone) Then
            strMsg = strMsg & "You must provide a Telephone Number" & vbCrLf
            Me.Telephone.BackColor = 255
        End If
            
        If IsNull(Me.CompanyName) Then
            strMsg = strMsg & "You must provide a Company Name" & vbCrLf
            Me.CompanyName.BackColor = 255
        End If
        
        If (strMsg <> vbNullString) Then        'IF True, then incomplete
            MsgBox Mid(strMsg, 1, Len(strMsg) - 2) 'Get rid of last vbcrlf
            GoTo Exit_Save_Click
        en dif
        
        Me.Date_Closed = Now()
        Me.TicketStatus = &quot;Closed&quot;
        Me.Lock = True
            
        DoCmd.Close acForm, &quot;HelpDeskCalls&quot;, acSaveYes
        DoCmd.OpenForm &quot;TicketOption&quot;
        MsgBox &quot;The ticket is saved&quot;, vbInformation
        
    ElseIf (varResponse = vbNo) Then
    
        MsgBox &quot;The status of this ticket is Pending, please return to this ticket later.&quot;, vbInformation
                
        Me.TicketStatus = &quot;Pending&quot;
        DoCmd.Close acForm, &quot;HelpDeskCalls&quot;, acSaveYes
        DoCmd.OpenForm &quot;TicketOption&quot;
        MsgBox &quot;The ticket is saved&quot;, vbInformation
    
        
    ElseIf (varResponse = vbCancel) Then
        
        Forms!frmMain.SetFocus
        
    End If
    
'********************
'*  Exit Procedure  *
'********************

Exit_Save_Click:
    
    Exit Sub
    
'****************************
'*  Error Recovery Section  *
'****************************

Err_Save_Click:
    
    MsgBox Err.Description
    Resume Exit_Save_Click

End Sub
 
I used the second version, I tried testing it by leaving some fields blank when I click on the button all the fields including those that had entries turn black, I wasn't able to select from my comboboxs..Any idea why? I paste it just as you posted...
 
Well, I don't know what your default background color is for the fields. I chose black (0). What you need to do is look at the property sheet of one of your fields and look at the background color for the field. Then replace my 0's with that number (may be a large number). For example,

Me.Area.BackColor = 125739288

It looked to me like your orginal code would never have worked as printed. You did something like this:

If (msgbox(....) = vbYes) then
end if

The following line of your code would not work. That's why I put the varResponse = Msgbox(...) statement and the If...ElseIF...ElseIF...EndIF statements in.

if (vbno) then
end if

The code I wrote should not have affected your combo boxes, I just combined some stuff. However, I might have missed somthing trying to figure out your if/then else statements. Therefore, I suggest you use debug to step thru your code. If you don't know how to use debug, then here's a quick lesson to get you started.

Type the word Stop above the line of code you want to examine. Or place the cursor on the line and toggle the breakpoint on/off. Then execute your code. Your program will pause executing when it encounters the Stop statement or the breakpoint line. Once paused, place your cursor on the field you want to examine, a tooltip will be displayed showing you the value of that field. Or you can type, in the debuggers Immediate window, ?NameOfYourFieldToEvaluate.
Press F8 to execute your code one line at a time. Press F5 to execute your code till it finds the next stop statement, breakpoint or reaches the end of the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top