Solved

Run query from an Event Procedure MS Access

Posted on 2014-12-08
3
512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

696 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