dbTalk Databases Forums  

DLookup in a form to find value in a table

comp.databases.ms-access comp.databases.ms-access


Discuss DLookup in a form to find value in a table in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
A_Classic_Man
 
Posts: n/a

Default DLookup in a form to find value in a table - 01-18-2011 , 11:50 PM






I have the following tables
1. tblActivities
2. tblEmpName with the fields IDNum and EmpName
3. tblTradeWorked with the fields WorkedFor and TradeWithIDNum

I have a main form frmEmpForm with a subform frmActivities. The main
form has a control button that is used to open frmTradeWorked.

When I enter a name in the control WorkedFor on the frmTradeWorked
which gets EmpName from tblEmpName, I want the control on the
frmTradeWorked entitled TradeWithIDNum to autofill with the correct
IDNum from the tblEmpName.

This is the code I am trying to use but it will not work.

Private Sub WorkedFor_AfterUpdate()
Me.TradeWithIDNum = DLookup("[IDNum]", "tblEmpName", "[EmpName]='" &
Me.[WorkedFor] & "'")
End Sub

Help is appreciated
Thanks
Ron

Reply With Quote
  #2  
Old   
Stuart McCall
 
Posts: n/a

Default Re: DLookup in a form to find value in a table - 01-19-2011 , 12:15 AM






"A_Classic_Man" <rmerkel2 (AT) cox (DOT) net> wrote

Quote:
I have the following tables
1. tblActivities
2. tblEmpName with the fields IDNum and EmpName
3. tblTradeWorked with the fields WorkedFor and TradeWithIDNum

I have a main form frmEmpForm with a subform frmActivities. The main
form has a control button that is used to open frmTradeWorked.

When I enter a name in the control WorkedFor on the frmTradeWorked
which gets EmpName from tblEmpName, I want the control on the
frmTradeWorked entitled TradeWithIDNum to autofill with the correct
IDNum from the tblEmpName.

This is the code I am trying to use but it will not work.

Private Sub WorkedFor_AfterUpdate()
Me.TradeWithIDNum = DLookup("[IDNum]", "tblEmpName", "[EmpName]='" &
Me.[WorkedFor] & "'")
End Sub

Help is appreciated
Thanks
Ron
It's a syntax problem. If you're going to use square brackets:

[WorkedFor]

it must be with the bang, not the dot, ie:

Me![WorkedFor]

However, since WorkedFor doesn't contain any spaces, you don't need the
brackets anyway (the same goes for IDNum and EmpName), so:

Me.TradeWithIDNum = DLookup("IDNum", "tblEmpName", "EmpName='" &
Me.WorkedFor & "'")

should do nicely.

Reply With Quote
  #3  
Old   
Salad
 
Posts: n/a

Default Re: DLookup in a form to find value in a table - 01-19-2011 , 12:23 AM



A_Classic_Man wrote:

Quote:
I have the following tables
1. tblActivities
2. tblEmpName with the fields IDNum and EmpName
3. tblTradeWorked with the fields WorkedFor and TradeWithIDNum

I have a main form frmEmpForm with a subform frmActivities. The main
form has a control button that is used to open frmTradeWorked
What do these 2 forms have to do with anything regarding your issue?
They add confusion to your problem since they are superfluous to the
rest of the description.


Quote:
When I enter a name in the control WorkedFor on the frmTradeWorked
which gets EmpName from tblEmpName, I want the control on the
frmTradeWorked entitled TradeWithIDNum to autofill with the correct
IDNum from the tblEmpName.

This is the code I am trying to use but it will not work.

Private Sub WorkedFor_AfterUpdate()
Me.TradeWithIDNum = DLookup("[IDNum]", "tblEmpName", "[EmpName]='" &
Me.[WorkedFor] & "'")
End Sub

Help is appreciated
Thanks
Ron
You could open a code module, and from the Immediate window do something
like

wf = "Joe Blow" 'put in valid name
? DLookup("[IDNum]", "tblEmpName", "[EmpName]='" & _
wf & "'")
Did you get a value? Or did you get a Null?

