473,408 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Clear a text box after event cancelled.

I have a form where one of the entries is "Contact". On BeforeUpdate
I check the table for the existance of that Contact. A mesage box
informs the user that the name is already in the table and cancels the
event. What I want to do is clear the text from the text box to allow
the user to enter another name without deleting it.

I have tried to set focus, set value to "" etc. but nothing works.

Any help is appreciated.

Thank you.
Nov 13 '05 #1
6 8982
On Sun, 16 Oct 2005 18:32:42 -0400, Shyguy <sh****@aol.com> wrote:
I have a form where one of the entries is "Contact". On BeforeUpdate
I check the table for the existance of that Contact. A mesage box
informs the user that the name is already in the table and cancels the
event. What I want to do is clear the text from the text box to allow
the user to enter another name without deleting it.

I have tried to set focus, set value to "" etc. but nothing works.

Any help is appreciated.

Thank you.


Access makes this really annoyingly difficult. Here's what I do...

Private mblnResetContact

Private Sub txtContact_BeforeUpdate(Cancel As Integer)

mblnResetContact = False
If Not CheckContactOK() then
mblnResetContact = True
End If

End Sub

Private Sub txtContact_AfterUpdate()

If mblnResetContact then
Me!txtContact = Null
mblnResetContact = False
endif

End Sub
Nov 13 '05 #2
On Sun, 16 Oct 2005 16:02:53 -0700, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Sun, 16 Oct 2005 18:32:42 -0400, Shyguy <sh****@aol.com> wrote:
I have a form where one of the entries is "Contact". On BeforeUpdate
I check the table for the existance of that Contact. A mesage box
informs the user that the name is already in the table and cancels the
event. What I want to do is clear the text from the text box to allow
the user to enter another name without deleting it.

I have tried to set focus, set value to "" etc. but nothing works.

Any help is appreciated.

Thank you.


Access makes this really annoyingly difficult. Here's what I do...

Private mblnResetContact

Private Sub txtContact_BeforeUpdate(Cancel As Integer)

mblnResetContact = False
If Not CheckContactOK() then
mblnResetContact = True
End If

End Sub

Private Sub txtContact_AfterUpdate()

If mblnResetContact then
Me!txtContact = Null
mblnResetContact = False
endif

End Sub

Thanks for the reply. Unfortunately I am totally lost as to how to
use this code. I tried pasting it into my code but I get an error
"sub or function not defined for "CheckContactOK" and I don't have a
clue as to where "Private mblnResetContact" would go.
Nov 13 '05 #3
On Mon, 17 Oct 2005 17:08:30 -0400, Shyguy <sh****@aol.com> wrote:
On Sun, 16 Oct 2005 16:02:53 -0700, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Sun, 16 Oct 2005 18:32:42 -0400, Shyguy <sh****@aol.com> wrote:
I have a form where one of the entries is "Contact". On BeforeUpdate
I check the table for the existance of that Contact. A mesage box
informs the user that the name is already in the table and cancels the
event. What I want to do is clear the text from the text box to allow
the user to enter another name without deleting it.

I have tried to set focus, set value to "" etc. but nothing works.

Any help is appreciated.

Thank you.


Access makes this really annoyingly difficult. Here's what I do...

Private mblnResetContact

Private Sub txtContact_BeforeUpdate(Cancel As Integer)

mblnResetContact = False
If Not CheckContactOK() then
mblnResetContact = True
End If

End Sub

Private Sub txtContact_AfterUpdate()

If mblnResetContact then
Me!txtContact = Null
mblnResetContact = False
endif

End Sub

Thanks for the reply. Unfortunately I am totally lost as to how to
use this code. I tried pasting it into my code but I get an error
"sub or function not defined for "CheckContactOK" and I don't have a
clue as to where "Private mblnResetContact" would go.


CheckContactOK was the name I made up for the function you would have that
checks to see if contact data is OK, reports the error if not, and returns
True for OK, False for not OK. You didn't show us your code for checking the
condition, so I invented a function name to refer to "your code".

mblnResetContact is declared as private, so it must appear before the first
procedure definition in your code module. "Private" means it is accessible to
all procedures in the module, and not to any code outside the module.
Nov 13 '05 #4
On Mon, 17 Oct 2005 20:02:53 -0700, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Mon, 17 Oct 2005 17:08:30 -0400, Shyguy <sh****@aol.com> wrote:
On Sun, 16 Oct 2005 16:02:53 -0700, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Sun, 16 Oct 2005 18:32:42 -0400, Shyguy <sh****@aol.com> wrote:

I have a form where one of the entries is "Contact". On BeforeUpdate
I check the table for the existance of that Contact. A mesage box
informs the user that the name is already in the table and cancels the
event. What I want to do is clear the text from the text box to allow
the user to enter another name without deleting it.

