Copy a value of text box to another text box

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hello to everybody.
If possible I would like to copy a text value to a text box (CellInfo) to
another (PositionInfo) text box if the value of the last one is 0 (default
value in the table) and
add a message to the user like "Please add a number to CellInfo field" if
"PositionInfo" has no data.

I tried with this few line of VBA, but doesn't work at all.
Private Sub PositionInfo_AfterUpdate()
If Me.PositionInfo = "0" Then
Me.PositionInfo = Me.CellInfo
End If
End Sub

Thanks and regards
John
 
The AfterUpdate event for a textbox only fires when a user actually enters
something in the textbox. If I'm understanding your post correctly, in this
case the user has NOT entered anything in the textbox (which is why you want
to display the message).

Usually in a situation like this, I put the test in the BeforeUpdate event
for the FORM. If my "error" condition winds up being true (PositionInfo =
0), then I display the message and cancel the update so that the user can go
back and make the entry.

Also, your code is testing for a value of "0", but in the text of your
description you say that the database default is 0. Those are two very
different things. "0" is a text character zero (hex 30) and 0 is the number
zero (hex 00). Which value is it that is the default in your table?

Hope this helps.

JP
 
The code you have should work, but where you've placed it won't. You've
placed it in the AfterUpdate event of PositionInfo, the textbox you're
trying to set the value in. The AfterUpdate event will run after the user
changes the value in the textbox, since that hasn't been done yet, the code
doesn't run. You will need to decide when you want this code to run. That
will tell you where it should be placed. A possible location would be the
AfterUpdate event of the CellInfo textbox. When the user makes an entry in
CellInfo, if the value in PositionInfo is zero, then it would update
PositionInfo with the value just entered in CellInfo.

To add he notice to the user, you would add a Msgbox to your code. You may
also want to add a SetFocus to move the cursor to PositionInfo.

Example:
Private Sub CellInfo_AfterUpdate()
If Me.PositionInfo = "0" Then
Me.PositionInfo = Me.CellInfo
MsgBox "You also need to make an entry in PositionInfo!", vbOkOnly +
vbInformation
Me.PositionInfo.SetFocus
 
Dear All,
let me write a THANK YOU VERY MUCH to both of you for a very professional
explanation and why not: a quick course also.

Kind regards
John
 
Back
Top