473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dynamically changing the control source of a textbox

12
Good Day,

I am attempting to change the control source of a textbox (Ms Access 2013) based on the quarter; however; the quarters I am using are not standard quarters. I've tried several options but nothing has worked so far. My quarters are:
November - January
February - April
May - July
August - October
The following is the last thing I've attempted (there were several previous unsuccessful attempts):

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.     If MonthName(DatePart("m", Date)) = "October" Then
  4.         Me.PreviousActionPlan1.ControlSource = "Test"
  5.     Else
  6.         If MonthName(DatePart("m", Date)) = "November" Then
  7.             Me.PreviousActionPlan1.ControlSource = "Test"
  8.         End If
  9.     End If
  10.  
  11. End Sub
I didn't bother doing the code for all months as I was testing as I go along and it isn't working. I thought maybe I have to DIM the result of the MonthName Function as text but I'm just guessing.

Any assistance is greatly appreciated.
Oct 24 '18 #1
8 4495
twinnyfo
3,653 Expert Mod 2GB
Merlene,

Forgive me for the length of this response, but I will discuss a few things before I directly address the question--but these things are related....

Let's just go in order of appearance of my discussion points:


1. Using MonthName()
There is no need to use such a complicated formula to achieve what you want here. An alternate would be:

Expand|Select|Wrap|Line Numbers
  1. If Month(Date) = 10 Then

2. Naming Conventions for Controls
If you troll this forum enough, you will run across me ranting about proper naming convetions--particularly for controls on Forma nd Reports. When you list Me.PreviousActionPlan1, I don't know whether that is referring to the Control that is named PreviousActionPlan1 or the underliying Field that is named PreviousActionPlan1. This is because the default naming conventions (that MS Access assigns to all new drag-n-drop-from-the-field-list fields is the name of the Field. This can cause some problems (as you continue developing in MS Access you will found out more about these problems), because that name is now ambigous. Just like I can't say for certain what you are referring to, MS Access will have the same problems when figuring our what to do. So, renaming the Control to something like txtPreviousActionPlan1, will make it not only clear to me that you want to set the control source of the Control, but it will also be clear to MS Access (it does have a mind of its own, by the way).

This bit about naming conventions is a standard principle in our community, and I highly recommend you take it to heart and exercise the practice regularly.


3. What are you setting the Control Source to?

Expand|Select|Wrap|Line Numbers
  1. .ControlSource = "Test"
Indicates (from the code snippet provided), that you always want the Control Source to be "Test". I am sure that is not what you want, due to the nature of your question. However, in general, your code should work to assign a new Control Source. It is better--in circumstances like these--to have an "Unbound" Control, meaning that there is no Control Source assigned to that Control in the first place.


4. A more direct answer to your question.
I don't know exactly what the layout of your Form is, so part of this is a guess that you want to show the current quarter and the next three quarters? (Whenever folks are dealing with quarters, it's usually financial, and the either want to look at preceding or upcoming quarters. THe general principle behind a solutino to this has less to do with constantly changing the control source of a control than it does with modifying the query underlying the form to reflect the data you want to portray.

That's the theory, here's the practical. Instead of having an underlying record source with output fields of "2018-Q4", "2019-Q1", "2019-Q2", etc., which are constantly changing as you pass through the calendar year, I would suggest that what you want is a query with output fields of "Q1" (in which the Quarter of the Current date is in Q1--i.e., today is Q4 2018 according to your list, but for this purpose would eb Q1), "Q2", "Q3", etc., which are constant, but the quarter they represent changes as time passes through the calendar. Thus, the controls on your forms (and reports) can have "Q1", "Q2", etc. as a Control Source. This precludes the need to change things programmatically--which can be clumsy, and mostly unecessary.

