Cancel button ruins program

ignite

Registered User.
Local time
Today, 01:53
Joined
Mar 15, 2004
Messages
31
Cancel button ruins program! Please help

Hello,

I'm making a Student Record System and one of the options I currently have is the ability to search through records (in this case students). When selected the user is allowed to enter a desired Surname and Forename to search as shown:

http://www.ingserv.com/help.jpg

This is created by the use of a macro with a where condition on click:


Code:
[Surname] Like [Students surname? (Omit for all records)] & "*" And [Forename] Like [Students forename? (Omit for all records)] & "*"

The code, incidently, all works fine the problem I am having is that when the user presses the 'Cancel' button as oppose to 'OK' the program springs up an error and closes my form down as shown:

http://www.ingserv.com/help2.jpg

Does anyone know any VB or an amendment I can make in my where condition to state what I want it to do upon clicking the 'Cancel' button please?

Any help much appreciated!

Thank you.
 
Hello,

I believe that you want to use the cancel button to cancel the action. if thats what you want try using the following code.

Private Sub btnUndo_Click()

Dim Msg, Style, Title, Response, MyString
Msg = "Do you want to Save Records ?" ' Define message.
Style = vbYesNoCancel + vbApplicationModal + vbSystemModal ' Define buttons.
Title = "Authentication Required" ' Define title.
Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then ' User chose Yes.
MyString = "Yes"
Dim i As Double
Dim ReturnValue As Variant
Dim maxvalue As Double
Dim stepp As Double
Dim X As Variant
Dim Total As Double

maxvalue = 700000
stepp = 100 / maxvalue

ReturnValue = SysCmd(SYSCMD_INITMETER, "UpDating Database", 100)
DoCmd.Hourglass True

For i = 1 To maxvalue
Total = Total + stepp
X = Int(Total)
ReturnValue = SysCmd(SYSCMD_UPDATEMETER, X)
Next i
DoCmd.Hourglass False


ReturnValue = SysCmd(SYSCMD_SETSTATUS, "Updating Database")
ReturnValue = SysCmd(SYSCMD_REMOVEMETER)


Dim currctl As Integer, numctls As Integer
Dim ctl As Control

numctls = Screen.ActiveForm.Count

For currctl = 0 To numctls - 1
Set ctl = Me(currctl)
If ctl.Tag = "Validate" Then
If IsNull(ctl) Then
MsgBox "Please fill in the Fields highlighted in red and click the Save button again.", vbOKOnly + vbCritical + vbDefaultButton1, "Field(s) Empty"
ctl.BackColor = 255
ctl.SetFocus
Exit Sub
Else
If Not IsNull(ctl) Then
ctlC.BackColor = Me!BackColor.BackColor
End If
End If
Next currctl

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.GoToRecord , , acNewRec


Else
If Response = vbNo Then
MyString = "No" ' Perform some action.

' For each control.
For Each ctlC In Me.Controls
If ctlC.ControlType = acTextBox Then
' Restore Old Value.
ctlC.Value = ctlC.OldValue
End If
Next ctlC
Else
If Response = vbCancel Then
MyString = "Cancel"
Dim ctlCC As Control
' For each control.
For Each ctlCC In Me.Controls
If ctlCC.ControlType = acTextBox Then
' Restore Old Value.
ctlCC.Value = ctlCC.OldValue
End If
Next ctlCC

DoCmd.Close

End If
End If
End If
End Sub

What this code exactly does is that when you click a button it gives you a message box with yes no and cancel buttons. yes will save the records, no will willnot save anything and return you to blank values and cancel will not save anything and close form.
 
Would I have to redo my button in order to use that VB code or can I use it side by side my macro - in which case how would I go about that please? :-)

Thank you.
 
right click the button>build event and just edit whats already there
 

Users who are viewing this thread

Back
Top Bottom