cannot exit form freely

A

Andre C

I have a form with an exit button. The problem is if the user opens
the form but decides he did not want to open it and presses exit the
validation rule kicks in and so he cannot exit.

As you will see from the code below I added
If Combo107 = Null Then DoCmd.Close

Combo107 should be null until someone selects within it. (it is used
to find a record -customer details) I have put a stop point in the
code after this and combo107 does indeed = null yet does not execute
the command

Any suggests.

Code snip

Private Sub Command92_Click()
On Error GoTo Err_Command92_Click


If Combo107 = Null Then DoCmd.Close

If Duration = 0 Then
MsgBox ("The duration has to be greater than zero.")
Exit Sub
End If

DoCmd.Close

Exit_Command92_Click:
Exit Sub

Err_Command92_Click:
MsgBox Err.Description
Resume Exit_Command92_Click


Also pressing the X in the window makes the prog come up with the
usual cannot find record message, is there a way to supress this?

Andre
 
R

Randy Harris

Andre C said:
I have a form with an exit button. The problem is if the user opens
the form but decides he did not want to open it and presses exit the
validation rule kicks in and so he cannot exit.

As you will see from the code below I added
If Combo107 = Null Then DoCmd.Close

Combo107 should be null until someone selects within it. (it is used
to find a record -customer details) I have put a stop point in the
code after this and combo107 does indeed = null yet does not execute
the command

Any suggests.

Code snip

Private Sub Command92_Click()
On Error GoTo Err_Command92_Click


If Combo107 = Null Then DoCmd.Close

If Duration = 0 Then
MsgBox ("The duration has to be greater than zero.")
Exit Sub
End If

DoCmd.Close

Exit_Command92_Click:
Exit Sub

Err_Command92_Click:
MsgBox Err.Description
Resume Exit_Command92_Click


Also pressing the X in the window makes the prog come up with the
usual cannot find record message, is there a way to supress this?

Andre


Andre,

The default property of the combo box returns a string value. A string
cannot be compared to a null with "=". It will always be false, regardless
of what the string contains.

You can use the IsNull operator to do the comparison, use the Nz function,
or something like

If Combo107 & "" = "" Then DoCmd.Close
 
M

Marshall Barton

Andre said:
I have a form with an exit button. The problem is if the user opens
the form but decides he did not want to open it and presses exit the
validation rule kicks in and so he cannot exit.


A table validation rule will not kick in unless there is
data to be saved. This will not happen if the user has not
entered at least one keystroke in a bound control OR you
have code that sets the value of a bound control.
 
A

Andre C

A table validation rule will not kick in unless there is
data to be saved. This will not happen if the user has not
entered at least one keystroke in a bound control OR you
have code that sets the value of a bound control.


No code, only within the rules but I do not believe it is because of
the validation. On of the boxes is compulsary.
 
A

Andre C

You can use the IsNull operator to do the comparison, use the Nz function,
or something like

If Combo107 & "" = "" Then DoCmd.Close

Thanks I tried the above exactly as written and the form exits fine
BUT after it closes and I return to teh switchboard there is a
warning box saying "the expression you entered refers to an object
that is closed or does not exist."

So close.

By the way I open the form from a switchboard in "add new record"
mode.
 
R

Randy Harris

Andre C said:
Thanks I tried the above exactly as written and the form exits fine
BUT after it closes and I return to teh switchboard there is a
warning box saying "the expression you entered refers to an object
that is closed or does not exist."

So close.

By the way I open the form from a switchboard in "add new record"
mode.

Does the error dialog that appears have a button on it marked "Debug"? If
so, click that button to see the line of code that is generating the error.
Post that line of code and identify what procedure it is in. Odds are good
that someone here can tell you what the problem is.
 
M

Marshall Barton

Andre said:
No code, only within the rules but I do not believe it is because of
the validation. On of the boxes is compulsary.


A box can not be compulsory if there is **no** data to be
saved. Something, somwhere must be adding some data to a
bound control. Check all the code in the form's module.

Even if you are only using a control's validation, it won't
kick in until the control's value is changed.
 