So to do this, you need a formula in your query (again, I don't know what your data set looks like) that converts the date of any transactions to Q1-Q4.

It would be helpful to see your underlying query and we could probably work from there.
Oct 24 '18 #2
Merlene
12
Thanks Twinnyfo, I admit that I should have assigned a better name, I am just starting off with Access and I would learn these lessons the hard way if they aren't pointed out. So I will definitely be more mindful going forward.

I put the "Test" in there only in an effort to see if what I was attempting was working. I actually want the form to pull from a different field based on the quarter that we are in. As each quarter's plans are different and have to be kept. Since after designing this database there will be users who are even more unfamiliar with Access I was attempting to have it do this on its own so that they persons running the report would not have to use the queries etc.

I hope I was able to accurately explain the situation.
Oct 24 '18 #3
twinnyfo
3,653 Expert Mod 2GB
Agreed, your users should never have to touch the queries. But that is not where I am going with this.

What does the underlying query look like? Then we can work from there.

By the way, I have been mulling over this problem and putzing with possible solutions, and one of your more significant challenges is the breakout of your quarters. The standard (or even Fiscal) quarters have lots written about them for delineation. Yours will ikely be a challenge. But that is what we have to work with so we will drive on.
Oct 24 '18 #4
NeoPa
32,556 Expert Mod 16PB
I think you've identified the one question that we can deal with in here Twinny.

@Merlene.
If you look at some of the threads that are put there to help you to post questions that we can work with most efficiently you'll find that we have a single question per thread rule here. Yours includes allusions to various questions - a bit like a request for a project plan. Going forward we'll deal with the first main fundamental question in your thread which is how to determine which quarter you're in. Anything beyond that will need to be requested separately in it's own thread.

So, as we're dealing with a question that hasn't been fully and clearly specified I'll start by assuming you're after a quarter indicator of 1 - 4 such that the year doesn't matter, the first quarter starts towards the end of the calendar year in November and the last quarter finishes at the end of October. A function that will return that value has been included below to get you started :
Expand|Select|Wrap|Line Numbers
  1. Public Function GetQuarter() As Integer
  2.     GetQuarter = 1 + ((Month(Date() + 10) Mod 12) \ 3
  3. End Function
Oct 24 '18 #5
Merlene
12
Thanks Twinny & Neo. I will set the quarters first and proceed from there. I will start a new post based on my progress after.
Oct 25 '18 #6
twinnyfo
3,653 Expert Mod 2GB
NeoPa,

Humbly submitted, this function returns the correct quarters:

Expand|Select|Wrap|Line Numbers
  1. Public Function GetQuarter(dtDate As Date) As Integer
  2.     GetQuarter = 1 + ((Month(dtDate) + 1) Mod 12) \ 3
  3. End Function
The previous function was missing a parentheses, but only calculated today's date (as well as returning incorrect quarters).
Oct 25 '18 #7
Merlene
12
Thanks Twinny, yes I realized the closing parentheses was missing, I only tested for today's date so didn't pick up on the rest. Thanks again for the correction.
Oct 25 '18 #8
NeoPa
32,556 Expert Mod 16PB
Darn it Twinny. You're so picky!

You also happen to be spot on. From the OP I took the question to be specific to the current date, so I suspect I was fine on that score - but I did miss out a closing parenth :-(

I also (deliberately in my confusion) set the November to January quarter as 4 instead of 1. I blame that on my Stylish scripts no longer working and my Bytes.com experience being the default layout which, let's just say, isn't my preference.

@Merlene.
Good for you. We'll look out for your next question thread.

PS. Just to be clear my original should have had another closing parenthesis after the call to Date().
Oct 26 '18 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
3
by: Simon Harvey | last post by:
Hi All. In one of my user controls I add a textbox to a placeholder sitting on the user control. txtUsername = new TextBox(); txtUsername.ID = "txtUsername";...
7
by: mef526 | last post by:
I would like to reference a dynamically created control and I know the name. I would like to use the following: Dim strName as String = "txtControl1" ' This is the ".Name" used when textbox was...
4
by: colin spalding | last post by:
Access 2003 I posted this last week; unfortunately, none of the suggested solutions cured the problem I have a subform which lists financial transactions for a client in the main form, which...
4
by: Lou O | last post by:
Is it possible to use the row (index) of a list box as control source Property for a text box? Example: Text1.ControlSource Property is set to "= List1.Column(0,2)" in design view. When I open...
15
beacon
by: beacon | last post by:
Hi everybody, Using Access 2003. I'm trying to dynamically set the record source for the current form and set the control source for a text box to input data to one of three tables. I have a...
5
by: bessora | last post by:
I have a form and combo boxes on it. Depending on what is current record of a form, I need to dynamically (in the code) change the row source of at least one combo box. Problem is that when I read...
16
by: Xenver | last post by:
Hi All, I have many buttons on a form, i want each of these buttons to open the same form but change the control source of the textbox on the form so i can use it to update multiple fields and not...
2
Seth Schrock
by: Seth Schrock | last post by:
I haven't tried anything yet, but I was wondering if anyone else has. I'm wanting to concatenate several fields together along with static text and I want the ability to change the arrangement of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.