How do you save a field?

  • Thread starter Claudette Hennessy
  • Start date
C

Claudette Hennessy

I have a form with txtFirstName and txtLastName. The client has asked to be
notified if the input folk enter a first and last name which is already in
the database.

The procedure below is working as is, however after the entry in txtLastName
is undone, I would like to move to the txtFirstName control and undo that
also.
I have tried DoCmd.GoToControl "txtFirstName" and then Me!txtFirstNameUndo,
but get runtime error message 2018, " I must save the field. " How?
DoCmd.Save does not list an acfield.

Private Sub txtLastName_BeforeUpdate(Cancel As Integer)

If (Not IsNull(DLookup("[LastName]", "tblStaff", "[LastName] ='" _
& Me!txtLastName & "'" & " AND [FirstName] ='" & Me!txtFirstName &
"'"))) Then
MsgBox "This person has already been entered in the database."
Cancel = True
Me!txtLastName.Undo

End If
End Sub

Any help appreciated..
 
J

Jeff L

If all you are trying to do is blank out the first name, then
Me.txtFirstName = "" will do it.

Hope that helps!
 
R

ruralguy via AccessMonster.com

Hi Claudette,
Have you just tried:

Me!txtLastName.Undo
Me!txtFirstName.Undo

The control does not need the focus to use this method.

If that does not work then try:

Me!txtLastName.Undo
Me!txtFirstName = Me!txtFirstName.OldValue

Claudette said:
I have a form with txtFirstName and txtLastName. The client has asked to be
notified if the input folk enter a first and last name which is already in
the database.

The procedure below is working as is, however after the entry in txtLastName
is undone, I would like to move to the txtFirstName control and undo that
also.
I have tried DoCmd.GoToControl "txtFirstName" and then Me!txtFirstNameUndo,
but get runtime error message 2018, " I must save the field. " How?
DoCmd.Save does not list an acfield.

Private Sub txtLastName_BeforeUpdate(Cancel As Integer)

If (Not IsNull(DLookup("[LastName]", "tblStaff", "[LastName] ='" _
& Me!txtLastName & "'" & " AND [FirstName] ='" & Me!txtFirstName &
"'"))) Then
MsgBox "This person has already been entered in the database."
Cancel = True
Me!txtLastName.Undo

End If
End Sub

Any help appreciated..
 
C

Claudette Hennessy

The focus in on txtLastName at the start of the procedure.

Me!txtLastName.Undo
Me!txtFirstName.Undo

blanks out txtLastName, and has no effect on txtFirstName.

Me!txtLastName.Undo
Me!txtFirstName = Me!txtFirstName.OldValue

generates error 438, object does not support this property or method.

Me!txtLastName = " ' generates error 2115, 'the function set to beforeupdate
is preventing Event Tracking from saving the field" ????

In any case, after txtLastName and txtFirstName are returned to " ", I want
to GoToControl txtFirstName so correct data can be input.

I thought this was going to be so simple.

ruralguy via AccessMonster.com said:
Hi Claudette,
Have you just tried:

Me!txtLastName.Undo
Me!txtFirstName.Undo
The control does not need the focus to use this method.

If that does not work then try:

Me!txtLastName.Undo
Me!txtFirstName = Me!txtFirstName.OldValue

Claudette said:
I have a form with txtFirstName and txtLastName. The client has asked to
be
notified if the input folk enter a first and last name which is already in
the database.

The procedure below is working as is, however after the entry in
txtLastName
is undone, I would like to move to the txtFirstName control and undo that
also.
I have tried DoCmd.GoToControl "txtFirstName" and then
Me!txtFirstNameUndo,
but get runtime error message 2018, " I must save the field. " How?
DoCmd.Save does not list an acfield.

Private Sub txtLastName_BeforeUpdate(Cancel As Integer)

If (Not IsNull(DLookup("[LastName]", "tblStaff", "[LastName] ='" _
& Me!txtLastName & "'" & " AND [FirstName] ='" & Me!txtFirstName &
"'"))) Then
MsgBox "This person has already been entered in the database."
Cancel = True
Me!txtLastName.Undo

End If
End Sub

Any help appreciated..
 
R

ruralguy via AccessMonster.com

I take it these are UNBOUND TextBoxes. You might try Me.UnDo in the
*AfterUpdate* event of the txtLastName control rather than the BeforeUpdate
event and then use a Me!txtFirstName.SetFocus to move back to the other
control.

Claudette said:
The focus in on txtLastName at the start of the procedure.

Me!txtLastName.Undo
Me!txtFirstName.Undo

blanks out txtLastName, and has no effect on txtFirstName.

Me!txtLastName.Undo
Me!txtFirstName = Me!txtFirstName.OldValue

generates error 438, object does not support this property or method.

Me!txtLastName = " ' generates error 2115, 'the function set to beforeupdate
is preventing Event Tracking from saving the field" ????

In any case, after txtLastName and txtFirstName are returned to " ", I want
to GoToControl txtFirstName so correct data can be input.

I thought this was going to be so simple.
Hi Claudette,
Have you just tried:

Me!txtLastName.Undo
Me!txtFirstName.Undo
The control does not need the focus to use this method.
[quoted text clipped - 30 lines]
 
C

Claudette Hennessy

It worked ! Thank you. Here is the complete procedure:


