Conditional execution

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

How can I make sure that a command button will only execute its commands if
3 fields in my form have values, ie are not blank anymore?

I tried putting "if textbox a is not null and combobox b is not null and
combobox c is not null then" before the commands, but that isn't working.
Is it just a syntax issue, or is that approach fundamentally wrong?

Thank you.
 
I think the cheap & easy way out is to make the command button invisible
until there's text in the text field, because once there is, due to the
relationships between the tables, pressing the button will generate an error
unless there's a value in the other two combo boxes.
 
Niniel,

On the Click event of your button...
If Not IsNull([A]) and Not IsNull() and Not IsNull([C]) Then
'do something
End If
 
Ah, it was the syntax. Thank you very much for correcting it.

I ended up using the code to simply unhide my command button, instead of
attaching it to the onclick event of the button.
 
Niniel said:
Ah, it was the syntax. Thank you very much for correcting it.

I ended up using the code to simply unhide my command button, instead of
attaching it to the onclick event of the button.

:

Niniel,

On the Click event of your button...
If Not IsNull([A]) and Not IsNull() and Not IsNull([C]) Then
'do something
End If




Al's idea is good and will work, but you can avoid a lot of indenting by
using:

If IsNull(A.Value) Or IsNull(B.Value) Or IsNull(C.Value) Then
MsgBox("Required data is missing. Please try again.")
Exit Sub
End If
....

or by checking each condition serially.

If IsNull(cbxEmployee.Value) Then
MsgBox("Employee is a required field.")
Exit Sub
End If
....

I have used both command button code and hiding the command button
methods on forms. I think the command code is better when there exists
a possibility of the .Visible logic getting more complicated in the
future. Users like the appropriate buttons to show up automatically,
but for more than a few simple conditions the .Visible logic quickly
becomes something to be avoided.

James A. Fortune
(e-mail address removed)
 
Niniel,
Rather than "hide" the button, you could make it "disabled" until all 3 values are not
null.
You'll neeed to interrogate the 3 fields on the AfterUpdate event of each field...
If Not IsNull([A]) and Not IsNull() and Not IsNull([C]) Then
cmdYourButton.Enabled = True
Else
cmdYourButton.Enabled = False
End If

Also, you'll have to run that same code on the OnCurrent event of the form. That way,
as you browse from record to record, the button will Enable/Disable accordingly to the
status of the 3 fields.
 
All right, thank you both for the code and the advice, I shall keep that in
mind.
I do like the idea of giving the user a more meaningful message the way you
suggested doing it rather than just throwing the standard access error
message at him.

James A. Fortune said:
Niniel said:
Ah, it was the syntax. Thank you very much for correcting it.

I ended up using the code to simply unhide my command button, instead of
attaching it to the onclick event of the button.

:

Niniel,

On the Click event of your button...
If Not IsNull([A]) and Not IsNull() and Not IsNull([C]) Then
'do something
End If




Al's idea is good and will work, but you can avoid a lot of indenting by
using:

If IsNull(A.Value) Or IsNull(B.Value) Or IsNull(C.Value) Then
MsgBox("Required data is missing. Please try again.")
Exit Sub
End If
....

or by checking each condition serially.

If IsNull(cbxEmployee.Value) Then
MsgBox("Employee is a required field.")
Exit Sub
End If
....

I have used both command button code and hiding the command button
methods on forms. I think the command code is better when there exists
a possibility of the .Visible logic getting more complicated in the
future. Users like the appropriate buttons to show up automatically,
but for more than a few simple conditions the .Visible logic quickly
becomes something to be avoided.

James A. Fortune
(e-mail address removed)
 
Ah, ok, I hadn't known about the "enabled" command; that's a good idea, thank
you.
I'll use this strictly for a new record input form though, so there won't be
any browsing.

The reason why I'm even doing this is that I found out quite unexpectedly
that if you don't enter any data into the form at all, and then press the
command button - which appends records to a different table and makes visible
more tabs - the user can progress to the next tab, but won't actually see
anything meaningful since the append operation failed due to a lack of a
primary record [error/success messages are suppressed for this operation].
This should never be a problem because it wouldn't make any sense not to
fill in the information first, but you never know... somebody might click on
that button by accident, as did I, and then wonder what the heck is going on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top