If you got a value then put the word
STOP
prior to your dlookup line and step thru the code with your form open.

Reply With Quote
  #4  
Old   
Phil
 
Posts: n/a

Default Re: DLookup in a form to find value in a table - 01-19-2011 , 03:15 AM



On 19/01/2011 05:50:21, A_Classic_Man wrote:
Quote:
I have the following tables
1. tblActivities
2. tblEmpName with the fields IDNum and EmpName
3. tblTradeWorked with the fields WorkedFor and TradeWithIDNum

I have a main form frmEmpForm with a subform frmActivities. The main
form has a control button that is used to open frmTradeWorked.

When I enter a name in the control WorkedFor on the frmTradeWorked
which gets EmpName from tblEmpName, I want the control on the
frmTradeWorked entitled TradeWithIDNum to autofill with the correct
IDNum from the tblEmpName.

This is the code I am trying to use but it will not work.

Private Sub WorkedFor_AfterUpdate()
Me.TradeWithIDNum = DLookup("[IDNum]", "tblEmpName", "[EmpName]='" &
Me.[WorkedFor] & "'")
End Sub

Help is appreciated
Thanks
Ron

Think it all depends on what TradeWithIDNum is. Is it an autonumber or just a
long number? Are there any relationships established between tblTradeWorked
and any other table? When you say you want to autofill what do you mean - are
you trying to add a new record or are you trying to find an existing record?
If you type DLookup("IDNum", "tblEmpName", "EmpName='" & Me!WorkedFor & "'")
in the immediate window, do you get an result? Note the removal of
unneccessary square brackets which you only need round fields with spaces in
them or if you are using reserved words as a field name (like [Date]) Phil

Reply With Quote
  #5  
Old   
A_Classic_Man
 
Posts: n/a

Default Re: DLookup in a form to find value in a table - 01-19-2011 , 08:37 AM



On Jan 19, 12:15*am, "Stuart McCall" <smcc... (AT) myunrealbox (DOT) com> wrote:
Quote:
"A_Classic_Man" <rmerk... (AT) cox (DOT) net> wrote in message

news:da6efdec-900c-4d2f-a4cf-f8ed2e08a097 (AT) u32g2000yqe (DOT) googlegroups.com...





I have the following tables
1. tblActivities
2. tblEmpName with the fields IDNum and EmpName
3. tblTradeWorked with the fields WorkedFor and TradeWithIDNum

I have a main form frmEmpForm with a subform frmActivities. The main
form has a control button that is used to open *frmTradeWorked.

When I enter a name in the control WorkedFor on the frmTradeWorked
which gets EmpName from tblEmpName, I want the control on the
frmTradeWorked entitled TradeWithIDNum to autofill with the correct
IDNum from the tblEmpName.

This is the code I am trying to use but it will not work.

Private Sub WorkedFor_AfterUpdate()
Me.TradeWithIDNum = DLookup("[IDNum]", "tblEmpName", "[EmpName]='" &
Me.[WorkedFor] & "'")
End Sub

Help is appreciated
Thanks
Ron

It's a syntax problem. If you're going to use square brackets:

[WorkedFor]

it must be with the bang, not the dot, ie:

Me![WorkedFor]

However, since WorkedFor doesn't contain any spaces, you don't need the
brackets anyway (the same goes for IDNum and EmpName), so:

Me.TradeWithIDNum = DLookup("IDNum", "tblEmpName", "EmpName='" &
Me.WorkedFor & "'")

should do nicely.- Hide quoted text -

- Show quoted text -
Thanks to all for the responses
Stuart, I tried your suggestion and still get nothing.
The IDNum fields are text fields and are size is set to 5 for both
fields.
TradeWithIDNum in the tblTradeWorked and IDNum in the tblEmpName are
the linked fields.

I have checked spelling of table and field names and all appear to be
correct.
Any other ideas what to look for. I have a feeling there is something
simple that I am missing