R

Randy Harris

Andre C said:
No, just Okay.

It sounds like something is changing the data on that form. You can confirm
this rather easily. Add "MsgBox Me.Dirty" right at the top of your form
Close event. When you close the form, it will pop up the message indicating
whether Access "thinks" the data has been changed or not.

If the form is dirty (which means the data has changed), do as Marsh
suggested, check through *all* of the code in the form module, something is
changing something.
 
A

Andre C

It sounds like something is changing the data on that form. You can confirm
this rather easily. Add "MsgBox Me.Dirty" right at the top of your form

It comes up FALSE

I do have some code in the On activate entry of the form as follows

If Combo128 = "T" Then
[VISIT-CODE-ID] = 10
Combo131 = 10
End If

The idea of this is that if combobox is set to T then it means the
record is a telephone contact so VISIS-CODE-ID needs to be set to 10.
However Combo128 defaults to T anyway when the form opens because T is
the most common option so i was trying to be nice and pre-fill inthe
form. I assume this is the problem.

Where else could I put this code?

Andre
 
R

Randy Harris

Ahhh, that's almost certainly what is causing the problem.

You can set default values for the controls without "dirtying" the form.
That is, they can contain your default values, but if the user doesn't
change anything, he can exit the record without saving it. Is Combo131
bound to [VISIT-CODE-ID]? If that's the case, comment out that code and
simply set 10 as the default value for Combo131.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.


Andre C said:
It sounds like something is changing the data on that form. You can confirm
this rather easily. Add "MsgBox Me.Dirty" right at the top of your form

It comes up FALSE

I do have some code in the On activate entry of the form as follows

If Combo128 = "T" Then
[VISIT-CODE-ID] = 10
Combo131 = 10
End If

The idea of this is that if combobox is set to T then it means the
record is a telephone contact so VISIS-CODE-ID needs to be set to 10.
However Combo128 defaults to T anyway when the form opens because T is
the most common option so i was trying to be nice and pre-fill inthe
form. I assume this is the problem.

Where else could I put this code?

Andre
 
M

Marshall Barton

Well, if any of those controls are bound, you will be
dirtying the record. This will be a very bad thing if the
current record is an existing record. If its a new record,
it will create a record whether the user intended it or not.

You didn't say if the combo boxes are bound or not, so I
can't say what to do about those.

OTOH, it sort of looks like [VISIT-CODE-ID] is a bound text
box. If that's the case, you should not be setting its
Value, you should set its DefaultValue
Me.[VISIT-CODE-ID].DefaultValue = 10
so its ready IF the user decides to create a new record.
BUT, why not just do this in design view and get rid of all
this confusing code.

It is also inappropriate to use the Activate event. It will
fire when the form first opens, but it will also fire at
other times and may mess up a record you didn't intend to
modify.
--
Marsh
MVP [MS Access]

Andre said:
It comes up FALSE

I do have some code in the On activate entry of the form as follows

If Combo128 = "T" Then
[VISIT-CODE-ID] = 10
Combo131 = 10
End If

The idea of this is that if combobox is set to T then it means the
record is a telephone contact so VISIS-CODE-ID needs to be set to 10.
However Combo128 defaults to T anyway when the form opens because T is
the most common option so i was trying to be nice and pre-fill inthe
form. I assume this is the problem.
 
A

Andre C

