Need Help With Ok/Cancel Dialog

Sausagefingers

Registered User.
Local time
Today, 19:13
Joined
Dec 14, 2006
Messages
51
Hi,
I have a module with which I would like to offer the user an option to cancel their action using a dialog box. Being new to VB, I'm not quite sure how to implement this.

Here is the content of the module so far:

'------------------------------------------------------------
' isGroupedOnClick
'
'------------------------------------------------------------
Function isGroupedOnClick()
On Error GoTo isGroupedOnClick_Err

If (Forms!frm_deliveryTransactions!isGrouped = True) Then
Beep
MsgBox "Are You Sure You Wish To Make This Change?", vbQuestion, "Alterations To Route Structure Will Result..."
Forms!frm_deliveryTransactions!isGrouped_Label.ForeColor = RGB(200, 0, 0)

End If
If (Forms!frm_deliveryTransactions!isGrouped = False) Then
Forms!frm_deliveryTransactions!isGrouped_Label.ForeColor = RGB(0, 0, 0)
End If


isGroupedOnClick_Exit:
Exit Function

isGroupedOnClick_Err:
MsgBox Error$
Resume isGroupedOnClick_Exit

End Function
------------------

Can somebody please offer some advice/code. Specifically, where will I insert it within the module.

TIA.
 
This is what I use for asking wether or not to print out specific documents that forms part of a pack that ends up being aroun 80 pages, hence the option to stop printing certain documents if needed.

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to print the cover?"
Style = vbYesNo + None + vbDefaultButton2
Title = "Service Users Guide"
Help = "DEMO.HLP"
Ctxt = 1000
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
Set objWord = CreateObject("Word.Application")
objWord.Visible = True 'True is visible
objWord.Documents.Add ("F:\service users guide\cover.doc")
objWord.PrintOut
objWord.ActiveDocument.Close (wdDoNotSaveChanges)
objWord.Quit
Set objWord = Nothing
Else ' User chose No.
End If

I think there might be a better way, but this sure works for me, and I am a complete novice.
 
Thanks for your help.
So far so good. I now have a dialog box that displays Yes/No buttons triggered by the OnClick event. The message and the title are correct. The problem I'm having now is applying the logic to the Yes/No scenario.

Assuming the Forms checkbox was clicked in error and the event triggered, then the 'No' button should ideally undo the OnClick event and reset the checkbox to it's original state.

Alternatively, if the user wishes to continue and confirm the action the 'Yes' button should (presumably) do nothing?

Currently modified module code:

'------------------------------------------------------------
' isGroupedOnClick
'
'------------------------------------------------------------
Function isGroupedOnClick()
On Error GoTo isGroupedOnClick_Err

If (Forms!frm_deliveryTransactions!isGrouped = True) Then
Beep
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Are You Sure You Wish To Make This Change?"
Style = vbYesNo + None + vbDefaultButton2
Title = "Alterations To Route Structure Will Result..."
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.

Else ' User chose No.
End If

Forms!frm_deliveryTransactions!isGrouped_Label.ForeColor = RGB(200, 0, 0)

End If
If (Forms!frm_deliveryTransactions!isGrouped = False) Then
Forms!frm_deliveryTransactions!isGrouped_Label.ForeColor = RGB(0, 0, 0)
End If


isGroupedOnClick_Exit:
Exit Function

isGroupedOnClick_Err:
MsgBox Error$
Resume isGroupedOnClick_Exit

End Function

------------

I need some help with the 'If Response = vbYes Then' and the 'Else' statement. Not knowing much VB, I am stuck for the solution. Can you help?

TIA
 
Hi,

I guess you want this to be OnClick event of the checkbox. Event handlers are usually subroutines. Is there a reason here, why you use a function instead?
If it was my project, I would use something like this code:

Code:
Sub isGroupedOnClick()
   Dim Msg as String, Title as String
   Dim Response as Integer

   On Error GoTo isGroupedOnClick_Err

   If Me.isGrouped.Value = True Then
      Beep
      Me.isGrouped_Label.ForeColor = RGB(200, 0, 0)         'I suppose this color change is a warning, so it should be done before the question is asked.
      Msg = "Are You Sure You Wish To Make This Change?"
      Title = "Alterations To Route Structure Will Result..."
      Response = MsgBox(Msg, vbYesNo, Title)
      If Response = vbYes Then           'User chose Yes.
            'Call the sub that makes the change
      Else                                         'User chose No.
            Me.isGrouped.Value = False    'Reset checkbox
      End If
      Me.isGrouped_Label.ForeColor = RGB(0, 0, 0)         'Reset label color, whatever the user chose

      isGroupedOnClick_Exit:
      Exit Function

      isGroupedOnClick_Err:
      MsgBox Error$
      Resume isGroupedOnClick_Exit

End Sub
 
Jimmy the Hand said:
Hi,

I guess you want this to be OnClick event of the checkbox. Event handlers are usually subroutines. Is there a reason here, why you use a function instead?

Hi and thanks for the response. You are right, this is to be the OnClick event for the checkbox.

There is no reason - except for a general lack of VB knowledge - that I used a module instead of a subroutine. I have taken your advice though and used the sub as you have suggested.

For the most part the subroutine is working fine although I am now faced with the task of implementing the 'reverse' of the code you have supplied.

When a user clicks on the checkbox which already has a 'True' value (and therefore un-checking the box), I would like the same messagebox to appear with the same options as before.

I'm assuming this will be part of the same subroutine?

Any help would be appreciated and please excuse my apalling level of competence here :o
 
If you use the AfterUpdate event of the checkbox then you can use the value of the checkbox to check whether it is true or false. If you use the click event then it runs before it has been updated so even though you click it and it looks like it is true, it is actually false until the update has occurred.

