chosing the right event !

afnan_88

Registered User.
Local time
Today, 06:36
Joined
Jun 16, 2010
Messages
27
hello guys

i wanna write a code for the following condition

if the uesr keep txtboxA empty then txtboxB must equal 4



i tried this code in the afterupdate event <<i think its a wrong place :confused:

If CtltxtboxA.Text = "" Then
CtltxtboxB.Text= 4
end if
=============

also this one:

If CtltxtboxA.Text = Null Then
CtltxtboxB.Text = "4"
endif

but it doesnt work and no error msgs !

thanks in advance
 
On the afterupdate of the control

Code:
If trim(Me.CtltxtboxA & "")  = "" Then
  Me.CtltxtboxB= 4
end if
 
How can this be in the AfterUpdate event of either control? If text box A has nothing ever entered, its AfterUpdate event won't be firing. And what if Text Box B isn't given a value, it won't ever fire the AfterUpdate event.

So the best choice is to use the form's BEFORE UPDATE event to fix it, or notify the user and cancel the update, if there is a discrepancy just before the record is committed.
 
Bob

99% of the time I work with unbound forms so that is why I leaned to the after update event.

It could also go on the on current event of the form.
 
Bob

99% of the time I work with unbound forms so that is why I leaned to the after update event.
So how would you deal with it if none of them were filled in? Probably from some validation code in your save procedure, right? That's the bound form's Before Update event.

It could also go on the on current event of the form.

No it can't because when the On Current event fires it has the new field information in it not the previous record.
 
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Nz(Me.CtltxtboxA.Value, "") = "" Then
 Me.CtltxtboxB.Value = 4
Else
 Me.CtltxtboxB.Value = Null
End If
End Sub
Note that the Text Property is rarely used in Access VBA, as it requires that the Control have focus at the time it is being referenced. Instead the Value Property is used.

Since Value is the Default Property for Textboxes, it can actually be omitted altogether, making the code
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Nz(Me.CtltxtboxA, "") = "" Then
 Me.CtltxtboxB = 4
Else
 Me.CtltxtboxB = Null
End If
End Sub
This code also takes into account the possibility that the user may come back, at a later date, and enter data in CtltxtboxA, and resets CtltxtboxB to Null, in that eventuality. If this doesn't suit your requirements the Else clause can be modified.

Linq ;0)>
 
thanks Dcrake , boblarson , missingling for your discussions
its working now ..

i still have a small q
i usually pass a value between two forms using the openArgs :

and i receive it in the onLoad event of the 2ed form

Dim i as string
i = Me.OpenArgs


after that i don't know how to assign it to a control "text box"
i try to add this line : Ctlmytxtbox.txet=i
but i get an error that that i cant assign a value to that object or sothing like that i'm sure that the value is received in the variable " i " but the problem as i think in my last line ..
 
Last edited:
sorry but am new to VB what is the diffrence bt. Nz() & trim () ?
 
I assume that when you wrote

Ctlmytxtbox.txet=i

you actually meant

Ctlmytxtbox.text=i

As I told you in my first post, the Text Property is rarely used in Access VBA, as it requires that the Control have focus at the time it is being referenced! As I also said, use the Value Property instead.

The Trim () function strips leading and trailing spaces from a string. DCrake's code
Code:
If trim(Me.CtltxtboxA & "")  = "" Then
  Me.CtltxtboxB= 4
End if
is intended to do just that, then concatenate a Zero-Length String to what is then in the textbox, and if what remains is still a Zero-Length String assign the value 4 to the second textbox. As Bob pointed out, putting this code in the AfterUpdate event of the control won't work, because if nothing is entered the AfterUpdate event won't fire. Validation code designed to check whether or not a given control has data in it has to be placed in the Form_BeforeUpdate event! Placing it in any event tied to a Control is doomed to failure! To totally negate it, all the user has to do is not even enter the Control!

The Nz() function assigns a Value to a Control if the Control is Null, which is to say unpopulated.

Nz(Me.CtltxtboxA.Value, "")

looks at CtltxtboxA and if it holds no data, assigns a Zero-Length String ("") to it.

If Nz(Me.CtltxtboxA.Value, "") = "" Then

does that, and if the Value of CtltxtboxA is then "" it means that the Control was blank, i.e. it either held an empty string or it was Null.

Both DCrake's code and mine will work, but it has to be in the Form_BeforeUpdate event.

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom