Solved I need help in DLookup (1 Viewer)

Local time
Today, 08:34
Joined
Aug 19, 2021
Messages
212
Hi,
I am using DLookup in Microsoft Visual Basic for Applications,
I actually want to get the values of 3 fields:
1) COACODE
2) AccountGroup
3) AccountType
in a form, from a query "COACODE".

1669095524783.png



Visual Basic Code is:
Code:
Private Sub AccountGroupID_AfterUpdate()
Me.COACODE.Value = DLookup("[COACODE]", "COACODE", "[ID]=" & Me.ID)
Me.AccountGroup.Value = DLookup("[GroupName]", "COACODE", "[ID]=" & Me.ID)
Me.AccountType.Value = DLookup("[AccountType]", "COACODE", "[ID]=" & Me.ID)
End Sub


The field COACODE and AccountGroup is appearing in my form. But instead of AccountType its ID is showing.
Please see the screenshot:
1669096516410.png


Let me tell you about the tables:
AccountType is a Table including 2 columns:
1669096939017.png


AccountGroup is another Table including 3 columns:
1669097063365.png

in AccountGroup table the AccountType column is a lookup field from AccountType table.

Thank you in advance to the experts for their precious knowledge.
 

June7

AWF VIP
Local time
Yesterday, 19:34
Joined
Mar 9, 2014
Messages
5,475
Why are you using DLookup() to pull data to form?

I never build lookups in table.

Make the AccountGroupID combobox RowSource a query that joins AccountType and AccountGroup tables and include the AccountType field. Expression in AccountType textbox can reference combobox column by its index to display the descriptive AccountType value. If data is in column 3, its index is 2. Something like:

=[cbxAcctGrp].Column(2)
 
Last edited:
Local time
Today, 08:34
Joined
Aug 19, 2021
Messages
212
Why are you using DLookup() to pull data to form?

I never build lookups in table.

Make the AccountGroupID combobox RowSource a query that joins AccountType and AccountGroup tables and include the AccountType field. Expression in AccountType textbox can reference combobox column by its index to display the descriptive AccountType value. If data is in column 3, its index is 2. Something like:

=[cbxAcctGrp].Column(2)
Hi, june7,
Thank you so much dear,
Can you please explain it through screenshots?
 

June7

AWF VIP
Local time
Yesterday, 19:34
Joined
Mar 9, 2014
Messages
5,475
I rather you follow my instructions and make attempt. Exactly what do you not understand?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:34
Joined
May 7, 2009
Messages
19,247
you try this:

Me.AccountType.Value = DLookup("AccountType", "AccountType", "AccountID = " & DLookup("[AccountType]", "COACODE", "[ID]=" & Me.ID))
 

June7

AWF VIP
Local time
Yesterday, 19:34
Joined
Mar 9, 2014
Messages
5,475
Arnel, are you advising OP to replicate AccountType into ChartOfAccount table?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:34
Joined
May 7, 2009
Messages
19,247
check the Spelling of DLookup (the last dlookup has double "u")
 
Local time
Today, 08:34
Joined
Aug 19, 2021
Messages
212
you try this:

Me.AccountType.Value = DLookup("AccountType", "AccountType", "AccountID = " & DLookup("[AccountType]", "COACODE", "[ID]=" & Me.ID))
I am still facing a problem, when I am selecting AccountGroup from AccountGroupID field an error occurs:
1669204427041.png

Then I am clicking end, Nothing is appearing in COACODE and AccountType:
1669204622524.png

When I am clicking on Debug then
1669204769951.png
 

Attachments

  • 1669204536878.png
    1669204536878.png
    13 KB · Views: 63

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:34
Joined
May 7, 2009
Messages
19,247
use Nz() function:

Me.AccountType.Value = DLookup("AccountType", "AccountType", "AccountID = " & Nz(DLookup("[AccountType]", "COACODE", "[ID]=" & Nz(Me.ID, 0)), 0))
 
Local time
Today, 08:34
Joined
Aug 19, 2021
Messages
212
use Nz() function:

Me.AccountType.Value = DLookup("AccountType", "AccountType", "AccountID = " & Nz(DLookup("[AccountType]", "COACODE", "[ID]=" & Nz(Me.ID, 0)), 0))
But I have to select AccountGroupID twice then AccountType appears:
1669205254472.png

After Second Selection it appears:
1669205317901.png
 

June7

AWF VIP
Local time
Yesterday, 19:34
Joined
Mar 9, 2014
Messages
5,475
This is still replicating AccountType into ChartOfAccount table which is not good RDBMS design. If you designed combobox and used textbox expression as I suggested, no VBA would be needed and data structure would be normalized. It would eliminate AccountType field from ChartOfAccount.
 
Local time
Today, 08:34
Joined
Aug 19, 2021
Messages
212
Here's a sample that might solve the problem
Thanks for sharing Pat, I tried but could not understand this.
Can you guide me through simple steps? According to my database?
 

ebs17

Well-known member
Local time
Today, 05:34
Joined
Feb 7, 2020
Messages
1,949
I would say think twice about everything you do. Start your application from scratch, because it already has a lot of bugs and problems, although it is not yet a real application.

I need help in DLookup
Code:
DLookup("COACODE", "COACODE", "ID = 87")
SELECT COACODE FROM COACODE WHERE ID = 87
As you can see very easily, a DLookup is a complete query encapsulated in an Access function. It returns exactly one return value.
Answer the question why single values should be fetched to DLookups by continuous fire when you can do that with a query.
I also find it problematic that a field is named exactly like a table/query. This may cause errors, but in any case it interferes with the readability and comprehensibility of codes.

Code:
SELECT COACODE, GroupName, AccountType FROM COACODE WHERE ID = 87
' ----------------------------------------------------------------------------------------------
SELECT COACODE, GroupName, AccountType FROM COACODE
You can easily see that you can get more than one value with one query, without filtering all values at once.
So why should there be a constant barrage of individual queries, where COACODE is a query and could/would actually be re-executed each time the query was executed (DLookup call). Technically, this is a disaster, and if the amount of data is large enough, this effort will lead to lousy performance.

If you can lookup individual values using DLookup's, you can do a full query on anything.

Furthermore, values are fetched there in order to rewrite them in a table. This is a gross violation of database rules (normalization, data modeling). To avoid redundancies and disturbances in data integrity, a value of an attribute is stored exactly once in one place. To use this value elsewhere, use references (ID's) to this value.

In summary, if you want to create a real database application and don't just want to pass the time with some games: Learn the basics of a database and start over.
 

Users who are viewing this thread

Top Bottom