473,486 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Open a form on a record closest to the current date

8 New Member
I have a form (single record form) that loads records from a query that are sorted date order as the records are viewed in date order. Some dates are in the past and some are in the future. Currently the form opens on the last record and the date of that record can be 6 months out. The users would like the form to open on the record closest to the current date. Does anyone know how I can achieve this?

Thanks
Feb 29 '08 #1
7 6516
Delerna
1,134 Recognized Expert Top Contributor
In vba
query the table to find the date that is closest to todays date
ie
Expand|Select|Wrap|Line Numbers
  1. SELECT max(Dte) as Dte FROM theTable WHERE Dte<=now()
  2.  
Put the date retrieved into a variable.
Now just do a find record for the date that was returned

I haven't put any specific code here, just the general idea.
Is that enough for you to do it?
If not, ask and I will post a fuller example.
Feb 29 '08 #2
GraemeC
8 New Member
In vba
query the table to find the date that is closest to todays date
ie
Expand|Select|Wrap|Line Numbers
  1. SELECT max(Dte) as Dte FROM theTable WHERE Dte<=now()
  2.  
Put the date retrieved into a variable.
Now just do a find record for the date that was returned

I haven't put any specific code here, just the general idea.
Is that enough for you to do it?
If not, ask and I will post a fuller example.

Thanks for this but yes a fuller example will be much appreciated.
Feb 29 '08 #3
Delerna
1,134 Recognized Expert Top Contributor
OK
Two questions and once I have the answers I will do a mockup and post back
1) What version of access are you using
2) Do you have much experience with VBA
Feb 29 '08 #4
GraemeC
8 New Member
Hi

We're using Access 2003.

I use VBA in Excel a lot but my experience of using VBA in Access is a slightly less.

As an aside I have got a query to determine the appropriate date in my table but now just help putting that into the form so that the form opens on that date. My question now is where do I put the select statement you gave me or what os the best way to call it when the form opens?

Thanks again.
Mar 1 '08 #5
Delerna
1,134 Recognized Expert Top Contributor
Put the following into the forms code page and adjust to suit your database
you will probably need to add a reference to the Microsoft DAO 3.6 Object library.
ADO is the better way to go but I chose DAO for you because I think its a little easier to learn. Others may disagree.
If you want to use ADO I suggest you check the help file for access.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim db As DAO.Database
  3.     Dim rst As DAO.Recordset
  4.     Dim strSQL As String
  5.     Dim TheDate As Date
  6.  
  7.     Set db = CurrentDb 
  8.     strSQL = "SELECT max(Dte) as Dte FROM theTable WHERE Dte<=now()"
  9.     Set rst = db.OpenRecordset(strSQL)
  10.     TheDate = rst.Fields(0)
  11.     TheControlForTheDateField.SetFocus
  12.     DoCmd.FindRecord TheDate, , True, , True
  13.     Set rst = Nothing
  14.     Set db = Nothing
  15. End Sub
  16.  
Mar 1 '08 #6
GraemeC
8 New Member
Thank you very much. That works perfectly.

Regards

Graeme
Mar 1 '08 #7
Delerna
1,134 Recognized Expert Top Contributor
You are very welcome
Mar 2 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

18
13557
by: Darryl Kerkeslager | last post by:
I don't do much with subforms - in fact I've deliberately avoided them - but .... I have a tab control that will contain several subforms, each bound to a separate table, and each table/subform...
1
3061
by: SorboPos | last post by:
Hi. I have a form with continuous forms and a data entry section in the header of the form. (I.e. all transactions show in the main form area like a data sheet and the data for the highlighed...
1
2947
by: 4004 | last post by:
I would like to open a columnar form (so I can see all the details) from a datasheet form (so I can see what is there) but keep the same recordset and current record. I can do the recordset set...
5
3917
by: ortaias | last post by:
I have a form which calls up a second form for purposes of data entry. When closing the data entry form and returning to the main form, things don't work as expected. When I return to the main...
3
2057
by: cyber0ne | last post by:
I'm designing a basic form for data entry into one main table. There are two fields in the table that I would like to be automatically populated, not user-entered, when the record is posted. ...
14
24975
by: keri | last post by:
Hi, Simple version of the question..... How do I use the where clause of the open form command to show an account with a matching ID to be displayed when the form is opened? Eg. I select a...
1
1794
by: cbanks | last post by:
Hello Ladies/Gents, im in need of some serious help here.. I have a tool that allows users to view information on a form and make changes to recordsets. This tool has buttons that allow a user to...
0
1755
by: trixxnixon | last post by:
i have a form that is being designed to pend requests in a requests database. the pend form is opened from an update form used by an employee to enter updates. when the pend form is updated, the...
16
5951
by: GLEberts | last post by:
Subject: Trying to open up a record in a form called "fmappointment" when clicking on a "looks like hyperlink" text box called "ContactID" I have made many instances of this in my database but in...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.