Ron

Reply With Quote
  #6  
Old   
A_Classic_Man
 
Posts: n/a

Default Re: DLookup in a form to find value in a table - 01-19-2011 , 09:00 AM



On Jan 19, 8:37*am, A_Classic_Man <rmerk... (AT) cox (DOT) net> wrote:
Quote:
On Jan 19, 12:15*am, "Stuart McCall" <smcc... (AT) myunrealbox (DOT) com> wrote:





"A_Classic_Man" <rmerk... (AT) cox (DOT) net> wrote in message

news:da6efdec-900c-4d2f-a4cf-f8ed2e08a097 (AT) u32g2000yqe (DOT) googlegroups.com....

I have the following tables
1. tblActivities
2. tblEmpName with the fields IDNum and EmpName
3. tblTradeWorked with the fields WorkedFor and TradeWithIDNum

I have a main form frmEmpForm with a subform frmActivities. The main
form has a control button that is used to open *frmTradeWorked.

When I enter a name in the control WorkedFor on the frmTradeWorked
which gets EmpName from tblEmpName, I want the control on the
frmTradeWorked entitled TradeWithIDNum to autofill with the correct
IDNum from the tblEmpName.

This is the code I am trying to use but it will not work.

Private Sub WorkedFor_AfterUpdate()
Me.TradeWithIDNum = DLookup("[IDNum]", "tblEmpName", "[EmpName]='" &
Me.[WorkedFor] & "'")
End Sub

Help is appreciated
Thanks
Ron

It's a syntax problem. If you're going to use square brackets:

[WorkedFor]

it must be with the bang, not the dot, ie:

Me![WorkedFor]

However, since WorkedFor doesn't contain any spaces, you don't need the
brackets anyway (the same goes for IDNum and EmpName), so:

Me.TradeWithIDNum = DLookup("IDNum", "tblEmpName", "EmpName='" &
Me.WorkedFor & "'")

should do nicely.- Hide quoted text -

- Show quoted text -

Thanks to all for the responses
Stuart, I tried your suggestion and still get nothing.
The IDNum fields are text fields and are size is set to 5 for both
fields.
TradeWithIDNum in the tblTradeWorked and IDNum in the tblEmpName are
the linked fields.

I have checked spelling of table and field names and all appear to be
correct.
Any other ideas what to look for. I have a feeling there is something
simple that I am missing

Ron- Hide quoted text -

- Show quoted text -
It's working now. The following code resolved the problem
Me.TradeWithIDNum = DLookup("IDNum", "tblEmpName", "IDNum='" &
Me.WorkedFor & "'")
Thanks to all for the help

Ron

Reply With Quote
  #7  
Old   
Stuart McCall
 
Posts: n/a

Default Re: DLookup in a form to find value in a table - 01-19-2011 , 11:58 AM



The working code is exactly what I posted. Oh well, blame the gremlins.

Reply With Quote
  #8  
Old   
A_Classic_Man
 
Posts: n/a

Default Re: DLookup in a form to find value in a table - 01-19-2011 , 02:06 PM



On Jan 19, 11:58*am, "Stuart McCall" <smcc... (AT) myunrealbox (DOT) com> wrote:
Quote:
The working code is exactly what I posted. Oh well, blame the gremlins.
Stuart

Me.TradeWithIDNum = DLookup("IDNum", "tblEmpName", "EmpName='" &
Me.WorkedFor & "'") What you suggested
Me.TradeWithIDNum = DLookup("IDNum", "tblEmpName", "IDNum='" &
Me.WorkedFor & "'") What works

I'm not sure what is going on with this. The way I think this through
is:

"TradeWithIDNum" on the current form is the "IDNum" in "tblEmpName"
where "EmpName" in "tblEmpName" is equal to "WorkedFor" on the current
form.

I guess that thought process is incorrect. When I Changed "EmpName" to
"IDNum" the code works perfectly

Thanks

Ron

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.