GoToControl problems

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

Guest

Greetings.

I have the following code, which tells the user that if the date is less
than the date in another field, 1) notify the user, 2) erase the incorrect
date and then 3) place the cursor back in the field to enter the correct
date. I can do steps 1 and 2, but for some reason step 3 will not work. Any
idea why it will not let me send the cursor to a control? I think I have
done it all correctly but I keep getting nowhere? I am thinking it is
because it is in an afterupdate event, but that just doesn't make sense to
me. Any thoughts would be highly appreciated. The code follows:

Private Sub txtDate_rcp_AfterUpdate()
Dim MoNo As String
Dim Yr As String

MoNo = DatePart("m", Me.txtDate_rcp)
Yr = DatePart("yyyy", Me.txtDate_rcp)
Me.txtMonth_rcp = MoNo
Me.txtYear_rcp = Yr

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " &
vbCrLf & _
"You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
DoCmd.GoToControl "txtDate_rcp"
End If

End Sub
 
Try replacing DoCmd.GoToControl "txtDate_rcp" with Me![txtYear_rcp].SetFocus
 
I can't find any info on it, but I think I recall there is an issue with
setting the focus while in a control to itself, but I am not absolutely sure.
In any case, this code really should be in the Before Update event. Then
you can use the Cancel capability of that event to cancel the update of the
control and keep the cursor in the control. See if this works for you:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Cancel = True
End If

'Note This works regardless of the value in Me.txtDate because if it is Null
'It Will Return Null
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)

End Sub

Notice the indentation in the code. Doesn't it make it easier to read?
 
I did try that and still had problems. My work around it ended up being
telling it to go to another control first and then back to the date control:


docmd.gotocontrol "txtMonth_rcp"
docmd.gotocontrol "txtDate_rcp"

I am not sure why it works that way though, but somehow it did.



Douglas J Steele said:
Try replacing DoCmd.GoToControl "txtDate_rcp" with Me![txtYear_rcp].SetFocus

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jessica said:
Greetings.

I have the following code, which tells the user that if the date is less
than the date in another field, 1) notify the user, 2) erase the incorrect
date and then 3) place the cursor back in the field to enter the correct
date. I can do steps 1 and 2, but for some reason step 3 will not work. Any
idea why it will not let me send the cursor to a control? I think I have
done it all correctly but I keep getting nowhere? I am thinking it is
because it is in an afterupdate event, but that just doesn't make sense to
me. Any thoughts would be highly appreciated. The code follows:

Private Sub txtDate_rcp_AfterUpdate()
Dim MoNo As String
Dim Yr As String

MoNo = DatePart("m", Me.txtDate_rcp)
Yr = DatePart("yyyy", Me.txtDate_rcp)
Me.txtMonth_rcp = MoNo
Me.txtYear_rcp = Yr

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " &
vbCrLf & _
"You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
DoCmd.GoToControl "txtDate_rcp"
End If

End Sub
 
What is the difference between the Before Update and the After Update
property? I didn't realize you could use the Before Update event in the way
you are describing.

I tried what you wrote but, it gave me an error message when I typed in an
invalid date that said:

The macro or function set to the BeforeUpdate or ValidationRule property for
this field is preventing Microsoft Access from saving the data in the field.

It then highlights this part of the code:

Me![txtDate_rcp] = Null

Any thoughts on how to get around this?

Jessica


Klatuu said:
I can't find any info on it, but I think I recall there is an issue with
setting the focus while in a control to itself, but I am not absolutely sure.
In any case, this code really should be in the Before Update event. Then
you can use the Cancel capability of that event to cancel the update of the
control and keep the cursor in the control. See if this works for you:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Cancel = True
End If

'Note This works regardless of the value in Me.txtDate because if it is Null
'It Will Return Null
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)

End Sub

Notice the indentation in the code. Doesn't it make it easier to read?

Jessica said:
Greetings.

I have the following code, which tells the user that if the date is less
than the date in another field, 1) notify the user, 2) erase the incorrect
date and then 3) place the cursor back in the field to enter the correct
date. I can do steps 1 and 2, but for some reason step 3 will not work. Any
idea why it will not let me send the cursor to a control? I think I have
done it all correctly but I keep getting nowhere? I am thinking it is
because it is in an afterupdate event, but that just doesn't make sense to
me. Any thoughts would be highly appreciated. The code follows:

Private Sub txtDate_rcp_AfterUpdate()
Dim MoNo As String
Dim Yr As String

MoNo = DatePart("m", Me.txtDate_rcp)
Yr = DatePart("yyyy", Me.txtDate_rcp)
Me.txtMonth_rcp = MoNo
Me.txtYear_rcp = Yr

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " &
vbCrLf & _
"You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
DoCmd.GoToControl "txtDate_rcp"
End If

End Sub
 
The before update event code occurs before the entered value is updated to
the table field. The after update event code occurs after the value is
updated to the table field. That is why the before update event can be
canceled. It prevents the update from occuring if set to True.

As to the error, ooops!. I should have caught that. You will have to leave
the incorrect value in the text box. You can't assign a value from there.
(guess I shouldn't have left my brain in the car this morning).

So, here is an update with corrections:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Cancel = True
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
Else
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)
End If


