Using VBA and the Visible Option

Michelle0531

New member
Local time
Today, 03:20
Joined
Nov 10, 2017
Messages
6
Help! I'm trying to create a code that will display a field depending on the value of a calculation. Basically, if the years of service is greater than 5, I want the field to show. Otherwise, I do not want it to show. Here's what I have and I cannot get it to work. Can anyone help?

Private Sub Vested_Click()
If (Now() - StartDate)/365 > 5 Then
Vested.Visible = True
Else Vested.Visible = False
End If
End Sub
 
Here's what I have and I cannot get it to work.

"Cannot get it to work" isn't a useful description. Describe what does happen.

Code:
Private Sub Vested_Click()
If (Now() - StartDate)/365 > 5 Then
Vested.Visible = True
Else Vested.Visible = False
End If
End Sub
You are asking a control to be made invisible in response to clicking on it. Seems an unlikely strategy. I also doubt that a control can be made invisible while it has focus.
 
And the code would be better placed in the form's OnCurrent event. Don't have to click anything.
 
Might it be the leap year difference?


?(Now() - #11/10/2012#) / 365
5.00412534880771

?(Now() - #11/10/2012#) /(365 + (5/4) *.25)
4.99984386781991


Jiri
 
Last edited:
What are you looking for? Num of years, month?
 
What I'm looking for is to have a box be visible only if the years of service is more than 5. When I say it's not working, I mean the box stays visible no matter what. Do I need to change the Click to OnCurrent?
 
I changed my code to read the following and the box still stays visible no matter what:

Private Sub Vested_OnCurrent()

If Now() - StartDate >= 5 Then
Vested.Visible = True
Else: Vested.Visible = False
End If


End Sub
 
Sorry...it's this (I always forget that /365):

Private Sub Vested_OnCurrent()

If (Now() - StartDate)/365 >= 5 Then
Vested.Visible = True
Else: Vested. Visible = False
End If

End Sub
 
Will using datediff func solve it:

Datediff("yyyy", startdate, date()) >= 5
 
Will using datediff func solve it:

Datediff("yyyy", startdate, date()) >= 5

No luck. The box stays visible no matter what. I also have the same problem with another visible code. The field stays visible no matter what. I want it to be invisible if the field is empty. See below:

Private Sub Salary_OnCurrent()

If Salary = 0 Then
Salary.Visible = False
Salary_Label.Visible = False
Else: Salary.Visible = True
Salary_Label.Visible = True
End If


End Sub
 
And, just a note, every other code I've worked on so far has worked. Just the visible one is the problem. Not sure what I'm doing different with that one.
 
Waht is your form single, continuous or datasheet.
 
Going back to the code in post 1, add a line setting the focus to another control
Change the name to a control on your own form

Code:
Private Sub Vested_Click()
If (Now() - StartDate)/365 > 5 Then
  Vested.Visible = True
Else
  [B][COLOR="Red"]Me.AnotherControl.SetFocus[/COLOR][/B]
  Vested.Visible = False
End If
End Sub
 
I was wondering when/if the OP was going to address that issue (Set Focus). Dollars to doughnuts there is where the problem lies...
 
I don't think this is a "Focus" problem.

Code:
Private Sub Salary_OnCurrent()      ' <=== #1

If Salary = 0 Then
  Salary.Visible = False                 ' <=== #2
  Salary_Label.Visible = False
Else
  Salary.Visible = True
  Salary_Label.Visible = True
End If

End Sub

Your problem starts with what appears to be something you attempted to do by hand, bypassing the form's wizards. Granted, the wizards are individually dumber than a box of rocks, but they can build GREAT starting points for subsequent customization.

At point #1, you refer to something that ONLY applies to a Form called Salary, because in the context of your stated problem, only a FORM has a current event. (OnCurrent is not Control event.) But normally, a form's OnCurrent entry-point name wouldn't be as you wrote it. It would just be Form_Current. Which is why I surmised that you built that by hand and avoided the wizards. They wouldn't do what you showed us.

At point #2, you use Salary as a control, not as a form name. Here, it might actually make sense, because the the label created automatically by typical form wizards or control wizards might bear the name of the underlying field and a label as you describe. The syntax doesn't look wrong.

Given that I see no problem with syntax, I'm going to guess that the reason your code isn't working is because it never gets called. The formula for the IF isn't what you were discussing in other parts of the thread, but once you get this to actually DO something, perhaps you can go back and work on the math. So let's get the logic up and running.

Here's what you do...

1. In Form Design mode on that form, with the properties sheet showing, select the Event properties and find the OnCurrent property. I'm guessing that it will be blank.

2. Double-click inside the box for OnCurrent; it will open up a code window into the VBA code for the form. You are now inside a window where you can see ALL of the code associated with your form's class module, not just the OnCurrent event.

3. Take the body of your Salary_OnCurrent (but not the entry point line and not the End Sub line because the form wizard just created a valid entry/exit pair for you) and do a Cut/Paste operation.

4. Delete the now-empty entry point and end point for your original Salary_OnCurrent because that is now an empty subroutine.

NOW try your form.
 
Doc is partly correct here.
Its not JUST a focus problem

What Michelle is trying to do is achievable but ... not easily reversible.
If that isn't an issue then fine ....but to me it makes little sense

I've knocked together a simple database using the same fields as in Michelle's post.
The screengrab below may help explain what Doc was saying

attachment.php


Database attached in case anyone wants to play with it.

Doc : I seem to remember you don't download any databases from the forum
Unless you want to make an exception in this case, you'll just have to trust me ...I'm a doctor! :D
 

Attachments

Colin, I'm a doctor too - but not of medicine. My screen name came about when one of my co-workers realized I had a Ph.D. (earned) degree so he said, "It's The Doc Man" - and I liked the sound of it enough to use it as a screen name.

You are correct. I so very rarely download anything that could have active code in it that you might just as well say "never" - but that is a side-effect of having been a systems admin on systems with "Sensitive But Unclassified" rating or higher. I've seen too many systems compromised by something innocent that just adjusts a minor setting and, bang-zoom, it's system rebuild time.
 
Hi Doc - I'm not really - that was just to get your attention ...

"Trust me ... I'm a doctor" is a well known UK phrase - maybe also in the US.
I just couldn't resist using it in that post

Anyway, that's why I added the screenshot - hopefully I got the gist of what you were saying...
 
..."Trust me ... I'm a doctor" is a well known UK phrase - maybe also in the US.
I just couldn't resist using it...

Recently made famous by none other then Gene “Dr. Love” Simmons!
 
Ridders is right. The problem seems to be the code needs to be in the On_Current event, not the On_Click event.
 

Users who are viewing this thread

Back
Top Bottom