Condition Button Visibility based on Two Control Values

Fozi

Registered User.
Local time
Today, 10:58
Joined
Feb 28, 2006
Messages
137
Hi

Trying to complete what I though would be a relatively simple task.

Got a form with two text boxes containing values - TotActivity and TotTime.

One the same form I've a Button called PrintRecord which I only want to display if both Text boxes are equal.

Currently I've entered

Private Sub Form_Current()
If Me.TotActivity = Me.TotTime Then
Me.BTN_PrintRecord.Visible = True
Else
Me.BTN_PrintRecord.Visible = False
End If
End Sub

Tried it with the .Value suffix also but to no avail. Any clues?

All help greatly appreciated

Thanks
Fozi:)
 
You may need to account for nulls:

If nz(Me.TotActivity) = nz(Me.TotTime) Then
 
Thanks for the reply Ken but doesn't seem to make a difference.

I keep getting a Runtime 2455 Error message saying

"You entered an expression that has an invalid reference to the property Form/Report"

Very strange
 
Thanks for the reply Ken but doesn't seem to make a difference.

I keep getting a Runtime 2455 Error message saying

"You entered an expression that has an invalid reference to the property Form/Report"

When I debug it takes me to the line:

If Me.TotActivity = Me.TotTime Then


Very strange
 
When you go to the debug line type in

Debug.print Me.TotActivity & vbTab & Me.TotTime

If the values look the same then are they both the same data type?

David
 
Couple of questions:

People sometimes "clean up" code before posting; is this the entire Form_Current sub?

What code, if any, do you have in the Form_Load and Form_Open subs? I ask this because Access is not always accurate in highlighting the offending lines of code.
 
Debug.print Me.TotActivity & vbTab & Me.TotTime

If the values look the same then are they both the same data type?


David

I'm not sure where this line is meant to be inserted. On clicking the Debug option it takes me to the offending line with a yellow bar over it. Where do I enter this text

Both Controls are set as the data type Fixed with 2 decimal places

Thanks
Fozi
 
Couple of questions:

People sometimes "clean up" code before posting; is this the entire Form_Current sub?

What code, if any, do you have in the Form_Load and Form_Open subs? I ask this because Access is not always accurate in highlighting the offending lines of code.


This is the only code I have in the Form_Current sub. In Form_Open I have a macro to open at a new record. There is nothing in Form_Load

Thanks for your help
Fozi
 
Insert the Debug.Print line above the offending line and set a breakpoint on it.
 
Hi David

done that but gives me little more info. On opening the form it take me straight to that line.

The value for the TotActivity displays if I hover over that control but nothing appears when I hover of TotTime.
 
That's the issue. Because this is happending prior to opening the form there may be only one side of the equasion. As the OnCurrent event is being processed before it actually displays the contents of the screen on the form load event. I suspect that the first record it is trying to display has one or both of the enties null or empty.

Code:
Private Sub Form_Current()
'First test for something in both fields

'Not adding the Trim() syntax traps blank or null fields

If Trim(Me.TotActivity & " ") <> "" And Trim(Me.TotTime & " ") <> ""  Then
   'Next test for matching items
    If Me.TotActivity = Me.TotTime Then
         Me.BTN_PrintRecord.Visible = True
    Else
         Me.BTN_PrintRecord.Visible = False
    End If
Else
    Me.BTN_PrintRecord.Visible = False
End If

End Sub
 
David

Thanks for your help yet again. Something you said triggered it for me. The form Open property was set to open on a new Record which was causing the problem. Removing that seemed to recify the situation.

Thanks
Fozi
 
Probably should have "triggered it for you" when I asked about code in the Form_Open sub 7 posts ago! :D
 
couldnt you use nz and just one line of code - for strings

Code:
BTN_PrintRecord.Visible = (nz(totactivity,vbnullstring) = nz(tottime,vbnullstring))

or maybe if they are numerics

Code:
BTN_PrintRecord.Visible = (nz(totactivity,0) = nz(tottime,0))
 
Hi folks

apologies but still not got this working as I'd like. Where I'm at..

  • I have two subforms on my parent form.
  • In each subform there is a calculated total.
  • In the parent form I have two controls referencing these totals. Namely TotActivity and TotTime
  • Where these totals are equal I want a Print Button on my parent form to display
  • Where not equal is should be invisible

So far I have got code entered only in the On_Current property of the parent form. All other code has been removed.
 
Hi

by way of an update. I've discovered that this works unless the two values ([TotActivity] and [TotTime]) end with ".00"!

Strange!
 
apologies but still not got this working as I'd like.

What doe this line mean? It works sometimes but not other? It doesn't work at all? It throws an error?

  • I have two subforms on my parent form.
  • In each subform there is a calculated total.
  • In the parent form I have two controls referencing these totals. Namely TotActivity and TotTime
You know, Fozi, it would have been an excellent idea to let us know that you were dealing with a form and two subforms rather than a simple form!

As I asked above, does it work sometimes but not others? Dealing with not one but two subforms, this may very well be a timing issue. It's probably worth while, in the main form's OnCurrent event to try , in the line

If Me.TotActivity <> Me.TotTime Then

replacing Me.TotActivity and Me.TotTime with the actual references to the subform flelds you use to populate these controls.

Also, have we ever determined that these fields are of the same datatype?
 
Missinglinq

Apologies for the abiguities which obviously frustrate. I will try to answer your questions in more detail now.

The two total controls on the parent form are both numeric controls, Fixed with 2 decimal places. These consistently add up to the correct totals.

The only function that is not working is where I require the Print Button to appear if these two totals are equal. At present the only time it appears is when I click to add a new record. On all other conditions - where equal or not, the button does not appear.

I share your opinion that it may be a time thing. Will try what you suggest an enter the line above referencing the subform controls directly.

Thanks
Fozi
 
Missingling

I entered the line you suggested but to no avail. Only on clicking for a new record where the two fields have a default value of zero does the button appear.
 

Users who are viewing this thread

Back
Top Bottom