Dlookup with DoCmd.OpenForm (1 Viewer)

Eljefegeneo

Still trying to learn
Local time
Today, 14:26
Joined
Jan 10, 2011
Messages
904
I am trying to open up my frmMain with only active clients. I do have a control [Class} which has among several options, "Active". However when I use a macro to open the form with the critera [Class] = "Active, sometimes I get non-active clients. This is because [Class} is updated on the form's OnCurrent event based on another calculated field. But, of course, I have to have a particular record open for the [Class] control to be updated. So I decided to use the DLookup function based on a query that only gives me "Active" clients. The query is:
SELECT tblSales.ClientID, Min(IIf([ContractsEnds]<Date() Or [DateCancelled]<Date(),"Inactive","Active")) AS CustomerStatus
FROM tblSales
GROUP BY tblSales.ClientID;

But, when I used
DoCmd.OpenForm "frmMain", , DLookup("[ClientID]", "qryOnAirActive", '[CustomerStatus] = 'Active'")
I get only one record and it is not active. I've tried all kinds of variations for the criteria with no good results, usually an error message.

The query works fine, I just can't get it to work with the Docmd.OpenForm

Any suggestions?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:26
Joined
Aug 30, 2003
Messages
36,125
Try

DoCmd.OpenForm "frmMain", ,, "[CustomerStatus] = 'Active'"
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:26
Joined
Jan 10, 2011
Messages
904
Thanks for the quick reply. [CustomerStatus] is only in the query, qryOnAirActive, not the frmMain or tblMain. [CustomerStatus} comes from the query based on a related table (ClientID = ClientId), tblSales. The field and control [Class] is from tblMain.
The query gives me the desired results, only one entry for each client that is "Active", but it won't work in the DLookup criteria.
 

nanscombe

Registered User.
Local time
Today, 22:26
Joined
Nov 12, 2011
Messages
1,082
I think it's a bit more complicated than that.

I suspect that the Form is based on a Client table where a calculated field is updated from the Sales table.

I think you'll have to come up with a query based on the Client and Sales tables combined.


I don't thin you could produce a form which was updatable using a combination of tblMain and query qryOnAirActive.

But what you might able to do is create a temporary table based on the values returned by qryOnAirActive.
 
Last edited:

Eljefegeneo

Still trying to learn
Local time
Today, 14:26
Joined
Jan 10, 2011
Messages
904
I've changed the query to get the ClientID from tblMain instead of the tblSales.
SELECT tblMain.ClientID, Min(IIf([ContractsEnds]<Date() Or [DateCancelled]<Date(),"Inactive","Active")) AS CustomerStatus
FROM tblSales INNER JOIN tblMain ON tblSales.ClientID = tblMain.ClientID
GROUP BY tblMain.ClientID;

But I still can't get the Dlookup to work. Am I just trying to do something that can't ge done?

DoCmd.OpenForm "frmMain", , DLookup("[ClientID]", "qryOnAirActive", "[CustomerStatus] = 'Active'")
 

nanscombe

Registered User.
Local time
Today, 22:26
Joined
Nov 12, 2011
Messages
1,082
Sorry about that, my train of thought got interrupted, anyway ...

(The next post below might be more useful)

Did you still want to be able to update the records of tblMain through this form?

If so there may be a solution without adding a marker to the record itself, which would ultimately be easier.


A query with the SQL below would produce a table called tmpOnAirActive which would hold the IDs of all the clients who were Active.

Code:
SELECT qryOnAirActive.ClientID INTO tmpOnAirActive
FROM qryOnAirActive
WHERE (((qryOnAirActive.CustomerStatus)="Active"));

Combining this table with tblSales would make an updatable query which could be used to give only Active clients.

Code:
SELECT tblMain.*
FROM tblMain INNER JOIN tmpOnAirActive ON tblMain.ClientId = tmpOnAirActive.ClientID;

The only trouble is you would need to keep the temporary table up to date.
 
Last edited:

nanscombe

Registered User.
Local time
Today, 22:26
Joined
Nov 12, 2011
Messages
1,082
Oh, I've just tried something else that I've never tried before ..

Code:
DoCmd.OpenForm "frmMain", , , "ClientId [COLOR="Red"]In(Select ClientId FROM qryOnAirActive WHERE CustomerStatus='Active')[/COLOR]"

I've only got two records to try it with but it seems to work.
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:26
Joined
Jan 10, 2011
Messages
904
You are SO GOOD! Works like a charm. Could you possibly explain how this is different from a DLookup statement?
 

nanscombe

Registered User.
Local time
Today, 22:26
Joined
Nov 12, 2011
Messages
1,082
What I've done is use the result of your query (qryOnAirActive) as a subquery in the Where clause of the DoCmd.OpenForm statement.

Code:
[URL="http://msdn.microsoft.com/en-us/library/office/ff820845.aspx"]DoCmd.OpenForm[/URL](FormName, View, FilterName, [COLOR="Red"]WhereCondition[/COLOR], DataMode, WindowMode, OpenArgs)

Code:
SELECT columns1 FROM table1 WHERE id1 IN(SELECT id2 FROM table2 WHERE condition2)

What has happened is the form is now based upon the sql statement using the WhereCondition supplied in the DoCmd.OpenForm statement above.

Code:
SELECT * FROM tblMain
WHERE ClientID IN(SELECT ClientId FROM qryOnAirActive WHERE CustomerStatus = 'Active')
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:26
Joined
Jan 10, 2011
Messages
904
Thanks again. Very easy to understand your explanation.
 

Users who are viewing this thread

Top Bottom