A text box should be easy, right?

gpeirce

Registered User.
Local time
Today, 11:24
Joined
Sep 24, 2015
Messages
23
I'm an experienced SQL programmer but I'm finding I know diddly-squat about Access.

I'm trying to make the simplest of text boxes on a form. I want to display a person's age just to the right of where they would input a birthdate. My age calculation works like a charm, putting the value in "agenow". I don't want "agenow" to be open for input, just a display variable. Here is the code for [agenow] to let you know it does work:



Private Sub Form_Current()
Dim agenow As Integer
agenow = DateDiff("yyyy", [DOB], Now())
If (Format([DOB], "mmdd") > Format(Now(), "mmdd")) Then
agenow = agenow - 1
End If


My problem is:
1. I don't know how to show this variable in a text box.
2. When it does show, I want it protected. It's just information only.
3. I've attached my screen print of the form.
4. If you can show me a working example of a variable in a text box, I sure would like to see it.

Can you help this dinosaur out? I do a simple Google search on "how do you make a text box in Access" and I get answers on how I make it html ready, how to make hyperlinks, videos on youtube about entering data into a text box. I'm sure a 5th grader knows this answer, so can you help? I don't think "I'm smarter than a 5th grader" right now.

Thank you.
 

Attachments

  • agenow.jpg
    agenow.jpg
    104.3 KB · Views: 105
Last edited:
you don't need any vba code just enter

= DateDiff("yyyy", [DOB], Now())

in the controlsource of your agenow control

it will not be editable

you might prefer to use Date() rather than Now() - in the context you are using it probably doesn't matter but the time element in Now() may produce unexpected results
 
The key to what is in the textbox is its ControlSource Property.

You can type an equals sign followed by an expression.

Code:
=DateDiff("yyyy", [DOB], Date())+(Format([DOB], "mmdd") > Format(Date(), "mmdd"))

Or create a custom function in VBA and call that:
Code:
= AgeToday([DOB])
 
just realized you have a bit more code so try

=DateDiff("yyyy", [DOB], Now())+((Format([DOB], "mmdd") > Format(Now(), "mmdd"))

In access, true has a value of -1 (false=0)
 
Thank you, CJ, but that did not address my items #1 and #4. How the devil do I show a variable name in the textbox? I have other variables that I'll be computing in a VBA but all I get is #name? when it displays.

Thank you again.
 
Thank you also, Galaxiom, but I'm really trying to learn out to use a text box.
 
Private Sub Form_Current()
Dim agenow As Integer
agenow = DateDiff("yyyy", [DOB], Now())
If (Format([DOB], "mmdd") > Format(Now(), "mmdd")) Then
agenow = agenow - 1
Me.NameOfTheBl...TextBox= agenow
End If
 
How the devil do I show a variable name

Er ... what do you want -show the value of a variable or its name?

Update: You may want to DL the free Smart indenter to indent your code. google.
 
Thank you spikepl for that clarification. I want to display the value of the person's age. What do I put into the text box properties? I have the name "agenow" in the control source of the text box but that is obviously wrong. Do you have an accdb that shows the calculation of variable from a VBA?
 
This is a case of paralysis by analysis. I gave you code, you did not comment on it. Now I have no clue what you want but being the short-tempered bitch I am also no inclination to pursue this. Wait for someone with a grain of patience.
 
Can you explain what you mean by:

Me.NameOfTheBl...TextBox= agenow

I'm confused about NameofTheBL... means. If my text box is named "text24", what do I put where??




Private Sub Form_Current()
Dim agenow As Integer
agenow = DateDiff("yyyy", [DOB], Now())
If (Format([DOB], "mmdd") > Format(Now(), "mmdd")) Then
agenow = agenow - 1
Me.NameOfTheBl...TextBox= agenow
End If
 
Thank you, CJ, but that did not address my items #1 and #4. How the devil do I show a variable name in the textbox?

you asked
1. I don't know how to show this variable in a text box.
I said
you don't need any vba code just enter

= DateDiff("yyyy", [DOB], Now())

in the controlsource of your agenow control

you asked
2. When it does show, I want it protected. It's just information only.
I said
it will not be editable
you asked
4. If you can show me a working example of a variable in a text box, I sure would like to see it.
I said

=DateDiff("yyyy", [DOB], Now())+((Format([DOB], "mmdd") > Format(Now(), "mmdd"))

what don't you understand?
 
Thank you all for your help. Turns out the answer to #1 was:

me.Text24 = agenow

and I just could not figure that out.
 
Last edited:
Thread undeleted - other users may find it helpful

good luck with your project
 

Users who are viewing this thread

Back
Top Bottom