Question : Add New record from unbound form

Hi experts,

Again, I'm pulling my hair off doing a seamingly very simple UI. Basically, I have an unbound form that will allow user to Search, Add and Edit records in my table. The reason I'm using an unbound form instead of bound form is because I have a lot of conditions to check before a record can be valid, and some controls are not actually from field in my table but rather lookup field from other tables.

Anyways, although I struggled quite a number of hours but finally the Search and Edit command buttons are woking fine now. The problem I have now is with Add New. As my table's primary key is a autonumber. I assume when I add a new record the ID is auto generated and written to my table after .Update method. How can I make sure that all the required fields are filled-up accordingly before user navigates to other records?

Not sure if I make it clear... Hope someone can give a hint or it would be much appreciated if there can be a similar example to look at.

Many thanks.

Answer : Add New record from unbound form

Agree with Capricorn1 and dqmq, however I'll add my 2 cents worth as well.

1.  As dqmd mentioned, you can use the controls validation rule, but sometimes users would like to be able to fill in controls in their preferred sequence, so you might consider using the controls Exit event and just popup a warning when they exit the control that indicates that the control is required before the record can be saved.

2.  Capricorn1's code will check all of the required fields but does not give you a very specific message.  Since your form is unbound, you probably won't have the built in navigation buttons (I think this is a good thing).  I generally provide Save and Cancel buttons on each of my forms.  With the Save button, you can do one of two things:

a.  You can do your validation tests in the Click event of the button, checking each of the fields which are required to ensure that they have a valid entry.  If not, display a field specific message, and set the focus back to the appropriate control.

b.  You can disable the Save button until all of the required fields are filled in.  To do this, you would need to use the Exit or AfterUpdate event of each of the controls and call code like Cap1 provided to enable/disable the command button based on the values in all of the fields.  The down side of this method is they have to search around on the form to identify which of the required fields is missing data.  I have, when the client wanted it, setup my required controls (textboxes and comboboxes) with a conditional format that is a different color than the standard.  I do this with a function (see below) and by setting the controls condition as "Expression Is" with a parameter that looks like:  fnRequiredButEmpty(Forms!formname.controlname)

If you do this and set the background property to a light color (I usually use a pale yellow), then the user can immediately see what controls are required, and which ones have not been filled in.  You could modify the function to check for specific value for specific controls instead of just testing to see whether the lenght > 0, but this will give you an idea.


Private Function fnRequiredButEmpty(Ctrl As Control) As Boolean

    RequiredButEmpty = (Len(Ctrl.Value & "") = 0)
   
End Function
Random Solutions  
 
programming4us programming4us