473,434 Members | 4,626 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,434 software developers and data experts.

Data Validation when using a Close button

SueHopson
47 32bit
Ok, I have spent way too much time on this, so reaching out for guidance.

When I run the code below it does everything I need it to do, except close the form... Before I added the required fields, I had originally used DoCmd.Close on the btnCxSave button, but now it closes the form before the Validation Code finishes running.

Thoughts?

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Form_BeforeUpdate(Cancel As Integer)
  3. If ValidateRecord = False Then
  4. Cancel = True
  5.  
  6. End If
  7. End Sub
  8.  
  9. Private Function ValidateRecord() As Boolean
  10.  
  11. ValidateRecord = False
  12. 'confirm name is entered
  13. If IsNull(CxName.Value) Then
  14. MsgBox CxName.ValidationText
  15. CxName.SetFocus
  16. Exit Function
  17. End If
  18. 'confirm either tel or email is entered - must have a minimum of one contact method minimum
  19. If IsNull(CxTel.Value) Then
  20.  If IsNull(CxEmail.Value) Then
  21.  MsgBox CxTel.ValidationText
  22.  CxTel.SetFocus
  23. Exit Function
  24. End If
  25. End If
  26.  
  27. ValidateRecord = True
  28.  
  29. End Function
  30.  
  31. Private Sub btnCxSave_Click()
  32.         ValidateRecord
  33.         'Forms("frmMAIN").[sbfmCx1_Customers].[Form].Requery
  34.         'Forms("frmMAIN").[sbfmCx2_Contacts].[Form].Requery
  35.         'Forms("frmMAIN").[sbfmCx3_Notes].[Form].Requery
  36. End Sub
  37.  
Apr 13 '23 #1
3 9040
NeoPa
32,556 Expert Mod 16PB
Hi Sue.

If I understand you correctly, and that means the code you're talking about is a different version than the set posted, you want the Save Command Button to :
  1. Check that the data is valid.
  2. If data valid.
    1. Save the data
    2. Close the form
  3. If data Invalid.
    1. Continue from the point where it found a problem
    2. Data remains unsaved
    3. Form remains open
To handle that we would need the btnCxSave_Click() code to attempt to save the record - rather than trying to invoke ValidateRecord() directly, as now seems to be the case. That will trigger the Form_BeforeUpdate() code to run automatically and leave Me.Dirty set to False if it succeeds and True otherwise.

Something like :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     Cancel = (Not ValidateRecord)
  3. End Sub
  4.  
  5. Private Function ValidateRecord() As Boolean
  6.     'ValidateRecord = False
  7.     'Above unnecessary as Boolean variables are always set to start as False.
  8.     With Me
  9.         'confirm name is entered
  10.         If IsNull(.CxName.Value) Then
  11.             Call MsgBox(.CxName.ValidationText)
  12.             Call .CxName.SetFocus
  13.             Exit Function
  14.         End If
  15.         'confirm either tel or email is entered
  16.         'must have a minimum of one contact method
  17.         If IsNull(.CxTel.Value) _
  18.         And IsNull(.CxEmail.Value) Then
  19.             Call MsgBox(.CxTel.ValidationText)
  20.             Call CxTel.SetFocus
  21.             Exit Function
  22.         End If
  23.     End With
  24.     ValidateRecord = True
  25. End Function
  26.  
  27. Private Sub btnCxSave_Click()
  28.     With Me
  29.         If .Dirty Then
  30.             .Dirty = False
  31.             If .Dirty Then Exit Sub
  32.             Call DoCmd.Close(ObjectType:=acForm _
  33.                            , ObjectName:=.Name _
  34.                            , Save:=acSaveNo)
  35.         End If
  36.         'Call .[sbfmCx1_Customers].[Form].Requery
  37.         'Call .[sbfmCx2_Contacts].[Form].Requery
  38.         'Call .[sbfmCx3_Notes].[Form].Requery
  39.     End With
  40. End Sub
NB. Although it seems quite counter-intuitive, the best code for telling Access to save the current record on a Form is to use Me.Dirty = False. A non-object-oriented version is to use Call DoCmd.RunCommand(acCmdSaveRecord) but I vaguely recall that sometimes even fails to execute as expected.
Apr 14 '23 #2
SueHopson
47 32bit
OK, that makes sense and I understand most of the logic behind the sequencing of these events, the function is called before the form updates and the save button is checking the form to see if it's been changed (Dirty = False)

So the private function is operating as expected and double checking the form and returning the proper messages...

HOWEVER when trying to enter a NEW customer record, after the first message appears, I am getting a 3021: No current record error. There is definitely a record being created as a new ID number is assigned and the records are being added to my table...

Specifically, the error points here to the .Dirty = False reference

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCxSave_Click()
  2.    With Me
  3.         If .Dirty Then
  4.             .Dirty = False
  5. '...the rest of the code....
So how is handling new records different in this code?
Apr 14 '23 #3
SueHopson
47 32bit
Just for fun, I also tried removing an existing customer name and saving it and got a run-time 2101 error.
The setting you entered isn't valid for this property

Same error line when I Debug as above
Apr 14 '23 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Dave Smithz | last post by:
Hi, In summary: I want to a form to submit information via a HTTP POST, however, when using Internet Explorer I want to be able to use the back button and all the information retained....
3
by: Stuart E. Wugalter | last post by:
Hello All! I have a button on a form that is tied to the following query: UPDATE tblSEQTAQ AS A SET A.taqcall = WHERE (((A.taqcall)="Allele 1")); The user looks at the form and decides to...
1
by: Scott | last post by:
Hi, I'm using a form to collect values for later analysis. My question is, how do I validate the value the user inputs into a textbox? For example, many of the values require a real number and...
1
by: ticars | last post by:
I have a web page which all it does is redirect a user to a different page (the page to redirect to is based on a database lookup). I want to make it so that when a user tries to use the back...
2
by: David | last post by:
Is there anyway to get a different DialogResult or detect that the user clicked the close button on a MessageBox? I have a message box that pops up when my program first starts that only has an ok...
1
by: daithi | last post by:
Hi This is a bit of mismatch but its more geared to the Java developer. I'm storing the file path from a browse button in Javascript but instead of locating a file I need it to locate a folder. Can...
1
by: sreedhardasi | last post by:
Hi, I would like to call a javascript function when user clicks on browser's close button. Here is the scenario. 1. User clicks browser close button. 2. User will be displayed a popup or a...
12
by: mylixes | last post by:
I have a Windows Application. I just don't understand why is it that when i click the Close button, instead of closing the form, it minimizes the whole application. I don't experience this on my...
2
by: eneyardi | last post by:
I have form name (home) it has a combobox its row source are name of employees. I want to put a command button add employee, that when i click it, msgbox or textbox will appear and you can add...
2
by: kroeh1234 | last post by:
I have an Equipment form with a subform for Utilization. So that the user can close or cancel a change, I created buttons, using the wizard, for close and undo. So, why does Access go through the...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.