Subform validation (datasheet view) - vba

I have a subform where the user can create rows. A new row is created by clicking a button on the main form, after which the user can set the other values. Two fields are required, and the user must not be allowed to leave them empty, even by closing the form. Instead, a MessageBox should alert the user and he should stay on the offending row, allowing him to set the required values.
This must work even if the user has not made any change to the fields.
I'm trying different events with message boxes to see if any of them fire, but none of them fire when I try to leave the fields empty, for example by changing row or closing the main form. I'm looking for some equivalent to "OnBlur" in Javascript.
I've tried BeforeUpdate and On Dirty, but both of them require a change. I've solved the one for when closing the form, using On Unload with the Cancel parameter.

If I understood it correctly you are trying to validate user's entry before inserting a new row. Did you try the event 'Before Insert' of the subform? Use Cancel = -1 to disallow inserting. That should work even if the user tries to close the main form.

Related

filter selection prompt by user

i want to run my code such that it can pause and allow user to filter a column and then the code continues to run. Is this possible?
I thought of using inputbox to prompt user to key in the name. However, this is not feasible because user may not be able to remember the exact name. It will be more convenient for the user to look at the drop down list and select the name he wants to filter.
Please advice me how can this be done. Thank you
You can't stop a VBA macro to wait until an action was done within Excel.
I see the following possibilities:
a) Create a small userform showing a dropdown. If you show that form modal (that is the default), the macro will pause and wait until the form is closed. However, you will need to implement code to fill up the dropdown and to set the filter by code.
b) End your macro and implement an event that fires when the user changes the filter. There is no "On filter change"-event in Excel, but there is a work around: Excel VBA Filter Change event handler. Put the second part of your code into a separate sub and call that from the event handler.

Allowing User to Select off of Partial Records in a Continuous Form in Access

I am creating a form in access to allow users to input multiple production records for a day.
The form is set as a continuous data entry form and has data validation in place to ensure the information being entered is consistent.
I am having a problem where if a user starts typing something on a new entry, they essentially have no way to back out of it or cancel the entry without completely filling out the form.
I want to keep the data validation to ensure the data being recorded is accurate, but also do not want to lock users into an entry unless it is completely filled out.
I think the ideal would be to allow users to create a new record or select other records without needing to save the current record.
If it would be possible to make it so records only save when a button at the top of the form is pressed I think that would be ideal, but I have not found a good way to do this without requiring it on every single entry.
I have attached a picture of what I am talking about, there could be various error messages but essentially if you try to click off when a record is incomplete it will give an error until the entire form is filled out.
Example of Error Message and Image of Continuous Form
'deselect' without saving but still save what had been entered up to
that point.
That you can simulate by setting the DefaultValue of each control in the AfterUpdate event of each control:
Me!SomeTextBox.DefaultValue = Nz(Me!SomeTextBox.Value)
However, I'm not sure that will be a good idea. And you may have to reset the default values when opening the form.

restricting a combobox to list values and outdated list values

I am creating a program that for simplicity's sake records the name of a staff member that receives a phone call. This program is designed to show old entries along with creating new ones.
The problem is that I want a user to only be able to select a listed name from the drop box when creating a new entry. But this list will only show current employees. Yet, when viewing older calls this combobox field also needs to display former employees that took a call that may no longer be in this list.
As far as I can tell with the Microsoft control and properties there is only 2 options that relate to this matter.
DropDownStyle as DropDown or DropDownList.
When using DropDown the user can submit any name (which is not wanted).
With DropDownList the user can only submit names on the list, but when browsing through old entries any names that are no longer on that list will not appear on their respective calls (which is also not wanted).
I'm aware I could end up having to implement my own combobox class but I wanted to see if anyone knew of a more elegant fix that combined both of these functionalities. Thanks!
It seems to me you have two modes. Add mode adds a new call record, while view mode displays old records.
Use the drop down list to restrict the user to what you load. When in Add mode, load the control with only current employees. When the form is in view mode, load with all employees.
Use DropDown. In the Validating event, set e.Cancel = True and instruct an ErrorProvider control to put up a warning with its SetError method if SelectedItem Is Nothing, but clear the error (by passing Nothing to SetError) otherwise. Then, in the combo box's SelectionChangeCommitted event, call the form's Validate method.

Need To Revert Multiple Controls Back To Previous Values

I have the following situation where I have multiple controls on a form. The user can select an Edit mode which allows them to modify the contents of the controls (textboxes, checkboxes, comboboxes, etc.) After editing has been made, the user can either select the Save or Cancel button.
Obviously clicking on save will save all changes to the database, however, if the user clicks on cancel, I want to 'somehow' revert all controls back to their previous state.
Currently, when the user clicks on cancel, the form turns off edit mode, but the controls remain with all the changes made until they reload the form.
Is there an easy way around this, or do I need to reload the entire form content from the database each time the user clicks on cancel?
Thanks in advance.
I'm just going to outline a possible solution here.
You could have a dictionary in your form in which you store the content of your controls whenever a user first modifies them, indexing them by the control name.
When the user starts modifying a control, you check if the control already exists in the dictionary. If it doesn't you add a new entry to the dictionary with the current value of teh control.
If the user cancels the editing process, you can loop through the dictionary, find each control in your form by its name (Form.Controls(ControlName) or something like that I think), set its value back to what it was and remove the entry from the dictionary.
It's not the most elegant solution as it will involve linking up all your controls to the TextChanged event or equivalent to capture their value when they are modified...
To avoid that, you could fill the dictionary right at the start when you load your form, after populating the values from the DB and then only set those controls' value back which have a different value when the user cancels.

Hiding columns in a datasheet

I am trying to hide specific columns in an Access 2007 split form through code. I need the form to check certain conditions to see whether it needs to display a column or not. I have code in the form's 'Activate' event to hide the column like this:
txtControl.ColumnHidden = True
This code works in the "Open" event, but if I hide the column on Activate, it won't display these changes until I close the form and open it again. I have tried calling the form's refresh, repaint, and requery methods, but this doesn't work. Please help!
Edit: Ideally, I need this event to occur whenever the focus switches to this form.That's why I'm using the Activate event rather than the Open event.
Try setting it in either the form's Current or Load events. You will also probably need to requery the control after setting that property: Me.TextControl.Requery Current is called every time a form's record is changed, the form is repainted or requeried. Load, as its name suggests, is called once, after the form has opened when the form loads its records. These have always been more reliable for me than using Activate, which really has to do with more the focus of the form, not really what you want.
I've had a problem like this before working in Access 2002. I was able to solve the problem with a subform by setting the subform source object equal to itself and then running the requery.
Me.SubForm.SourceObject = Me.SubForm.SourceObject
Me.SubForm.Requery
See if this technique works for your particular situation.