OTOH, it sort of looks like [VISIT-CODE-ID] is a bound text
box. If that's the case, you should not be setting its
Value, you should set its DefaultValue
Me.[VISIT-CODE-ID].DefaultValue = 10
so its ready IF the user decides to create a new record.
BUT, why not just do this in design view and get rid of all
this confusing code.
Many thanks for your input (Randy's too) I am still learning access
and sometimes try to pull off things that appear logical until I try
them.

This is going to sound silly but I cannot set the default value of
combobox131 and I don't fully understand why.

Randy said " Is Combo131
bound to [VISIT-CODE-ID]? If that's the case, comment out that code
and
simply set 10 as the default value for Combo131."

Yes it is bound. The combobox draws its data from a table
"visit-types" Thre are three columns to this combo box. What I need
from the user is the visit type (in this case telephone) and I store
the associated [VISIT-CODE-ID] (the key field) into the new record. I
know that I want the default to be the 10th one in the list (which is
telephone). So what do I put into the property default value. Not 10
-that does not work. Not QQ which is the description linked to 10 in
visit-types table. I am confused.

So just to sum up: on part of this form are two comboboxes, both bound
to fields. As above combo131 gets its data from a table - visit type.
Combo128 has a set of strings as its data and is bound to "client
seen" field.

I then have the code

If Combo128 = "T" Then
[VISIT-CODE-ID] = 10
Combo131 = 10
End If

In the activate event

My rationale (faulty I accept) is that if I open the record up (from
the switchboard) in add record mode I have a default value of "T" in
combo128 and hence the code sets Combo131 to 10 to be nice to the
user. However, if I i open the form in edit record mode combo128 will
take its data from the database and therefore may not be "T"

If I can set combo128 as "T" why can't I set combo131 as entry number
10 of the table?
 
M

Marshall Barton

Andre said:
Marshall said:
OTOH, it sort of looks like [VISIT-CODE-ID] is a bound text
box. If that's the case, you should not be setting its
Value, you should set its DefaultValue
Me.[VISIT-CODE-ID].DefaultValue = 10
so its ready IF the user decides to create a new record.
BUT, why not just do this in design view and get rid of all
this confusing code.
Many thanks for your input (Randy's too) I am still learning access
and sometimes try to pull off things that appear logical until I try
them.

This is going to sound silly but I cannot set the default value of
combobox131 and I don't fully understand why.

Randy said " Is Combo131
bound to [VISIT-CODE-ID]? If that's the case, comment out that code
and
simply set 10 as the default value for Combo131."

Yes it is bound. The combobox draws its data from a table
"visit-types" Thre are three columns to this combo box. What I need
from the user is the visit type (in this case telephone) and I store
the associated [VISIT-CODE-ID] (the key field) into the new record. I
know that I want the default to be the 10th one in the list (which is
telephone). So what do I put into the property default value. Not 10
-that does not work. Not QQ which is the description linked to 10 in
visit-types table. I am confused.

So just to sum up: on part of this form are two comboboxes, both bound
to fields. As above combo131 gets its data from a table - visit type.
Combo128 has a set of strings as its data and is bound to "client
seen" field.

I then have the code

If Combo128 = "T" Then
[VISIT-CODE-ID] = 10
Combo131 = 10
End If

In the activate event

My rationale (faulty I accept) is that if I open the record up (from
the switchboard) in add record mode I have a default value of "T" in
combo128 and hence the code sets Combo131 to 10 to be nice to the
user. However, if I i open the form in edit record mode combo128 will
take its data from the database and therefore may not be "T"

If I can set combo128 as "T" why can't I set combo131 as entry number
10 of the table?


We need to figure out why you can not set the combo box's
DefaultValue. Maybe it's because of your misperception
about using the number 10 to refer to the 10th item in the
combo box's rowsouce, which certainly won't work. The
Default value should be set to whatever you have in that row
and the column specified in the BoundColumn property. If
you operate this whole thing manually, then look in the
form's table to see what was saved in the field the combo
box is bound to you should see the value that you want to
enter into the DafaultValue property. I think you said you
tried QQ, but you didn't say if that is in the combo box's
bound column and the table's field.
 
A

Andre C

I thought it would be easier to do some screen shots so I have.

http://ccgi.andre02.plus.com/wordpress/?p=86.

Sorry if the layout is poor but I did this quite hastily. Please
wander around the blog whist there :)

I hope this helps to solve this puzzle. Along with my total probnlem
which you have been helpign me with this is the only other problem at
mo and it has been bothering me and more so my users for some time
now.

I am going away over the weekend so I may not respond to any further
posts here or on blog until late Sunday (British time)

Andre
 
R

Randy Harris

Andre, Marsh is a lot smarter than me, so I've been monitoring this while he
attempts to get you the help you need. Throughout the thread, several
different problems have been described. I think it might be helpful if you
could restate exactly what the current status is.

Did you take that "[VISIT-CODE-ID] = 10" out?
What specific problem are you having in setting a default for Combo131?
What error is occurring now, other than not getting a default for Combo131?
 
G

google

I now have a very strong suspicion that the problem lies in the exit
button code. As when I press the X the error does not happen.

The error message I get is the expression you entered refers to an
object that does not exist
.. Here is the code.

Private Sub Command92_Click()
On Error GoTo Err_Command92_Click


If Combo107 & "" = "" Then DoCmd.Close

If Duration = 0 Then
MsgBox ("The duration has to be greater than zero.")
Exit Sub
End If

DoCmd.Close

Exit_Command92_Click:
Exit Sub

Combo 107 is the "firstly whose patient is it" box you can see in the
images of the form.

As for the dedfault value. It doesa not seem to matter what I put in
the default value property box for combo131, it still dispalyed the
first value in the list. i.e QA.

Hopefully the blog images will help visualise this.
 
G

google

I have solved the problem of exiting cleanly and the problem, or
misinterpretation was indeed in the exit button.

the line If Combo107 & "" = "" Then DoCmd.Close produces an error
because combo107 is closed so the error routine is fired which includes
a dispaly error line which is what I am seeing. By commenting out '
MsgBox Err.Description I no longer get the error message. So now it
exits fine.

What remains an issue is why I cannot set a default on combo131 so I
can therefore eliminate the form activate event that marshall is
uncomfortable about.
 
M

Marshall Barton

Andre said:
I thought it would be easier to do some screen shots so I have.

http://ccgi.andre02.plus.com/wordpress/?p=86.

Sorry if the layout is poor but I did this quite hastily. Please
wander around the blog whist there :)

