A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Entering New Records in a Form - Validation



 
 
Thread Tools Display Modes
  #1  
Old December 14th, 2006, 04:27 PM posted to microsoft.public.access.forms
Will
external usenet poster
 
Posts: 2
Default Entering New Records in a Form - Validation

I have a table where all the fields are set as required and a form based on
this table with Data Entry set to True. I only want the user to be able to
save a record on the form if all the fields are filled in. If the user
decides to cancel/close at some point I just want the form to close or
prompt with a message whether they want to save. From my research, I've
found that any validation should go in the form's beforeupdate property. My
code at the moment is :
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me.cboProductID & "") = 0 Then
MsgBox "Please select Product"
Me.ProductID.SetFocus
Cancel = True
Me.Undo
ElseIf Len(Me.txtProductName & "") = 0 Then
MsgBox "Please enter Product Name"
Me.txtProductName.SetFocus
Cancel = True
Me.Undo
ElseIf Len(Me.cboProductType & "") = 0 Then
MsgBox "Please select Product Type"
Me.cboProductType.SetFocus
Cancel = True
Me.Undo
.............'Continues'.......................... ......

I have a save and exit button which attempts to save the record with the
code : If Me.Dirty = True Then Me.Dirty = False. If the form's before
update validation is not met, I get runtime error 2101 : The setting you
entered isn't valid for this property. I have handled this in my error
handling. If the user then closes the form I get "you can't save this
record at this time" - How can I get this not to show? or replace it with my
own "Are you sure you wish to exit message box"? Also, is this the best way
to perform form validation? thanks


  #2  
Old December 14th, 2006, 11:34 PM posted to microsoft.public.access.forms
missinglinq via AccessMonster.com
external usenet poster
 
Posts: 545
Default Entering New Records in a Form - Validation

The Len function returns the number of characters in a string or string
variable. I think the problem is that you're trying to get Access to use Len
with 2 comboboxes, not just string or string variables. I think you need to
do your validation check on whatever controls on your form these comboboxes
are filling in; I assume you're using them to fill in controls on your
records.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via http://www.accessmonster.com

  #3  
Old December 15th, 2006, 02:29 PM posted to microsoft.public.access.forms
Sprinks
external usenet poster
 
Posts: 531
Default Entering New Records in a Form - Validation

Will,

If your BeforeUpdate event finds a blank control, you should exit the sub
and set the focus to the offending control such that the Me.Dirty test is
never performed. Also, it's easier to loop through the controls and use the
Tag property to store part of the field-specific message:

Dim ctl as Control
Dim strMsg as String
Dim intResponse as Integer

For Each ctl in Me.Controls
If IsNull(ctl) Then
Select Case ctl.ControlType
Case acTextbox
strMsg = "Please enter " & ctl.Tag
Case acComboBox, acListBox, acOptionGroup
strMsg = "Please select " & ctl.Tag
End Select
intResponse = MsgBox (strMsg, vbOKCancel, "Required Field")
If intResponse = vbOK Then
Cancel = True
Me![ctl].SetFocus
Exit Sub
Else
Me.Undo
End If
End If
Next ctl

Hope that helps.
Sprinks


"Will" wrote:

I have a table where all the fields are set as required and a form based on
this table with Data Entry set to True. I only want the user to be able to
save a record on the form if all the fields are filled in. If the user
decides to cancel/close at some point I just want the form to close or
prompt with a message whether they want to save. From my research, I've
found that any validation should go in the form's beforeupdate property. My
code at the moment is :
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me.cboProductID & "") = 0 Then
MsgBox "Please select Product"
Me.ProductID.SetFocus
Cancel = True
Me.Undo
ElseIf Len(Me.txtProductName & "") = 0 Then
MsgBox "Please enter Product Name"
Me.txtProductName.SetFocus
Cancel = True
Me.Undo
ElseIf Len(Me.cboProductType & "") = 0 Then
MsgBox "Please select Product Type"
Me.cboProductType.SetFocus
Cancel = True
Me.Undo
.............'Continues'.......................... ......

I have a save and exit button which attempts to save the record with the
code : If Me.Dirty = True Then Me.Dirty = False. If the form's before
update validation is not met, I get runtime error 2101 : The setting you
entered isn't valid for this property. I have handled this in my error
handling. If the user then closes the form I get "you can't save this
record at this time" - How can I get this not to show? or replace it with my
own "Are you sure you wish to exit message box"? Also, is this the best way
to perform form validation? thanks



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:42 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.