473,385 Members | 2,014 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,385 software developers and data experts.

Clear bound fields on form

294 256MB
I have a form where users can enter a new record (or multiple).

If a user decides to not enter anything, it keeps throwing errors (my exceptions to prevent null fields from being entered).

The first control is a combobox that is the FK in the table, and the rest of the fields are just fields in the table (name, DOB, etc).

If I wanted to clear all fields so the user can exit the form without saving anything to the table, how can I perform this?

I tried Me.Combbox.Clear (clear isn't an available method), Me.Combobox.Text = "", etc.


Here is the code:

Expand|Select|Wrap|Line Numbers
  1. Me.EmployeeID = Null
  2.  
  3. Me.EmployeeID.SetFocus
  4. Me.EmployeeID.ListIndex = -1
  5.  
  6.     If IsNull(Me.EmployeeID.Value) = False Then
  7.         MsgBox ("Please delete all fields or press escape.")
  8.     ElseIf IsNull(Me.BenFirstName.Value) = False Then
  9.         MsgBox ("Please delete all fields or press escape.")
  10.     ElseIf IsNull(Me.BenLastName.Value) = False Then
  11.         MsgBox ("Please delete all fields or press escape.")
  12.     ElseIf IsNull(Me.BenSSN.Value) = False Then
  13.         MsgBox ("Please delete all fields or press escape.")
  14.     ElseIf IsNull(Me.BenStreet.Value) = False Then
  15.         MsgBox ("Please delete all fields or press escape.")
  16.     ElseIf IsNull(Me.BenCity.Value) = False Then
  17.         MsgBox ("Please delete all fields or press escape.")
  18.     ElseIf IsNull(Me.BenState.Value) = False Then
  19.         MsgBox ("Please delete all fields or press escape.")
  20.     ElseIf IsNull(Me.BenZip.Value) = False Then
  21.         MsgBox ("Please delete all fields or press escape.")
  22.     Else
  23.         DoCmd.Close
  24.         DoCmd.OpenForm "Home"
  25.     End If
  26.  
Nothing has worked thus far.

When the user goes to delete the text in a field, it also throws the error, which is very annoying.

Any ideas? Thanks.
Apr 2 '14 #1
6 5173
jimatqsi
1,271 Expert 1GB
mcupito,
Separate the logic for a form close and a save. You don't say where this code is. Maybe it is in the click event of a save button? In the form Close event you could
Expand|Select|Wrap|Line Numbers
  1. docmd.Close acForm,me.Name,acSaveNo
That would prevent save logic from being executed, which might prevent your code above from running - depends where it is.

Jim
Apr 3 '14 #2
jimatqsi
1,271 Expert 1GB
In the form close you'd probably want to add a message "You're about to throw away your last change" with a chance to cancel.

Jim
Apr 3 '14 #3
mcupito
294 256MB
Apologies, Jim.

I have a button that I'm using to trigger the On Click event, to go back to a different form and not save.

I tried your code but the record still saved.
Here's what I tried.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Home_Click()
  2.  
  3. Dim Cancel As Integer
  4. Dim alrtMsg As Integer
  5.  
  6. alrtMsg = MsgBox("Do you wish to discard your record(s)?", vbYesNo, "Warning")
  7.  
  8. If alrtMsg = vbYes Then
  9. DoCmd.Close acForm, Me.Name, acSaveNo
  10. DoCmd.OpenForm "Home"
  11. Else
  12. Cancel = True
  13. End If
  14.  
  15. End Sub
Apr 3 '14 #4
mcupito
294 256MB
So, I found this.

"...the acSaveNo option, means that you don't save any design changes of the form, and has no influence on the record."
Apr 3 '14 #5
jimatqsi
1,271 Expert 1GB
Oh, oops, I knew that, I think. What you need is
Expand|Select|Wrap|Line Numbers
  1. docmd.undo
I think that's it.

What I've found in the past is that if you have any fields that have default values, the action of the default value being placed in the text box or other object initiates a new record. So anything that would typically save changes tries to save an unfinished record if you don't actually do anything to complete the record.

I'm not really sure Undo will overcome that but it may. You might also change the default process to operate from code within the form's before Insert event.

Jim
Apr 3 '14 #6
mcupito
294 256MB
I appreciate it, you helped me out regardless.

I kind of took an unnecessarily "safe" approach.
Any time the record was going to be saved, I prompt the user.
See below:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If Me.Dirty Then
  3.   If MsgBox("Do you wish to save the record(s)?", vbOKCancel, "Save?") = vbCancel Then
  4.     Me.Undo
  5.   End If
  6. End If
  7. End Sub
  8.  
  9. Private Sub Home_Click()
  10.  
  11. Dim Cancel As Integer
  12. Dim alrtMsg As Integer
  13.  
  14. alrtMsg = MsgBox("Are you sure you want to exit?", vbYesNo, "Warning")
  15.  
  16. If alrtMsg = vbYes Then
  17. Me.Undo
  18. DoCmd.Close acForm, Me.Name, acSaveNo
  19. DoCmd.OpenForm "Home"
  20. Else
  21. Cancel = True
  22. End If
  23. End Sub
Apr 3 '14 #7

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

Similar topics

2
by: tshad | last post by:
Is there a way to tell a screen on Postback to clear all the objects (or put them back to their defaults on Postback)? At the moment, I am just going through and setting them manually before...
1
by: John Dalberg | last post by:
When I change the data source for a report say like from SQL to a stored procedure or if the table fields have changed, all the bound fields on the report vanish. It's time consuming to recreate...
1
by: Aparna Rege | last post by:
Hi, I am having trouble with a tab control. All the fields on the form are data bound and are spread over two tabs of a tab control. I have placed a Clear button with the code to clear all the...
6
by: Welie | last post by:
I am having a problem an it's driving me crazy, I hope someone can correct my technique. I can't find a pattern and don't know where to look to debug this. The problem is on a continuous form, the...
9
by: rglaeser | last post by:
Whenever I try to adjust the spacing in a Report while in Design View; some of the fields are bound to each other. This makes adjusting them very difficult, if not impossible. How do I un-bound these...
1
by: loisk | last post by:
Hi, I have created a new Access query and form, linked to MySQL table. I have some fields for just displaying info from MySQL, and others (also bound) are to be entered by user. However when I...
1
by: BarbieJo | last post by:
Using Access 2007 I want all the bound fields in my form to be locked until a command button is clicked. I have already tried locking the form through the OnLoad property using the following code:...
1
by: Lilly Torres | last post by:
I have a simple form to add, edit, delete & view records, what I want is that when I am viewing the records i can have the oportunity to press the Add Record Button and that the form automatically...
2
by: nectar | last post by:
Hello, I have a two tables in my database (Customer, Transaction) which are related using the CustomerID. I have dragged and dropped the fields of Customer on my form using Visual Studio tools....
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.