I hope this helps to solve this puzzle. Along with my total probnlem
which you have been helpign me with this is the only other problem at
mo and it has been bothering me and more so my users for some time
now.


After looking at your screen shots, I don't see why setting
combo131's DafaultValue property to 10 will not work. (I
see that 10 is the bound column's value for the QQ code,
regardless of whether it's the 10th item in the list or not)

Some other thoughts:
1) To avoid damaging an existing record, remove these lines
from the Activate event.

If Combo128 = "T" Then
[VISIT-CODE-ID] = 10
Combo131 = 10
End If

2) Remember that the DefaultValue only gets applied at the
time of the first keystroke that enters data in any bound
control **on a NEW record**. If you were expecting more
than this, then maybe that's the source of all this
confusion?

3) If you want to have the value of [VISIT-CODE-ID] set to
10 whenever "T" is selected in combo128 regardless of
whether this a new record or or an existing record, then use
combo128's AfterUpdate event with the line:
Combo131 = 10

4) For all our sakes, please use more meaningful names for
your controls. The names combo128 and combo131 just don't
convey much in the way explaining the intent of the
controls. A common, but certainly not universal, convention
is to prefix the bound field's name with the type of
control, e.g. cboClientSeen and cboVisitCodeID.

5) Do not listen to Randy when he says I'm smarter, that's
just a load of hogwash. However, you should definitely
listen to his suggestions, especially when he asks for
clarifications before attempting to provide a technical
answer.
 
A

Andre C

2) Remember that the DefaultValue only gets applied at the
time of the first keystroke that enters data in any bound
control **on a NEW record**. If you were expecting more
than this, then maybe that's the source of all this
confusion?

And herein was the problem to a greater degree. Everything now works.
4) For all our sakes, please use more meaningful names for
your controls. The names combo128 and combo131 just don't
convey much in the way explaining the intent of the
controls. A common, but certainly not universal, convention
is to prefix the bound field's name with the type of
control, e.g. cboClientSeen and cboVisitCodeID.
I know this but am too lazy to impliment. Trouble is I design the form
and them do some code and then think about renaming the controls which
means I have to change code as well.

Some day I will learn
5) Do not listen to Randy when he says I'm smarter, that's
just a load of hogwash.

No comment!!!!

Andre
 

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

Top