BeforeUpdate event

  • Thread starter vbnetman via AccessMonster.com
  • Start date
V

vbnetman via AccessMonster.com

Good morning,
After validating data in the BeforeUpdate event of a 2 field form, I want to
close the form if the data is good or, if the data is bad, keep the form open
and prompt the user to make a change. For some reason I’m having a tough time
figuring out how to do this with something like an ‘OK’ button. Could someone
lend a hand?

Thank you
 
K

kingston via AccessMonster.com

Try using the AfterUpdate event and something like this:

If Me.Field1 = Bad Then
Msgbox Me.Field1 & " is not a valid value."
Me.Field1 = Null (or Me.Field1.OldValue)
Else
DoCmd.Close
End If

You'll have to modify this to work with two values but I think it'll do what
you want. Just don't allow cycling to all records in the form so that the
record stays current.
 
V

vbnetman via AccessMonster.com

Hello Kingston,
Thank you for the response. Yes, but I want to catch bad data before it
updates a table
kingston said:
Try using the AfterUpdate event and something like this:

If Me.Field1 = Bad Then
Msgbox Me.Field1 & " is not a valid value."
Me.Field1 = Null (or Me.Field1.OldValue)
Else
DoCmd.Close
End If

You'll have to modify this to work with two values but I think it'll do what
you want. Just don't allow cycling to all records in the form so that the
record stays current.
Good morning,
After validating data in the BeforeUpdate event of a 2 field form, I want to
[quoted text clipped - 4 lines]
Thank you
 
K

kingston via AccessMonster.com

If each field can be validated by itself, use the field's validation rule at
the table level or the control's validation rule at the form level.
Otherwise if both fields have to be validated together, to prevent the table
from being updated at all (even for a little while by using the AfterUpdate
event), I think you'll have to create two unbound textboxes. That way, you
can validate both of them and then add them to the table in a procedure using
the AfterUpdate event. IOW, I don't think that you'll be able to use the
BeforeUpdate event on two controls simultaneously (one field is going to be
updated before the focus can be moved to the second field).
Hello Kingston,
Thank you for the response. Yes, but I want to catch bad data before it
updates a table
Try using the AfterUpdate event and something like this:
[quoted text clipped - 14 lines]
 
V

vbnetman via AccessMonster.com

Both fields work together and it is their combination that I need to validate.
Exactly as you say, one field needs to be saved before moving on. The
workaround I did was to validate both fields in the BeforeUpdate event of the
second field. If it does not validate, I undo it and used code to close the
form then immediately re-open it setting the focus to the first field on load.
This clears the textboxes and the user proceeds normally. The problem is the
trigger…what triggers the event? For example, a user enters data in field 1
then tabs to field 2and enters data…here’s where I’m hung up…I’ve got a
flashing cursor in field 2…now what? Do I stick an OK button in there and if
so what’s the pseudocode behind the button? The validation will occur right
when I leave field 2.

Thank you

If each field can be validated by itself, use the field's validation rule at
the table level or the control's validation rule at the form level.
Otherwise if both fields have to be validated together, to prevent the table
from being updated at all (even for a little while by using the AfterUpdate
event), I think you'll have to create two unbound textboxes. That way, you
can validate both of them and then add them to the table in a procedure using
the AfterUpdate event. IOW, I don't think that you'll be able to use the
BeforeUpdate event on two controls simultaneously (one field is going to be
updated before the focus can be moved to the second field).
Hello Kingston,
Thank you for the response. Yes, but I want to catch bad data before it
[quoted text clipped - 4 lines]
 
V

vbnetman via AccessMonster.com

-also, I tried the unbound textbox idea but ran into simialr problems. After
the textboxes were populated, I did an SQL statement "Insert into...". Unless
the first record was saved I could not proceed to the next SQL statement for
the second textbox.
If each field can be validated by itself, use the field's validation rule at
the table level or the control's validation rule at the form level.
Otherwise if both fields have to be validated together, to prevent the table
from being updated at all (even for a little while by using the AfterUpdate
event), I think you'll have to create two unbound textboxes. That way, you
can validate both of them and then add them to the table in a procedure using
the AfterUpdate event. IOW, I don't think that you'll be able to use the
BeforeUpdate event on two controls simultaneously (one field is going to be
updated before the focus can be moved to the second field).
Hello Kingston,
Thank you for the response. Yes, but I want to catch bad data before it
[quoted text clipped - 4 lines]
 
K

kingston via AccessMonster.com

Create two unbound textboxes. Use the AfterUpdate event on each one. The
logic should go as such:

TextBox1_AfterUpdate
If IsNull(Me.TextBox2) Then
Nothing or save the value if you want
Else
If Me.TextBox1=Valid and Me.TextBox2=Valid Then
Update Table
Else
Clear values and tell user to try again
End If
End If

Use something similar for TextBox2_AfterUpdate
-also, I tried the unbound textbox idea but ran into simialr problems. After
the textboxes were populated, I did an SQL statement "Insert into...". Unless
the first record was saved I could not proceed to the next SQL statement for
the second textbox.
If each field can be validated by itself, use the field's validation rule at
the table level or the control's validation rule at the form level.
[quoted text clipped - 11 lines]
 
V

vbnetman via AccessMonster.com

Kingston,
Thank you, I will give it a shot. You've helped tremendously!
Create two unbound textboxes. Use the AfterUpdate event on each one. The
logic should go as such:

TextBox1_AfterUpdate
If IsNull(Me.TextBox2) Then
Nothing or save the value if you want
Else
If Me.TextBox1=Valid and Me.TextBox2=Valid Then
Update Table
Else
Clear values and tell user to try again
End If
End If

Use something similar for TextBox2_AfterUpdate
-also, I tried the unbound textbox idea but ran into simialr problems. After
the textboxes were populated, I did an SQL statement "Insert into...". Unless
[quoted text clipped - 6 lines]
 

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