End Sub

Jessica said:
What is the difference between the Before Update and the After Update
property? I didn't realize you could use the Before Update event in the way
you are describing.

I tried what you wrote but, it gave me an error message when I typed in an
invalid date that said:

The macro or function set to the BeforeUpdate or ValidationRule property for
this field is preventing Microsoft Access from saving the data in the field.

It then highlights this part of the code:

Me![txtDate_rcp] = Null

Any thoughts on how to get around this?

Jessica


Klatuu said:
I can't find any info on it, but I think I recall there is an issue with
setting the focus while in a control to itself, but I am not absolutely sure.
In any case, this code really should be in the Before Update event. Then
you can use the Cancel capability of that event to cancel the update of the
control and keep the cursor in the control. See if this works for you:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Cancel = True
End If

'Note This works regardless of the value in Me.txtDate because if it is Null
'It Will Return Null
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)

End Sub

Notice the indentation in the code. Doesn't it make it easier to read?

Jessica said:
Greetings.

I have the following code, which tells the user that if the date is less
than the date in another field, 1) notify the user, 2) erase the incorrect
date and then 3) place the cursor back in the field to enter the correct
date. I can do steps 1 and 2, but for some reason step 3 will not work. Any
idea why it will not let me send the cursor to a control? I think I have
done it all correctly but I keep getting nowhere? I am thinking it is
because it is in an afterupdate event, but that just doesn't make sense to
me. Any thoughts would be highly appreciated. The code follows:

Private Sub txtDate_rcp_AfterUpdate()
Dim MoNo As String
Dim Yr As String

MoNo = DatePart("m", Me.txtDate_rcp)
Yr = DatePart("yyyy", Me.txtDate_rcp)
Me.txtMonth_rcp = MoNo
Me.txtYear_rcp = Yr

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " &
vbCrLf & _
"You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
DoCmd.GoToControl "txtDate_rcp"
End If

End Sub
 
Thanks! That makes sense now and I can see a few other instances where I
would rather have the before update option.

Jessica


Klatuu said:
The before update event code occurs before the entered value is updated to
the table field. The after update event code occurs after the value is
updated to the table field. That is why the before update event can be
canceled. It prevents the update from occuring if set to True.

As to the error, ooops!. I should have caught that. You will have to leave
the incorrect value in the text box. You can't assign a value from there.
(guess I shouldn't have left my brain in the car this morning).

So, here is an update with corrections:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Cancel = True
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
Else
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)
End If


End Sub

Jessica said:
What is the difference between the Before Update and the After Update
property? I didn't realize you could use the Before Update event in the way
you are describing.

I tried what you wrote but, it gave me an error message when I typed in an
invalid date that said:

The macro or function set to the BeforeUpdate or ValidationRule property for
this field is preventing Microsoft Access from saving the data in the field.

It then highlights this part of the code:

Me![txtDate_rcp] = Null

Any thoughts on how to get around this?

Jessica


Klatuu said:
I can't find any info on it, but I think I recall there is an issue with
setting the focus while in a control to itself, but I am not absolutely sure.
In any case, this code really should be in the Before Update event. Then
you can use the Cancel capability of that event to cancel the update of the
control and keep the cursor in the control. See if this works for you:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Cancel = True
End If

'Note This works regardless of the value in Me.txtDate because if it is Null
'It Will Return Null
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)

End Sub

Notice the indentation in the code. Doesn't it make it easier to read?

:

Greetings.

I have the following code, which tells the user that if the date is less
than the date in another field, 1) notify the user, 2) erase the incorrect
date and then 3) place the cursor back in the field to enter the correct
date. I can do steps 1 and 2, but for some reason step 3 will not work. Any
idea why it will not let me send the cursor to a control? I think I have
done it all correctly but I keep getting nowhere? I am thinking it is
because it is in an afterupdate event, but that just doesn't make sense to
me. Any thoughts would be highly appreciated. The code follows:

Private Sub txtDate_rcp_AfterUpdate()
Dim MoNo As String
Dim Yr As String

MoNo = DatePart("m", Me.txtDate_rcp)
Yr = DatePart("yyyy", Me.txtDate_rcp)
Me.txtMonth_rcp = MoNo
Me.txtYear_rcp = Yr

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " &
vbCrLf & _
"You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
DoCmd.GoToControl "txtDate_rcp"
End If

End Sub
 
You can think of them like this:
Before Update = birth control pill
After Update = RU235
:)


Jessica said:
Thanks! That makes sense now and I can see a few other instances where I
would rather have the before update option.

Jessica


Klatuu said:
The before update event code occurs before the entered value is updated to
the table field. The after update event code occurs after the value is
updated to the table field. That is why the before update event can be
canceled. It prevents the update from occuring if set to True.

As to the error, ooops!. I should have caught that. You will have to leave
the incorrect value in the text box. You can't assign a value from there.
(guess I shouldn't have left my brain in the car this morning).

So, here is an update with corrections:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Cancel = True
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
Else
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)
End If


End Sub

