473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Where do I put my code?

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

Nov 12 '05 #1
2 2534
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


Nov 12 '05 #2
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

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
2
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:...
4
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...
16
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...
7
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...
10
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...
7
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...
0
NeoPa
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...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
0
NeoPa
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.