Solved DLookup (1 Viewer)

TajikBoy

Member
Local time
Today, 10:53
Joined
Mar 4, 2011
Messages
82
Good afternoon guys/gals,

I'm trying to assign a value to a text box on form load with below code but Access tells me I can't....
As usual, what am I doing wrong?

Also tried .value, same error.....

Me.Text981 = DLookup("[CompanyName]", "tblVendors", "[ID]=" & Me.VendorName)
 

Ranman256

Well-known member
Local time
Today, 13:53
Joined
Apr 9, 2015
Messages
4,337
This looks correct...what error msg?
Is the box name correct? Me.vendorname
where is the code? In form load? In other event?
is the return field name correct?
 

TajikBoy

Member
Local time
Today, 10:53
Joined
Mar 4, 2011
Messages
82
This looks correct...what error msg?
Is the box name correct? Me.vendorname
where is the code? In form load? In other event?
is the return field name correct?
Hi,

Text981 - correct, me.vendorName correct

Code in Form Load....

If I change the vendorname combobox to a different vendor it works ?
 

TajikBoy

Member
Local time
Today, 10:53
Joined
Mar 4, 2011
Messages
82
This looks correct...what error msg?
Is the box name correct? Me.vendorname
where is the code? In form load? In other event?
is the return field name correct?
1670496309033.png
 

Ranman256

Well-known member
Local time
Today, 13:53
Joined
Apr 9, 2015
Messages
4,337
Is ID a numeric field?
is the combo bound to the correct column?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:53
Joined
May 7, 2009
Messages
19,247
does vendorname combobox has a value when the form load?
does Me.Text981 textbox has an expression in it as Control source?
 

Ranman256

Well-known member
Local time
Today, 13:53
Joined
Apr 9, 2015
Messages
4,337
Ah, the Text981 cannot be bound to a field.
it must be unbound
 

TajikBoy

Member
Local time
Today, 10:53
Joined
Mar 4, 2011
Messages
82
does vendorname combobox has a value when the form load?
does Me.Text981 textbox has an expression in it as Control source?
Yes, it opens up from a list a view contract form sort of and yes, same Dlookup is in there too, as I need it to update if I change the vendor on the form
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:53
Joined
May 7, 2009
Messages
19,247
that is the problem, remove the expression on the Control Source.
and just add code to the AfterUpdate of the vendorname combobox
to fill the textbox again using Dlookup().
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:53
Joined
Sep 21, 2011
Messages
14,350
You cannot assign a value to a control that has an expression as it's source?
I cannot see any vendor name working like that?. Plus if vendor name is a string, it should be surrounded by single quotes, or triple " if the string could have a single quote in it.
 

TajikBoy

Member
Local time
Today, 10:53
Joined
Mar 4, 2011
Messages
82
OK..... Problem solved for erratic working.....

Apart from the expression error as mentioned by you guys, I found out that my data entry guy took a short cut and instead of linking the ID fields, he has just typed the vendor names, now he is correcting, after he will dig his own grave - is there a easier code to make him disappear???

Thanks a million, and for sure, I'll be back soon :)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:53
Joined
Feb 19, 2002
Messages
43,347
Do you realize that the Load event runs only once when the form loads? If you ever navigate to a new record or use the form to add a record, the code won't run.

A better way to do this is to put an expression into the unbound CompanyName field' ControlSource.

= YourCompanyComboName.Column(1)

The RowSource of the combo is a zero based array so .Column(1) references the SECOND column of the RowSource. If the name is in a different column, Count the columns, 1,2,3,... then subtract 1.

And a warning " Me.Text981 " is not only a bad name for a control, it is indicative of an upcoming problem. There are a limited number of controls allowed on any form. You have apparently added and deleted lots of controls to get up to 981. I suggest an immediate C&R. I don't remember if that resets the count. You may have to create a new, empty database and import all the objects to avoid not being able to update the form any more. Access never forgets and this eventually causes a problem.
 
Last edited:

June7

AWF VIP
Local time
Today, 09:53
Joined
Mar 9, 2014
Messages
5,488
If PK ID field is autonumber type then the FK field must be number type. How could names be typed into field? Your 'data entry guy' is a user? Why would a user be setting up links?
 

TajikBoy

Member
Local time
Today, 10:53
Joined
Mar 4, 2011
Messages
82
Do you realize that the Load event runs only once when the form loads? If you ever navigate to a new record or use the form to add a record, the code won't run.

A better way to do this is to put an expression into the unbound CompanyName field' ControlSource.

= YourCompanyComboName.Column(1)

The RowSource of the combo is a zero based array so .Column(1) references the SECOND column of the RowSource. If the name is in a different column, Count the columns, 1,2,3,... then subtract 1.

And a warning " Me.Text981 " is not only a bad name for a control, it is indicative of an upcoming problem. There are a limited number of controls allowed on any form. You have apparently added and deleted lots of controls to get up to 981. I suggest an immediate C&R. I don't remember if that resets the count. You may have to create a new, empty database and import all the objects to avoid not being able to update the form any more. Access never forgets and this eventually causes a problem.
HI Pat,

Yes I do, that was a feeble attempt to resolve an issue caused by something else, which I have fixed now, also the bad name issue, form was copied over from a different project and modified, hence the 981......

All good now and value your advice.....

Thanks,
 

Users who are viewing this thread

Top Bottom