Rookie Mistake?

tweeter85usn

New member
Local time
Today, 13:08
Joined
Mar 23, 2012
Messages
6
Good Day My Friends,

I have come across a problem. I'm sure it is something simple and I just can't figure it out or find it on the web. It may be that I am trying to ask the wrong thing. Anyway.

Here is the Code I am using.

*Note: PQS1Issue and PQS2Issue are checkboxes'''

Code:
Private Function YouSure()
  If MsgBox("Clearing this checkbox will clear all information under it.", vbYesNo, "Warning") = vbYes Then
    DoCmd.Save
  Else
    Me.Undo
    Me.PQS1Name.Enabled = True
    Me.PQS1DateIssued.Enabled = True
    Me.PQS1DateDue.Enabled = True
    Me.PQS1Name.Undo
    Me.PQS1DateIssued.Undo
    Me.PQS1DateDue.Undo
   End If
End Function
'''''''''''''''''''''''''''''''
Private Function YouSure2()
  If MsgBox("Clearing this checkbox will clear all information under it. Are you sure you want to do this?", vbYesNo, "Are You Sure?") = vbYes Then
    DoCmd.Save
  Else
    Me.Undo
    Me.PQS2Name.Enabled = True
    Me.PQS2DateIssued.Enabled = True
    Me.PQS2DateDue.Enabled = True
    Me.PQS2Name.Undo
    Me.PQS2DateIssued.Undo
    Me.PQS2DateDue.Undo
  End If
End Function
'''''''''''''''''''''''''''''''''''''
Private Sub PQS1Issued_Click()
    If PQS1Issued.Value = 0 Then
    Call YouSure
    Else
    End If
End Sub
Private Sub PQS2Issued_Click()
    If PQS2Issued.Value = 0 Then
    Call YouSure2
    Else
    End If
End Sub

The problem I am having is that when I press no on either PQS1Issue or PQS2Issue, it runs "Undo" on the other one as well. I need to be able to undo the action of which ever I am using at the time and not both. Both are on the same form.

Please let me know if you need more info.
 
Both blocks have ...
Code:
Me.Undo
... and 'Me' is the form. The whole form.
 
I replaced
Code:
Me.Undo
with
Code:
Me.PQS1Issued.Undo
(and the same for PQS2Issued) and now it does not cancell anything. It just clears the individual forms.
 
When an Undo is applied to a specific control, it will only be effective in the BeforeUpdate event of that control. After that (once focus has left the control), the original value in the control can only be restored with an Undo on the form, which would undo ALL control values on the form. If all you want to do is clear the values, then there's no need for an Undo. Just do Me.PQS1Issued.Value = "", etc.

Also, this is unrelated to your issue, but you have YouSure and YouSure2 defined as functions, but are using them as subroutines. Subroutines are generally used to perform an operation. Functions are used to perform an operation or calculation and return the results. Just change "Function" to "Sub" and it will work as intended and be more proper form.

Good luck.
 
well, here is what i would do. combine into one routine-

Code:
Sub YouSure(chkBox As Integer)
Dim lReply As Long

lReply = MsgBox("Clearing this checkbox will clear all information under it." & _
" Are you sure you want to do this?", vbYesNo, "Are You Sure?")

   Select Case lReply
   Case vbYes
      Me.Undo
      Me.PQS & chkBox & Name.Enabled = True
      Me.PQS & chkBox & DateIssued.Enabled = True
      Me.PQS & chkBox & DateDue.Enabled = True
      Me.PQS & chkBox & Name.Undo
      Me.PQS & chkBox & DateIssued.Undo
      Me.PQS & chkBox & DateDue.Undo
      DoCmd.Save
   Case vbNo
      Msgbox ("You said No")
      Exit Sub
   Case vbCancel
      Msgbox ("You Cancelled")
      End Select
'''''''''''''''''''''''''''''''

then the same routine can be called instead of multiples and by using numbers like Call YouSure(2), you could add other check boxes withing adding additional routines. The routine YouSure(chkBox As Integer) will collect the number from the callout and add it to the line Me.PQS & chkBox.Value & Name.Enabled = True

I'll need to check the code when i get home, this is on the fly.....

Call the routine as below...
Code:
Private Sub PQS1Issued_Click()
    If PQS1Issued.Value = 0 Then
    Call YouSure(1)
    Else
    End If
End Sub

Private Sub PQS2Issued_Click()
    If PQS2Issued.Value = 0 Then
     Call YouSure(2)
    Else
    End If
End Sub

Thanks


nigel
 
DoCmd.Save doesn't save the record, it saves the form. To save the record, use:
Code:
DoCmd.RunCommand acCmdSaveRecord
or
Code:
Me.Dirty = False
 
I am getting an error with the following code: "Compile Error: Expected Expression".

Code:
Sub YouSure(chkBox As Integer)
Dim lReply As Long
lReply = MsgBox("Clearing this checkbox will clear all information under it." & _
" Are you sure you want to do this?", vbYesNo, "Are You Sure?")
   Select Case lReply
   Case vbYes
      Me.PQS & Integer & Name.Enabled = False
      Me.PQS & Integer & DateIssued.Enabled = False
      Me.PQS & Integer & DateDue.Enabled = False
      Me.PQS & Integer & Name.Value = Null
      Me.PQS & Integer & DateIssued.Value = Null
      Me.PQS & Integer & DateDue.Value = Null
   Case vbNo
      Me.PQS & Integer & Name.Enabled = True
      Me.PQS & Integer & DateIssued.Enabled = True
      Me.PQS & Integer & DateDue.Enabled = True
      Me.PQS & Integer & Name.undo
      Me.PQS & Integer & DateIssued.undo
      Me.PQS & Integer & DateDue.Value.undo
      Exit Sub
      End Select
End Sub
'''''''''''''''''''''''''''''''
Private Sub PQS1Issued_Click()
If Me.PQS1Issued.Value = -1 Then
Call YouSure(1)
Else
DoCmd.Save
End If
End Sub
Private Sub PQS2Issued_Click()
If Me.PQS2Issued.Value = -1 Then
Call YouSure(2)
Else
DoCmd.Save
End If
End Sub
 
Last edited:
Code:
Me.PQS & chkBox & Name.Enabled = True

That expression is invalid since a reference to an object cannot simply be concatenated into the command. (Even if it did have a dot before Name.)

To refer to a control using its name as a variable:

Code:
Me.Controls(strControlName).Enabled = True

Moreover it would be better to pass the control to the sub as a variable.

Code:
Sub YouSure(chkBox As Control)
 
     chkBox.Enabled = True
 
End Sub

This sub can be passed any control that has an enabled property without regard for its parent object.
 

Users who are viewing this thread

Back
Top Bottom