I have tried to set focus, set value to "" etc. but nothing works.

Any help is appreciated.

Thank you.

Access makes this really annoyingly difficult. Here's what I do...

Private mblnResetContact

Private Sub txtContact_BeforeUpdate(Cancel As Integer)

mblnResetContact = False
If Not CheckContactOK() then
mblnResetContact = True
End If

End Sub

Private Sub txtContact_AfterUpdate()

If mblnResetContact then
Me!txtContact = Null
mblnResetContact = False
endif

End Sub

Thanks for the reply. Unfortunately I am totally lost as to how to
use this code. I tried pasting it into my code but I get an error
"sub or function not defined for "CheckContactOK" and I don't have a
clue as to where "Private mblnResetContact" would go.


CheckContactOK was the name I made up for the function you would have that
checks to see if contact data is OK, reports the error if not, and returns
True for OK, False for not OK. You didn't show us your code for checking the
condition, so I invented a function name to refer to "your code".

mblnResetContact is declared as private, so it must appear before the first
procedure definition in your code module. "Private" means it is accessible to
all procedures in the module, and not to any code outside the module.

I didn't use a function. This is the code I used.

--------
Private Sub Contact_BeforeUpdate(Cancel As Integer)

On Error GoTo Contact_BeforeUpdate_Err

