I have an input form which has a command button called cmdSave. Thre
procedure behind this button does 2 things
1. It validates that 2 text controls are completed and
2. Pops up lots of message boxes asking the user if they want to save the
record, edit the record and input another record.
However I am having problems getting the code sequence right and wondered
first of all whether it was wise to put all this code behind the command
button and would it be better say in the BeforeUpdate property of the form.
Can anyone advise me and also can they spot anything obviously wrong with
this code.
I am a NEWBIE!!!!
Thanks
Tony Williams
Here is the code:
Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click
DoCmd.SetWarnings False
Dim Answer As Integer
Dim Answer2 As Integer
Dim Answer2a As Integer
Dim Answer3 As Integer
Answer = MsgBox("Are you sure you want to save this record?", 36, "Enter New
Record")
If Answer = vbYes Then
If IsNull(DocNametxt.Value) Then
MsgBox "You cannot save a record without a Document Name. Please
enter a name.", 16
[DocNametxt].SetFocus
Cancel = True
End If
If IsNull(cmbAuthor.Value) Then
MsgBox "You cannot save a record without an Author." & vbCrLf &
"Please enter an Author's name.", vbCritical, "Name Required"
Cancel = True
[cmbAuthor].SetFocus
End If
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
Answer2 = MsgBox("Do you want to input another record?", 36,
"New Record")
If Answer2 = vbYes Then
DoCmd.OpenForm "frmdocumentrecord", acNormal
DoCmd.GoToRecord , , acNewRec
If IsNull(Me.Docnumtxt) Then
Populate_URN
[DocNametxt].SetFocus
End If
End If
If Answer2 = vbNo Then
Answer2a = MsgBox("Do you want to return to edit the
record?", 36, "New Record")
If Answer2a = vbYes Then
DoCmd.OpenForm "frmdocumentrecord", acNormal
Cancel = True
End If
If Answer2a = vbNo Then
DoCmd.Close
Cancel = True
End If
End If
If Answer = vbNo Then
Answer3 = MsgBox("Do you want to return to edit the record?",
36, "New Record")
If Answer3 = vbYes Then
DoCmd.OpenForm "frmdocumentrecord", acNormal
Cancel = True
End If
If Answer3 = vbNo Then
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
DoCmd.Close
End If
End If
End If
DoCmd.SetWarnings True
Exit_cmdSave_Click:
Exit Sub
Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click
End Sub 2 2512
Tony Williams wrote: I have an input form which has a command button called cmdSave. Thre procedure behind this button does 2 things 1. It validates that 2 text controls are completed and 2. Pops up lots of message boxes asking the user if they want to save the record, edit the record and input another record. However I am having problems getting the code sequence right and wondered first of all whether it was wise to put all this code behind the command button and would it be better say in the BeforeUpdate property of the form. Can anyone advise me and also can they spot anything obviously wrong with this code. I am a NEWBIE!!!! Thanks Tony Williams
I would do your validation in the BeforeUpdate. When the button is
pressed you can determine if the data has changed with the Dirty
property. You can then force a save. The following could be put in
your button code
If Me.Dirty Then Me.Dirty = False
and this will call the validation routine in BeforeUpdate. You will
want to have an Error routine in the button code because if you Cancel
the save in the BeforeUpdate you want to trap the error message. Ex:
Err_CommandSave:
If err.Number <> 2501 then msgbox err.description
resume Exit_CommandSave
You really don't need the error routine. It simply stops the message
from being displayed that informs the user the action was canceled.
In your case I might want to present a new form instead of annoying the
user with messageboxes. Create a new form with an option group and 3
checkbox options; New Record, Edit Current Record, Exit. Let's call
this form RecOptions. If NewRec or Edit Current are selected, make the
form invisible, else close the form. Lets say the option group is
called Frame1
Me.Visible = False
Sub CommandSave
If Me.Dirty Then Me.Dirty = False
Docmd.OpenForm "RecOptions",,,,acDialog
Select Case Forms!RecOptions!Frame1
Case 1
'code to go to new record
Case 2
'use selected to edit/view current
Me.WhateverFieldYouWant.SetFocus
Case 3
'user selected to exit
docmd.close
end select
Exit_CommandSave:
Exit sub
Err_CommandSave:
If err.Number <> 2501 then msgbox err.description
resume Exit_CommandSave
End Sub
Now you get rid of the myriad number of messageboxes.
Personally, I like the navigation buttons. You could put a button if
you like to save and do the Me.Dirty = False. But with the navigation
buttons they simply stay on the record or can press the button to go to
a new record. If the table isn't going to be huge, you can have a
synchronized combo to let the user select the record to view/edit. But
that's my personal preference.
Here is the code:
Private Sub cmdSave_Click() On Error GoTo Err_cmdSave_Click DoCmd.SetWarnings False Dim Answer As Integer Dim Answer2 As Integer Dim Answer2a As Integer Dim Answer3 As Integer
Answer = MsgBox("Are you sure you want to save this record?", 36, "Enter New Record") If Answer = vbYes Then If IsNull(DocNametxt.Value) Then MsgBox "You cannot save a record without a Document Name. Please enter a name.", 16 [DocNametxt].SetFocus Cancel = True End If If IsNull(cmbAuthor.Value) Then MsgBox "You cannot save a record without an Author." & vbCrLf & "Please enter an Author's name.", vbCritical, "Name Required" Cancel = True [cmbAuthor].SetFocus End If DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70 Answer2 = MsgBox("Do you want to input another record?", 36, "New Record") If Answer2 = vbYes Then DoCmd.OpenForm "frmdocumentrecord", acNormal DoCmd.GoToRecord , , acNewRec If IsNull(Me.Docnumtxt) Then Populate_URN [DocNametxt].SetFocus End If End If If Answer2 = vbNo Then Answer2a = MsgBox("Do you want to return to edit the record?", 36, "New Record") If Answer2a = vbYes Then DoCmd.OpenForm "frmdocumentrecord", acNormal Cancel = True End If If Answer2a = vbNo Then DoCmd.Close Cancel = True End If End If
If Answer = vbNo Then Answer3 = MsgBox("Do you want to return to edit the record?", 36, "New Record") If Answer3 = vbYes Then DoCmd.OpenForm "frmdocumentrecord", acNormal Cancel = True End If If Answer3 = vbNo Then DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 DoCmd.Close End If End If End If DoCmd.SetWarnings True Exit_cmdSave_Click: Exit Sub
Err_cmdSave_Click: MsgBox Err.Description Resume Exit_cmdSave_Click
End Sub
Thanks Salad that's good advice about the option group I'll think I'll try
that
Tony
"Salad" <oi*@vinegar.com> wrote in message
news:Tf******************@newsread2.news.pas.earth link.net... Tony Williams wrote: I have an input form which has a command button called cmdSave. Thre procedure behind this button does 2 things 1. It validates that 2 text controls are completed and 2. Pops up lots of message boxes asking the user if they want to save
the record, edit the record and input another record. However I am having problems getting the code sequence right and
wondered first of all whether it was wise to put all this code behind the command button and would it be better say in the BeforeUpdate property of the
form. Can anyone advise me and also can they spot anything obviously wrong
with this code. I am a NEWBIE!!!! Thanks Tony Williams
I would do your validation in the BeforeUpdate. When the button is pressed you can determine if the data has changed with the Dirty property. You can then force a save. The following could be put in your button code If Me.Dirty Then Me.Dirty = False and this will call the validation routine in BeforeUpdate. You will want to have an Error routine in the button code because if you Cancel the save in the BeforeUpdate you want to trap the error message. Ex: Err_CommandSave: If err.Number <> 2501 then msgbox err.description resume Exit_CommandSave You really don't need the error routine. It simply stops the message from being displayed that informs the user the action was canceled.
In your case I might want to present a new form instead of annoying the user with messageboxes. Create a new form with an option group and 3 checkbox options; New Record, Edit Current Record, Exit. Let's call this form RecOptions. If NewRec or Edit Current are selected, make the form invisible, else close the form. Lets say the option group is called Frame1 Me.Visible = False
Sub CommandSave If Me.Dirty Then Me.Dirty = False Docmd.OpenForm "RecOptions",,,,acDialog Select Case Forms!RecOptions!Frame1 Case 1 'code to go to new record Case 2 'use selected to edit/view current Me.WhateverFieldYouWant.SetFocus Case 3 'user selected to exit docmd.close end select
Exit_CommandSave: Exit sub Err_CommandSave: If err.Number <> 2501 then msgbox err.description resume Exit_CommandSave End Sub
Now you get rid of the myriad number of messageboxes.
Personally, I like the navigation buttons. You could put a button if you like to save and do the Me.Dirty = False. But with the navigation buttons they simply stay on the record or can press the button to go to a new record. If the table isn't going to be huge, you can have a synchronized combo to let the user select the record to view/edit. But that's my personal preference. Here is the code:
Private Sub cmdSave_Click() On Error GoTo Err_cmdSave_Click DoCmd.SetWarnings False Dim Answer As Integer Dim Answer2 As Integer Dim Answer2a As Integer Dim Answer3 As Integer
Answer = MsgBox("Are you sure you want to save this record?", 36, "Enter
New Record") If Answer = vbYes Then If IsNull(DocNametxt.Value) Then MsgBox "You cannot save a record without a Document Name. Please enter a name.", 16 [DocNametxt].SetFocus Cancel = True End If If IsNull(cmbAuthor.Value) Then MsgBox "You cannot save a record without an Author." & vbCrLf & "Please enter an Author's name.", vbCritical, "Name Required" Cancel = True [cmbAuthor].SetFocus End If DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70 Answer2 = MsgBox("Do you want to input another record?", 36, "New Record") If Answer2 = vbYes Then DoCmd.OpenForm "frmdocumentrecord", acNormal DoCmd.GoToRecord , , acNewRec If IsNull(Me.Docnumtxt) Then Populate_URN [DocNametxt].SetFocus End If End If If Answer2 = vbNo Then Answer2a = MsgBox("Do you want to return to edit the record?", 36, "New Record") If Answer2a = vbYes Then DoCmd.OpenForm "frmdocumentrecord", acNormal Cancel = True End If If Answer2a = vbNo Then DoCmd.Close Cancel = True End If End If
If Answer = vbNo Then Answer3 = MsgBox("Do you want to return to edit the
record?", 36, "New Record") If Answer3 = vbYes Then DoCmd.OpenForm "frmdocumentrecord", acNormal Cancel = True End If If Answer3 = vbNo Then DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 DoCmd.Close End If End If End If DoCmd.SetWarnings True Exit_cmdSave_Click: Exit Sub
Err_cmdSave_Click: MsgBox Err.Description Resume Exit_cmdSave_Click
End Sub This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rachel Weeden |
last post by:
I'm working on an ASP Web application, and am having syntax issues in
a WHERE statement I'm trying to write that uses the CInt Function on a
field.
Basically, I want to select records using...
|
by: BoB Teijema |
last post by:
Hi all,
One of our companies is having problems with a query on a linked server.
They have two servers, serverA and serverB. On serverA they have set up a
linked server to serverB.
Query:...
|
by: Barry Edmund Wright |
last post by:
I would really appreciate your assistance.
I am using Access 2000 to create a form that Lists Names and Addresses based
on a number of selection criteria one of which is a combo box cboPCZip. All...
|
by: Dixie |
last post by:
I have a problem using Dev Ashish's excellent module to concatenate the
results of a field from several records into one record.
I am using the code to concatenate certain awards onto a...
|
by: Mr. Mountain |
last post by:
In the following code I simulate work being done on different threads by
sleeping a couple methods for about 40 ms. However, some of these methods
that should finish in about 40 -80 ms take as long...
|
by: Ray Stevens |
last post by:
I am attempting to test a VeriSign account and the setup instructions stated
that the certs file needed to go into the Windows\System32 folder. I am
getting an error from the code-behind assebly...
|
by: Britney |
last post by:
Original code:
this.oleDbSelectCommand1.CommandText = "SELECT TOP 100 user_id, password, nick_name, sex, age, has_picture, city, state, " +
"country FROM dbo.users WHERE (has_picture = ?) AND (sex...
|
by: NeoPa |
last post by:
Background
Whenever code is used there must be a way to differentiate the actual code (which should be interpreted directly) with literal strings which should be interpreted as data. Numbers don't...
|
by: Miroslaw Makowiecki |
last post by:
Where can I download Comeau compiler as a trial version?
Thanks in advice.
|
by: NeoPa |
last post by:
Intention :
To prepare a WHERE clause for multiple field selection, but to ignore any fields where the selection criteria are not set. ONLY WORKS WITH TEXT FIELD SELECTIONS.
Scenario :
You have...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |