Full Version: Dlookup As Control Source
UtterAccess Forums > Microsoft® Access > Access Forms
Quicksilver2002
I'm working on a database and need to switch the control source of two textboxes based on the value selected in cboPart. When I have a part select and open the query manually the correct data is there, I just can get the values to show in the txtboxes on my form. Can someone tell what I'm doing wrong? I'm getting #Name? for everything. Thanks for your help!
Below is my code
CODE
Private Sub cboPart_AfterUpdate()
If Me.cboPart.Value = "1234" Then
    Me.txtLL.ControlSource = DLookup("[LowLimit]", "qryDifferentTolerances")
    Me.txtUl.ControlSource = DLookup("[UpperLimit]", "qryDifferentTolerances")
Else
    Me.txtLL.ControlSource = DLookup("[LowLimit]", "qryTolerances")
    Me.txtUl.ControlSource = DLookup("[UpperLimit]", "qryTolerances")
End If
Me.txtLL.Requery
Me.txtUl.Requery
End Sub
ipisors
Hi -
You don't need to use the .ControlSource
Since your code will continually be looking to update them anyway, you can simply code:
CODE
Me.txtLL.Value = DLookup("[LowLimit]", "qryDifferentTolerances")

Are you sure you don't need the optional 3rd argument (criteria) in the Dlookup() ?
Quicksilver2002
That worked! Thanks for your hlep
ipisors
No problem. And just to clarify, thinking about it more, I'm fairly sure that you could code the .ControlSource if you wanted to, but your code would have to supply the literal string value, so it might be something like:
!--c1-->
CODE
Me.txtLL.ControlSource = "=DLookup(" & chr(34) & "[LowLimit]" & chr(34) & ", " & chr(34) & "qryDifferentTolerances" & chr(34) & ")"

(untested aircode)
Of course I'm not suggesting you do that, just supplying a clarification.
Galaxiom
One DLookup is an easy to populate a control. However when you start using more than one from the same domain it is time to rethink your strategy.
ubforms are often a much better alternative and can be made to look like their controls are on the main form.
ipisors
Agree. Good point.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.