Private Sub txtLastName_AfterUpdate()
If (Not IsNull(DLookup("[LastName]", "tblStaff", "[LastName] ='" _
& Me!txtLastName & "'" & " AND [FirstName] ='" & Me!txtFirstName &
"'"))) Then
MsgBox "This person has already been entered in the database."
Cancel = True
Me!txtLastName = " "
Me!txtFirstName.SetFocus
Me!txtFirstName = " "

End If
End Sub

Again, many thanks.
Claudette
ruralguy via AccessMonster.com said:
I take it these are UNBOUND TextBoxes. You might try Me.UnDo in the
*AfterUpdate* event of the txtLastName control rather than the
BeforeUpdate
event and then use a Me!txtFirstName.SetFocus to move back to the other
control.

Claudette said:
The focus in on txtLastName at the start of the procedure.

Me!txtLastName.Undo
Me!txtFirstName.Undo

blanks out txtLastName, and has no effect on txtFirstName.

Me!txtLastName.Undo
Me!txtFirstName = Me!txtFirstName.OldValue

generates error 438, object does not support this property or method.

Me!txtLastName = " ' generates error 2115, 'the function set to
beforeupdate
is preventing Event Tracking from saving the field" ????

In any case, after txtLastName and txtFirstName are returned to " ", I
want
to GoToControl txtFirstName so correct data can be input.

I thought this was going to be so simple.
Hi Claudette,
Have you just tried:

Me!txtLastName.Undo
Me!txtFirstName.Undo
The control does not need the focus to use this method.
[quoted text clipped - 30 lines]
Any help appreciated..
 
R

ruralguy via AccessMonster.com

That is great Claudette! By the way, you do *not* need Cancel = True in the
AfterUpdate event. I would also recommend initializing your TextBoxes to
ZeroLengthStrings (ZLS) rather than a single space. In other words Me!
txtLastName = "" rather than Me!txtlastName = " ".

Claudette said:
It worked ! Thank you. Here is the complete procedure:

Private Sub txtLastName_AfterUpdate()
If (Not IsNull(DLookup("[LastName]", "tblStaff", "[LastName] ='" _
& Me!txtLastName & "'" & " AND [FirstName] ='" & Me!txtFirstName &
"'"))) Then
MsgBox "This person has already been entered in the database."
Cancel = True
Me!txtLastName = " "
Me!txtFirstName.SetFocus
Me!txtFirstName = " "

End If
End Sub

Again, many thanks.
Claudette
I take it these are UNBOUND TextBoxes. You might try Me.UnDo in the
*AfterUpdate* event of the txtLastName control rather than the
[quoted text clipped - 35 lines]
 
D

Douglas J. Steele

If that code actually works for Claudette, it implies to me that she's not
using Option Explicit, since Cancel hasn't been declared in that routine.

Claudette: make sure that your class module has Option Explicit as its first
line (or it can be the 2nd line, after the Option Compare Database line). To
have this automatically happen, select Tools | Options while in the VB
Editor, and go to the Module tab. Make sure the checkbox "Require Variable
Declaration" is checked.

The reason for this is that forcing you to declare all variables will
prevent the kind of errors that occur if you've got code like:

intLoop = 1
Do While intLoop < 10
' do something here
intLoo = intLoop + 1
Loop

You'd have an infinite loop there, since intLoop's value never gets
incremented because of the typo.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


ruralguy via AccessMonster.com said:
That is great Claudette! By the way, you do *not* need Cancel = True in
the
AfterUpdate event. I would also recommend initializing your TextBoxes to
ZeroLengthStrings (ZLS) rather than a single space. In other words Me!
txtLastName = "" rather than Me!txtlastName = " ".

Claudette said:
It worked ! Thank you. Here is the complete procedure:

Private Sub txtLastName_AfterUpdate()
If (Not IsNull(DLookup("[LastName]", "tblStaff", "[LastName] ='" _
& Me!txtLastName & "'" & " AND [FirstName] ='" & Me!txtFirstName &
"'"))) Then
MsgBox "This person has already been entered in the database."
Cancel = True
Me!txtLastName = " "
Me!txtFirstName.SetFocus
Me!txtFirstName = " "

End If
End Sub

Again, many thanks.
Claudette
I take it these are UNBOUND TextBoxes. You might try Me.UnDo in the
*AfterUpdate* event of the txtLastName control rather than the
[quoted text clipped - 35 lines]
Any help appreciated..
 
R

ruralguy via AccessMonster.com

I couldn't agree more Doug. Claudette, when you add the Option Explicit to
your Form module it will cause an error if you use a variable in a subroutine
and haven't declared it yet. Just go through and add Dim statements to the
SubRoutines that need them and thise errors will go away and you will be fine.

If that code actually works for Claudette, it implies to me that she's not
using Option Explicit, since Cancel hasn't been declared in that routine.

Claudette: make sure that your class module has Option Explicit as its first
line (or it can be the 2nd line, after the Option Compare Database line). To
have this automatically happen, select Tools | Options while in the VB
Editor, and go to the Module tab. Make sure the checkbox "Require Variable
Declaration" is checked.

The reason for this is that forcing you to declare all variables will
prevent the kind of errors that occur if you've got code like:

intLoop = 1
Do While intLoop < 10
' do something here
intLoo = intLoop + 1
Loop

You'd have an infinite loop there, since intLoop's value never gets
incremented because of the typo.
That is great Claudette! By the way, you do *not* need Cancel = True in
the
[quoted text clipped - 24 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