473,406 Members | 2,620 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,406 software developers and data experts.

Setting controlsource to Dlookup

12
Good Day,

I am attempting to set the controlsource of a textbox on a form to a dlookup function but it isn't working. The field just appears blank. When I check the code in the immediate window I get compile error: variable not yet created in this context.

I am relatively new to Access but I've been trying to figure this out on my own with no luck so far.

Expand|Select|Wrap|Line Numbers
  1. Me.txtPreviousActionPlan1.ControlSource = _ 
  2.     Nz(DLookup("Q3Milestone1", _
  3.                "TBL_ActionPlan", _
  4.                "SingleName =" & "'" & [SingleName] & "'"), _
  5.        "")
Any help or guidance is appreciated.
Oct 28 '18 #1

✓ answered by twinnyfo

Merlene,

Exactly what is it that you are trying to do? Becuase your latest post made it more confusing than the original.

A ControlSource for a control is the source of the data for the particular control. This can either be a field on a table or data that is derived from another source. Thus the ControlSource of a text box will take one of two general forms: [FieldName] (from an underlying Table or Query) or "= A Value or calculated formula" (that is assigned to the Control and displayed on the form).

Your code above is doing neither, which is why it does not work; but this also begs my original question in this post: "What are you trying to do?" Because it is not clear.

If you are truly trying to change the Control Source of the text box, then you must assign a Field Name from the underlying Table or Query which is part of the current underlying record source of the form:

Expand|Select|Wrap|Line Numbers
  1. Me.txtPreviousActionPlan1.ControlSource = "Q3Milestone1"
If you are trying to change the value displayed by the text box, then, you ignore the Control Source and simply assign a value to the text box:

Expand|Select|Wrap|Line Numbers
  1. Me.txtPreviousActionPlan1 = _
  2.     Nz(DLookup("Q3Milestone1", _ 
  3.                "TBL_ActionPlan", _
  4.                "SingleName = '" & [SingleName] & "'"), _
  5.        "")
Again, it is unclear which you are aiming at.

6 2709
twinnyfo
3,653 Expert Mod 2GB
Merlene,

It appears you are trying to set the Control Source in VBA? It is possible to do this in VBA, but in your particular case, it is not necessary. You should be able to do this by simply setting the control’s Control Source property to:

Expand|Select|Wrap|Line Numbers
  1. = Nz(DLookup("Q3Milestone1", _
  2.              "TBL_ActionPlan", _
  3.              "SingleName = '" & [SingleName] & "'"), _
  4.      "")
Hope this hepps.
Oct 28 '18 #2
Merlene
12
Hi Twinny,

Yes, I've tried it directly in the control source to ensure that the function was otherwise working however, I wanted the control source to be dependent on the quarter. Here's my full code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.     If GetQuarter() = 4 Then
  4.         Me.txtPreviousActionPlan1.ControlSource = _
  5.             Nz(DLookup("Q3Milestone1", _
  6.                        "TBL_ActionPlan", _
  7.                        "SingleName = '" & [SingleName] & "'"), _ 
  8.                "")
  9.      End If
  10.  
  11. End Sub
Oct 29 '18 #3
twinnyfo
3,653 Expert Mod 2GB
Merlene,

Exactly what is it that you are trying to do? Becuase your latest post made it more confusing than the original.

A ControlSource for a control is the source of the data for the particular control. This can either be a field on a table or data that is derived from another source. Thus the ControlSource of a text box will take one of two general forms: [FieldName] (from an underlying Table or Query) or "= A Value or calculated formula" (that is assigned to the Control and displayed on the form).

Your code above is doing neither, which is why it does not work; but this also begs my original question in this post: "What are you trying to do?" Because it is not clear.

If you are truly trying to change the Control Source of the text box, then you must assign a Field Name from the underlying Table or Query which is part of the current underlying record source of the form:

