Solved

Run query from an Event Procedure MS Access

Posted on 2014-12-08
3
381 Views
Last Modified: 2014-12-09
I have an Access form (MyForm) that has an event procedure under its On Activate event.

MyForm has a textbox on it (MyTextBox).

I have a table (MyTable) that contains a small list of specific dates.

In the On Activate event procedure, I want to query MyTable to see if the current date is one of the values in the table (field name is DateField).  If it is, set the value of MyTextBox to the current date. If it isn't, do something else.

I don't want the query results displayed in a datasheet that I have to wind up closing or anything like that. I just want to query the table for the current date value. If it's in there, put that value in the textbox and be done with the query.

I do not want to use DLookup in any way, shape or form to accomplish this. I cannot stand DLookup. It always produces a noticeable annoying delay between the time it executes and the time it populates the control.

Thank you.
0
Comment
Question by:dbfromnewjersey
3 Comments
 
LVL 12

Accepted Solution

by:
pdebaets earned 300 total points
Comment Utility
Dlookup or DCount is the straight-forward way to do this.

dim intCount as integer
intCount = DCount("*","MyTable", "format(DateField,'yyyymmdd') = '" & format(Date(),"yyyymmdd") & "'")
if intCount > 0 then
    '* The current date exists in MyTable
    MyTextBox = Date()
Else
    '* The current date does not exist in MyTable
End if

Open in new window

0
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 50 total points
Comment Utility
What pdebaets posted should get you going...

Just FYI
In some cases dcount may be just as slow (or slower) than Dlookup...
(Because Dcount will force a count of all matching values..., whereas Dlookup will only find the first match)

I cannot stand DLookup. It always produces a noticeable annoying delay between the time it executes and the time it populates the control.
I have never had a real problem with the speed of any aggregate function unless the underlying data (or container object) was too complex...

JeffCoachman
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 150 total points
Comment Utility
> I have never had a real problem with the speed of any aggregate function
> unless the underlying data (or container object) was too complex...

Neither have I, so maybe you have (had) other issues.

So, it is as simple as:

If IsNull(DLookup("DateField", "MyTable", "DateField = Date()")) Then
    ' Do something else.
Else
    Me!MyTextBox.Value = Date
End If

Of course, if you insist, you can open a recordset and check for records:

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Select DateField From MyTable Where DateField = Date()")
If rst.RecordCount = 0 Then
    ' Do something else.
Else
    Me!MyTextBox.Value = Date
End If
rst.Close
Set rst = Nothing
Set dbs = Nothing

In any case, to speed up things, add an index on DateField.

/gustav
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now