Jessica said:
What is the difference between the Before Update and the After Update
property? I didn't realize you could use the Before Update event in the way
you are describing.

I tried what you wrote but, it gave me an error message when I typed in an
invalid date that said:

The macro or function set to the BeforeUpdate or ValidationRule property for
this field is preventing Microsoft Access from saving the data in the field.

It then highlights this part of the code:

Me![txtDate_rcp] = Null

Any thoughts on how to get around this?

Jessica


:

I can't find any info on it, but I think I recall there is an issue with
setting the focus while in a control to itself, but I am not absolutely sure.
In any case, this code really should be in the Before Update event. Then
you can use the Cancel capability of that event to cancel the update of the
control and keep the cursor in the control. See if this works for you:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Cancel = True
End If

'Note This works regardless of the value in Me.txtDate because if it is Null
'It Will Return Null
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)

End Sub

Notice the indentation in the code. Doesn't it make it easier to read?

:

Greetings.

I have the following code, which tells the user that if the date is less
than the date in another field, 1) notify the user, 2) erase the incorrect
date and then 3) place the cursor back in the field to enter the correct
date. I can do steps 1 and 2, but for some reason step 3 will not work. Any
idea why it will not let me send the cursor to a control? I think I have
done it all correctly but I keep getting nowhere? I am thinking it is
because it is in an afterupdate event, but that just doesn't make sense to
me. Any thoughts would be highly appreciated. The code follows:

Private Sub txtDate_rcp_AfterUpdate()
Dim MoNo As String
Dim Yr As String

MoNo = DatePart("m", Me.txtDate_rcp)
Yr = DatePart("yyyy", Me.txtDate_rcp)
Me.txtMonth_rcp = MoNo
Me.txtYear_rcp = Yr

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " &
vbCrLf & _
"You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
DoCmd.GoToControl "txtDate_rcp"
End If

End Sub
 
If you don't want to be locked onto the control until a
valid entry is made, then add the line:
Me.txtDate_rcp.Undo

The issue here is that users tend to enter anything that
will allow them to move on until they figure out what the
value is supposed to be, but then forget to come back and
fix the valid, but incorrect value.
--
Marsh
MVP [MS Access]
Thanks! That makes sense now and I can see a few other instances where I
would rather have the before update option.


Klatuu said:
The before update event code occurs before the entered value is updated to
the table field. The after update event code occurs after the value is
updated to the table field. That is why the before update event can be
canceled. It prevents the update from occuring if set to True.

As to the error, ooops!. I should have caught that. You will have to leave
the incorrect value in the text box. You can't assign a value from there.
(guess I shouldn't have left my brain in the car this morning).

So, here is an update with corrections:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Cancel = True
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
Else
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)
End If


End Sub

Jessica said:
What is the difference between the Before Update and the After Update
property? I didn't realize you could use the Before Update event in the way
you are describing.

I tried what you wrote but, it gave me an error message when I typed in an
invalid date that said:

The macro or function set to the BeforeUpdate or ValidationRule property for
this field is preventing Microsoft Access from saving the data in the field.

It then highlights this part of the code:

Me![txtDate_rcp] = Null

Any thoughts on how to get around this?

Jessica


:

I can't find any info on it, but I think I recall there is an issue with
setting the focus while in a control to itself, but I am not absolutely sure.
In any case, this code really should be in the Before Update event. Then
you can use the Cancel capability of that event to cancel the update of the
control and keep the cursor in the control. See if this works for you:

Private Sub txtDate_rcp_BeforeUpdate()

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " & _
vbCrLf & "You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Cancel = True
End If

'Note This works regardless of the value in Me.txtDate because if it is Null
'It Will Return Null
Me![txtMonth_rcp] = DatePart("m", Me.txtDate_rcp)
Me![txtYear_rcp] = ("yyyy", Me.txtDate_rcp)

End Sub

Notice the indentation in the code. Doesn't it make it easier to read?

:

Greetings.

I have the following code, which tells the user that if the date is less
than the date in another field, 1) notify the user, 2) erase the incorrect
date and then 3) place the cursor back in the field to enter the correct
date. I can do steps 1 and 2, but for some reason step 3 will not work. Any
idea why it will not let me send the cursor to a control? I think I have
done it all correctly but I keep getting nowhere? I am thinking it is
because it is in an afterupdate event, but that just doesn't make sense to
me. Any thoughts would be highly appreciated. The code follows:

Private Sub txtDate_rcp_AfterUpdate()
Dim MoNo As String
Dim Yr As String

MoNo = DatePart("m", Me.txtDate_rcp)
Yr = DatePart("yyyy", Me.txtDate_rcp)
Me.txtMonth_rcp = MoNo
Me.txtYear_rcp = Yr

If Me![txtDate_rcp] < me![txtDate_org] Then
MsgBox "This date is before than the original tagged date. " &
vbCrLf & _
"You need to enter a valid date.", vbOKOnly, "Date Error"
Me![txtDate_rcp] = Null
Me![txtMonth_rcp] = Null
Me![txtYear_rcp] = Null
DoCmd.GoToControl "txtDate_rcp"
End If

End Sub
 
Back
Top