Enable Button Due To Criteria?

tarcona

Registered User.
Local time
Today, 08:30
Joined
Jun 3, 2008
Messages
165
Just a simple problem that I can't seem to figure out.

Background: I have a form and I have option to add a new company. Then when the user gets done filling out information they need to press Save. But the problem is that sometimes a blank record is saved...

So what I thought was that you can not save a record until 4 vital fields are filled out: Company Name, Address, City, and State.

So I disabled the Save command button and put code in various events to say that if these four fields are not null, then the save button is enabled. But nothing has worked thus far...any help? Thanks.
 
In the OnChange event of each of the four text box controls call a proceedure that checks to see if they are populated, if they are then enable the save button. Otherwise have the save button default to 'Not Enabled'. There are a number of other ways to do this as well - :)
 
I appreciate your helpfulnes. Ok...I can do everything, but not unsure how to check if they are populated? Could you give me more information on that?

I tried this code:

If Not(IsNull(Me.txtCompany And Me.txtAddress And Me.txtCity And Me.txtState)) Then
Me.btnSave.Enabled = True

But this did not work.
 
If Not(IsNull(Me.txtCompany And Me.txtAddress And Me.txtCity And Me.txtState))


Will not work ... has to be something along the lines of ....

Code:
If Not(IsNull(Me.txtCompany) And Not(IsNull(Me.txtAddress) And Not(IsNull(Me.txtCity) And Not(IsNull(Me.txtState)

Although I would break this up to give a specific tip to the user ...

Code:
If Len(Nz(Me!txtCompany, "")) = 0 Then
      MsgBox "You have not entered a company name.", , "Enter Company"
      Me!txtCompany.SetFocus
ElseIf Len(Nz(Me!txtAddress, "")) = 0 Then
      MsgBox "You have not entered an address.", , "Enter Address"
      Me!txtAddress.SetFocus
...

Instead of bounding it all up in one and the user doesn't know what is needed and leaves em guessing.

Note: If you put these on the command button you are fine, if you put these on the AfterUpdate() event of the control, you cannot set the focus back to the control because the control has not yet lost focus.

-dK
 
Last edited:
try

If Not Is Null(Me.txtCompany) And Not Is Null(Me.txtAddress) ...
 
Perfect. One question to better understand the code....what does the Nz mean? And what event would be the correct placement of this code?
 
I would use something like:

If Me!txtCompany & "" = "" Then
 
The IsNull() function returns true if the field is null and false if it's not.
The Nz() function returns zero if the field is null; otherwise it returns the value of the field. This is why it is great if you are doing calculations (set up a conditional statement to do calculations if something is there or do nothing if something is not there).

This allows you to check to see if the field is null or a zero length string in one line. You can take one extra step and go ...

Len(Trim(Nz(Me!txtControl, ""))) = 0 for a little extra protection.

-dK
 
Last edited:
I am still a little unsure as in where to place the code...

I tried in the After Update event of the last field (txtState) but that didn't work correctly.
 
On the BeforeUpdate() of the control itself would not be prudent because the user may never enter/change the control thus it would never check. If you place it in the AfterUpdate() then you have defeated your purpose of catching the discrepancy because it has already occurred.

My apologies for saying this before .. I hadn't finished my coffee yet. =]


If you are using a command button to exit the form ... place the code on the OnClick() event of the button ...

Code:
If Len(Nz(Me!txtCompany, "")) = 0 Then
      MsgBox "You have not entered a company name.", , "Enter Company"
      Me!txtCompany.SetFocus
ElseIf Len(Nz(Me!txtAddress, "")) = 0 Then
      MsgBox "You have not entered an address.", , "Enter Address"
      Me!txtAddress.SetFocus
...
Else 
      'it has passed all the checks so do what you want to do (save, etc.)
End If

Alternatively, you can place it in the BeforeUpdate() event of the form before the record is saved.

If your
 
IT should be in the AfterUpdate event of each of the textboxes holding the four required fields. You need to copy and post the exact code you tried.
 
LMAO ... I've got my mind twisted all the way around.

In the words of Willy Wonka, "Wait! Stop! Reverse that!"

Linq is correct.

-dK
 
Ok the problem that has arisen is with the placement of the code. The code works fine, but I dont want to put it on all of the fields because after I fill out the first one, and then press the TAB button it will keep on giving me error messages saying I have to fill out the rest of the first. And I haven't even had a chance to fill it out!

I put the code in the last required field (txtState) and that worked. BUT when, say, I left a field blank and it says to go back and put in information...and then I do put in information...the Save button still is disabled.
 
Ah thanks for the example DB, but it is unrecognizable format. I have Access 2000, so if you could convert it, that would be awesome.

Again, I appreciate both of your help in getting me through this.
 
If your save button is a command button and the code is written in the OnClick() event ... is the button enabled through the properties of the button?

-dK
 
I suggested the AfterUpdate event for each textbox, because that's where the post was heading and I was trying to put out a fire here at the same time as I was answering your post. In truth, if this is a standard Access form, i.e. bound to an underlying table or query, you have a major problem with this approach. Your user doesn't have to use your Save button to save a record! Yhey can move to another record, or simply Close the form!

Validation code used to see if a record has data in one or more textboxes is normally done in the Form's BeforeUpdate event. That way, no matter how the record is saved, these textboxes will be checked.
 
I appreciate everyone's help. Maybe the problem is with the Enable feature. Maybe I will enable the Save button, and when clicked it will search to see if all the required fields have information...if they do then it will proceed....if they don't it will ask the user to go back and filled out the missing fields. If the user does not want to then they user will have a choice of closing the form without saving...


Does that sound logical?
 
And Missinglinq. You are right. The Save button does not just Save the record. It is actually there to search for duplicated information. It does other various things too. But when the user adds a new company, to eliminate any duplications, the user then presses "Save" and a DLookup function looks through to see if that information is duplicated. So it is not just there to Save, because I know that it is automatically saved.

Thanks.
 

Users who are viewing this thread

Back
Top Bottom