Expand|Select|Wrap|Line Numbers
  1. Me.txtPreviousActionPlan1.ControlSource = "Q3Milestone1"
If you are trying to change the value displayed by the text box, then, you ignore the Control Source and simply assign a value to the text box:

Expand|Select|Wrap|Line Numbers
  1. Me.txtPreviousActionPlan1 = _
  2.     Nz(DLookup("Q3Milestone1", _ 
  3.                "TBL_ActionPlan", _
  4.                "SingleName = '" & [SingleName] & "'"), _
  5.        "")
Again, it is unclear which you are aiming at.
Oct 29 '18 #4
Merlene
12
My apologies for the confusion.
- The record source for my form is from a query : Qry_UpdateWL
- I am attempting to change the control source of the textbox : txtPreviousActionPlan1
- I would like the control source to change based on the current quarter. The data in the textbox reflects an officer's comments which changes each quarter.
- The comments are contained in the table: TBL_ActionPlan
- The Field Name with the comments for quarter 4 is : Q3Milestone1
- The query on which the record source is based also contains the Field Name Q3Milestone1
- In attempting to get the control source to work I read that it was not necessary to link to the query and that it was sometimes easier to use the table directly for DLookup function. So I tried that instead (which is why the Dlookup is not linked to the same query as the rest of the form)

I will revert to using the record source query in the Dlookup to see if it works.
Oct 29 '18 #5
Merlene
12
It worked! Thanks so much for your patience and again I apologize for not being clear from the beginning! My code is now:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.     If GetQuarter() = 4 Then
  4.         Me.txtPreviousActionPlan1.ControlSource = "Q3Milestone1"
  5.  
  6.     End If
  7.  
  8. End Sub
Oct 29 '18 #6
twinnyfo
3,653 Expert Mod 2GB
I'm glad I coudl hepp! Let us know if there is anything else!
Oct 29 '18 #7

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

Similar topics

3
by: Shelli Carol | last post by:
Good day, I have: 1) A table with numbered fields (1, 2, 3, etc.) 2) A form text box, whose ControlSource property contains an expression. This expression returns a number. If I just...
1
by: Knud Rogilds | last post by:
I have tried everything, what am I doing wrong? I have a data entry form in datasheet view (for mass data entry). One of the fields are SalesID, and I would like to use DLookup to populate an...
6
by: LSemos | last post by:
I am trying to lookup phone numbers in a separate table based on the name in the current Tab control tblContacts has the fields: Name, Phone, Fax, and Email In my Form's Tab control, I have...
3
by: Otie | last post by:
Is there a simple answer as to why my database will not save the data that correctly appears in the AccountID textbox on a form to the table when I set the AccountID.ControlSource =...
2
by: ctyrrell | last post by:
I have read with interest the many discussions of the 3048 Error: Cannot open any more databases. I understand how the number of open Table ID's causes the problem. My question has to do with the...
6
by: Don Sealer | last post by:
I've written this expression for a DLookup function. It works almost alright. What I'm trying to do is type in a description and the ID field (number) populates automatically. It works almost as...
13
by: nzyui | last post by:
Trying to lookup a field from a table called condition this is a separate table not linked to anything: setup as shown Code S M L DG 1 2 3 RI 2 4 5 need...
6
by: MLH | last post by:
I have a form named frmVehicleEntryForm. It has a 3-tab tab control. On the 2nd tab page, there's a textbox named VehicleLocationName whose default value setting is ...
2
by: Nick AU | last post by:
Hello, I am using MS Access 2003 and trying to set a textbox's ControlSource in VBA code. this following works fine Task.ControlSource = "." However, if i try to set the control source...
6
doma23
by: doma23 | last post by:
I have an If-Else condition in VBA. I want to do next: If (Not IsNull(Me.cmbSubstantive)) Then Me!txtSub_Artikel.ControlSource = DLookup("", "tblSubstantive", " = '" & !! & "'") Else ...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.