Small Code Not Working...

Ashfaque

Search Beautiful Girls from your town for night
Local time
Today, 13:58
Joined
Sep 6, 2004
Messages
897
Hi,

Is there any specific reason that below is not woring in On Current event of a form?

Basically I want few buttons to disable if the name in TxtRectEnable text box is not = SSNP.

This SSNP name is coming from a login form called "FrmLogIn" upon closing. I mean the name is being copied to F_Main form where the below code is written.

And when I see the name in appearing in TxtRectEnable text box on F_Main form but below code is not working. When I trapped using F8 keys the
TxtRectEnable text box is null becasue till that movement form dont open. But when it opens I see the user name TxtRectEnable text box

Private Sub Form_Current()
Me.Refresh
If TxtRectEnable = "SSNP" Then
Me.CmdRectNew.Enabled = True
Else
Me.CmdRectNew.Enabled = False
End If
End Sub

Please this looks funny to me.....

Any thought...

Regards,
Ashfaque
 
Try this:
Code:
Private Sub Form_Current()
Me.Refresh
If TxtRectEnable[COLOR="DarkRed"][B].value[/B][/COLOR] = "SSNP" Then
Me.CmdRectNew.Enabled = True
Else
Me.CmdRectNew.Enabled = False
End If
End Sub
 
The value property of a control is not updated until the record is saved. If I have understood your question you might need to use the Text property of the control which returns what is currently shown in the textbox before the record is saved.

Also note that all objects should be fully referenced by including Me.

The whole If section can be shortend to:
Code:
Me.CmdRectNew.Enabled = (Me.TxtRectEnable.Text = "SSNP")

For multiple buttons:

Code:
With Me
   boolVariable = (.TxtRectEnable.Text = "SSNP" )
   .CmdRectNew.Enabled = boolVariable
   .Anotherbutton.Enabled = boolVariable
   etc
End With

BTW It is not necessary to specify the Value property becasue it is the default property of a control in VBA.
 
Thanks,

I used your method Galaxi but it gave me below;

Run-time error 2185.
You can't reference a propert or method for a control unless the control has focus.

There fore set the control as below for 2 cmd btn to disable. But even there diff user name, it keeps btns disable. Another thing I need to ask you, how about otehr conditional part? I if the condition is false or true then how to make it disable / enable.

Private Sub Form_Current()
Me.Refresh
Me.TxtRectEnable.SetFocus
With Me
boolVariable = (.TxtRectEnable.Text = "Hat Jef")
.CmdRectNew.Enabled = boolVariable
.CmdRectReport.Enabled = boolVariable
End With
End Sub
 
2 things....

1. Do you think no need to define boolVariable anywhere ?
2. I can not set focus on TxtRectEnable in future bcz I want it to be hidden.

Regards,
Ashfaque
 
If you are going to hide the textbox then you need to set a global variable based on the value before the login form closes. Then set the Enabled property based on that variable.

Yes the variable should be instantiated. I didn't include that step as it is routine. Since it needs to be a global then it will definitely need to be defined as a Public variable in a Standard Module.
 
Galaxi,

I never did this way before, can you please let me know how to do it?

Do you want me a new model to create like

Public Function MyUser() As ???


End Function

But never used this method..please help..

Thanks,
 
A Global variable is a variable declared Public in any Standard Module (not a Form Class Module). Its scope is the whole Project so its value can be written or read from any sub or function at any time.

An alterntative to the Global variable to pass the boolean to the new form is the OpenArgs argument of the OpenForm Command. It sets the OpenArgs Property of the form. Many developers prefer this and I would recommend it.

Code:
boolVariable = (.TxtRectEnable.Text = "Hat Jef")
In this expression in the parentheses should return True or False letting the value to the variable which is then subsequently used to let the Enabled property of the controls.
 

Users who are viewing this thread

Back
Top Bottom