Solved

Run query from an Event Procedure MS Access

Posted on 2014-12-08
3
426 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

832 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