So, for simplicity's sake, you should use the Checkbox's afterupdate event and then you can just use the
Code:
If Me.CheckboxName = True Then
     ... do whatever if true here
Else
     ...do whatever if false here
End if
 
boblarson said:
If you use the AfterUpdate event of the checkbox then you can use the value of the checkbox to check whether it is true or false. If you use the click event then it runs before it has been updated so even though you click it and it looks like it is true, it is actually false until the update has occurred.

So, for simplicity's sake, you should use the Checkbox's afterupdate event and then you can just use the
Code:
If Me.CheckboxName = True Then
     ... do whatever if true here
Else
     ...do whatever if false here
End if

Excellent, thanks. This seems to make much more sense. So much so that this here novice has been able to implement the event procedure albeit with code snippets provided by all respondees so far.

New Code:

'------------------------------------------------------------
' isGroupedAfterUpdate
'
'------------------------------------------------------------
Private Sub isGrouped_AfterUpdate()
If Me.isGrouped = True Then
Me.isGrouped_Label.ForeColor = RGB(200, 0, 0)
Msg = "Are You Sure You Wish To Make This Change?"
Title = "Alterations To Route Structure Will Result..."
Response = MsgBox(Msg, vbYesNo, Title)
If Response = vbYes Then 'User chose Yes.
'Call the sub that makes the change
Else 'User chose No.
Me.isGrouped.Value = False 'Reset checkbox
End If

Else
Me.isGrouped_Label.ForeColor = RGB(200, 0, 0)
Msg = "Are You Sure You Wish To Make This Change?"
Title = "Alterations To Route Structure Will Result..."
Response = MsgBox(Msg, vbYesNo, Title)
If Response = vbYes Then 'User chose Yes.
'Call the sub that makes the change
Else 'User chose No.
Me.isGrouped.Value = True 'Reset checkbox
End If

End If
End Sub
-----------
Does this look correct? If so, thanks to all for the solution. :)
 
If you are using the AfterUpdate event of the checkbox, then this part of your code:
Code:
Me.isGrouped.Value = False 'Reset checkbox

and the other part

Me.isGrouped.Value = True 'Reset checkbox
will wind up putting you into an infinite loop because you will end up changing the checkbox again after it had updated and then because it has changed it will run the code again and set the value of the checkbox and then it will update because it has updated, and so on, and so on. Why are you setting the value of the check box back to whatever the previous value was anyway?
 
Hi Bob,
I will try to explain as best I can the 'logic' behind the isGrouped checkbox on the form.

The project is - essentially - a Transport Database. Part of its function (this part) is to log the arrival/standing times of a fleet of delivery vehicles on a daily basis. Our company delivers (daily) on 28 different routes comprising of approximately 280 'drops' or branches. However, not all of the branches are unique drops. In many cases, a number of branches will share the same destination. In other words, one drop may contain 3, 4 or even 5 branches (others may be stand-alone).

As and when this occurrs the same arrival and standing time is required for multiple branches. Or, as my manager has stipulated :rolleyes: the first branch in any instance of the above is the ONLY branch that arrival/standing time is required the others may be ignored (phew). That decision is taken purely on the basis of the number of keystrokes and is understandable IMO.

To complicate matters a little, the routes may change slightly from time to time. One 'grouped' branch may require a delivery on Tuesday but not on Thursday etc. Flexibility is somewhat important here, so the checkbox indicates to the operator that branch 'B' and 'C' is grouped or concatenated to branch 'A' most of the time so they require no daily input of arrival/standing time yet, offers the operator the option to drop branches from their 'grouped' status if required, in order to add arrival/standing times if neccessary.

Of course, I could just have set the checkbox to locked but then that extra, critical functionality would have been lost, the operator would no longer have been able to make those minor but important changes to the structure of the delivery route (phew).

Hope this goes some way to anwering your question :)
 
Hi,

I can't say I understood your last post with the explanation, despite I think I can contribute to your success.
What Bob said about event order is true, still I think the OnClick event will be perfect here, because changing the value of the checkbox via VBA code doesn't fire another OnClick event, so infinite loop is not an issue. Also, you can use the present state of the checkbox, once you know that it's the before update state. I'll show you what I mean.

Code:
Sub isGroupedOnClick()
   Dim Msg as String, Title as String
   Dim Response as Integer

   On Error GoTo isGroupedOnClick_Err
'Display the warning message here, before checking state of the box.
'Reason: Alteration [B]will[/B] result, anyway, no matter what the state of the checkbox was

   Msg = "Are You Sure You Wish To Make This Change?"
   Title = "Alterations To Route Structure Will Result..."
   Beep
   Me.isGrouped_Label.ForeColor = RGB(200, 0, 0)         
   Response = MsgBox(Msg, vbYesNo, Title)

'Do the checking now
   If Response = vbYes then      'the alteration was confirmed by user
      If Me.isGrouped.Value = False Then   'The checkbox had not been checked so far, so the Click is aimed to [B]make[/B] the change
          'Use the code that [B]makes[/B] the change
      Else              'Checkbox had been checked so far, which means that the Click was aimed to [B]reverse[/B] the change.
          'Use the code that [B]reverses[/B] the change
      End If
   Else                              'alteration was not confirmed by user, so checkbox state needs to be reset
      Me.isGrouped.Value = - (Me.isGrouped.Value + 1)   'This reverses the checkbox, whatever it was.
   End If  
  Me.isGrouped_Label.ForeColor = RGB(0, 0, 0)         'Reset label color after actions done 

   isGroupedOnClick_Exit:
   Exit Function

   isGroupedOnClick_Err:
   MsgBox Error$
   Resume isGroupedOnClick_Exit
End Sub

I think this will work. I certainly hope so.
 

Users who are viewing this thread

Back
Top Bottom