Solved

Run query from an Event Procedure MS Access

Posted on 2014-12-08
3
398 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
ID: 40488200
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
ID: 40488244
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
ID: 40488370
> 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

895 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

17 Experts available now in Live!

Get 1:1 Help Now