If (Eval("DLookUp(""[Contact]"",""[Contacts]"",""[Contact] =
Form.[Contact] "") Is Not Null")) Then

Beep

MsgBox "The Contact you entered already exists on Database.",
vbInformation, "Duplicate Contact"

DoCmd.CancelEvent

End If

Contact_BeforeUpdate_Exit:
Exit Sub

Contact_BeforeUpdate_Err:
MsgBox Error$
Resume Contact_BeforeUpdate_Exit
End Sub
--------

Can I make this as a function?
Nov 13 '05 #5
On Mon, 17 Oct 2005 23:12:50 -0400, Shyguy <sh****@aol.com> wrote:

....
I didn't use a function. This is the code I used.

--------
Private Sub Contact_BeforeUpdate(Cancel As Integer)

On Error GoTo Contact_BeforeUpdate_Err

If (Eval("DLookUp(""[Contact]"",""[Contacts]"",""[Contact] =
Form.[Contact] "") Is Not Null")) Then

Beep

MsgBox "The Contact you entered already exists on Database.",
vbInformation, "Duplicate Contact"

DoCmd.CancelEvent

End If

Contact_BeforeUpdate_Exit:
Exit Sub

Contact_BeforeUpdate_Err:
MsgBox Error$
Resume Contact_BeforeUpdate_Exit
End Sub
--------

Can I make this as a function?


The simple answer to your question is "yes".

Next, I'd want to make some style comments
1. DoCmd.CancelEvent is an obsolete style that harkens back to Access 2.0.
That's what the Cancel argument is for - you let Cancel = True to cancel an
event. Of course, that won't matter to us in this case, because to change the
value, we have to -not- cancel the event.
2. In Access 95 and above, there is an Err object with properties, including a
Description property. Using Error$ is another throw-back to Access 2.0.
3. That Eval statement is a bit wierd. It took me a minute to figure out why
it's done that way, but I guess it's a clever way to get "Form.[Contact]" to
be evaluated without writing a lot of extra code. I'd use single quotes
inside the Eval string, though, because it's easier to read and maintain.

Here's what the finished code might look like...

'These 2 statements normally appear at the top of every module.
Option Explicit
Option Compare Database

Private mblnResetContact As Boolean

Private Sub txtContact_BeforeUpdate(Cancel As Integer)

mblnResetContact = False
If Not CheckContactOK() then
mblnResetContact = True
End If

End Sub

Private Sub txtContact_AfterUpdate()

If mblnResetContact then
Me!txtContact = Null
mblnResetContact = False
endif

End Sub

Private Function CheckContactOK() As Boolean
Dim blnContactOK As Boolean

On Error GoTo Err_Catch

Dim strLookupExpr As String
strLookupExpr = "DLookUp('[Contact]','[Contacts]','[Contact] = " & _
"Form.[Contact] ') Is Null"

blnContactOK = Eval(strLookupExpr)

If Not blnContactOK Then
Beep
MsgBox "The Contact you entered already exists on Database.", _
vbInformation+vbOkOnly, _
"Duplicate Contact"
End If

Proc_Final:
CheckContactOK = blnContactOK
Exit Function

Err_Catch:
MsgBox Err.Description, vbExclamation+vbOkOnly

'Don't second guess the user if the code is broken...
blnContactOK = True

Resume Proc_Final

End Sub

' ... Other form code goes here ...
Nov 13 '05 #6
On Mon, 17 Oct 2005 22:24:28 -0700, Steve Jorgensen
<no****@nospam.nospam> wrote:
On Mon, 17 Oct 2005 23:12:50 -0400, Shyguy <sh****@aol.com> wrote:

...
I didn't use a function. This is the code I used.

--------
Private Sub Contact_BeforeUpdate(Cancel As Integer)

On Error GoTo Contact_BeforeUpdate_Err

If (Eval("DLookUp(""[Contact]"",""[Contacts]"",""[Contact] =
Form.[Contact] "") Is Not Null")) Then

Beep

MsgBox "The Contact you entered already exists on Database.",
vbInformation, "Duplicate Contact"

DoCmd.CancelEvent

End If

Contact_BeforeUpdate_Exit:
Exit Sub

Contact_BeforeUpdate_Err:
MsgBox Error$
Resume Contact_BeforeUpdate_Exit
End Sub
--------

Can I make this as a function?


The simple answer to your question is "yes".

Next, I'd want to make some style comments
1. DoCmd.CancelEvent is an obsolete style that harkens back to Access 2.0.
That's what the Cancel argument is for - you let Cancel = True to cancel an
event. Of course, that won't matter to us in this case, because to change the
value, we have to -not- cancel the event.
2. In Access 95 and above, there is an Err object with properties, including a
Description property. Using Error$ is another throw-back to Access 2.0.
3. That Eval statement is a bit wierd. It took me a minute to figure out why
it's done that way, but I guess it's a clever way to get "Form.[Contact]" to
be evaluated without writing a lot of extra code. I'd use single quotes
inside the Eval string, though, because it's easier to read and maintain.

Here's what the finished code might look like...

'These 2 statements normally appear at the top of every module.
Option Explicit
Option Compare Database

Private mblnResetContact As Boolean

Private Sub txtContact_BeforeUpdate(Cancel As Integer)

mblnResetContact = False
If Not CheckContactOK() then
mblnResetContact = True
End If

End Sub

Private Sub txtContact_AfterUpdate()

If mblnResetContact then
Me!txtContact = Null
mblnResetContact = False
endif

End Sub

Private Function CheckContactOK() As Boolean
Dim blnContactOK As Boolean

On Error GoTo Err_Catch

Dim strLookupExpr As String
strLookupExpr = "DLookUp('[Contact]','[Contacts]','[Contact] = " & _
"Form.[Contact] ') Is Null"

blnContactOK = Eval(strLookupExpr)

If Not blnContactOK Then
Beep
MsgBox "The Contact you entered already exists on Database.", _
vbInformation+vbOkOnly, _
"Duplicate Contact"
End If

Proc_Final:
CheckContactOK = blnContactOK
Exit Function

Err_Catch:
MsgBox Err.Description, vbExclamation+vbOkOnly

'Don't second guess the user if the code is broken...
blnContactOK = True

Resume Proc_Final

End Sub

' ... Other form code goes here ...

OK. I have no idea what the code you send me does, but it works
great! It will take me a while to figure it out, and I'm sure I will
learn a lot from it.

Thank you so much for your help!!!
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: InvisibleDuncan | last post by:
I have a ListView that populates some fields whenever the user selects an item. However, if they change the data in the fields and then select a new item without saving, I want to display a message...
10
by: David | last post by:
Is there something that I can use to prevent the a user from dragging something, an image for instance. In IE I can use the ondrag = "return false;", is there a way to achieve the same way with...
7
by: MLH | last post by:
I tried the following code to prevent a checkbox from being updated (going from a value of Null to True, from True to False or from False to True). I was surprised it did not work. Can anyone...
2
by: akameswaran | last post by:
Admittedly this problem causes no actual functional issues aside from an occasional error message when the program exits. The error is: Unhandled exception in thread started by Error in...
4
by: Academic | last post by:
Does it make sense to put this If e.Cancel Then Exit Sub at the beginning of form closing events so if the user cancels the app's exiting in one Closing routine he will not be asked again by...
1
by: mailpitches | last post by:
X-No-Archive: yes a function, the return value of the function is a boolean. What does this boolean value mean? Example: <body onload="document.getElementById('field').onkeydown=function(x) {...
3
by: lesperancer | last post by:
I've got application A, with a hidden form, whose unload event is cancelled unless you use an 'exit' button (so I can do special processing) this works fine when using just application A and it...
6
by: MLH | last post by:
I have a form, frmUSPSReturnReceipts, with a control named NotExpectingGreenTickets. The control's Exit event procedure follows: Private Sub NotExpectingGreenTickets_Exit(Cancel As Integer) If...
5
by: Steve | last post by:
In VB6 I have a screen the has a text box and a datagrid If I click the datagrid, the 1st event is to validate the textbox and then the 2nd event is a datagrid mousedown event. If a get an error...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.