Solved On Load (1 Viewer)

SzymonK

New member
Local time
Today, 08:17
Joined
Mar 29, 2022
Messages
14
Hi All
I hope you're well
How can I wright an expression for the form Event / on load so that I get the following:
Load the last saved record in table(TblMainAndPlaner) by a specific SiteId. both these data are in the same table.
For example: Load last record where SiteId = 2
If you think that this is not the best solution, please can you show me a better option.

TIA
Szymon
 

CJ_London

Super Moderator
Staff member
Local time
Today, 08:17
Joined
Feb 19, 2013
Messages
16,663
need more information

Load the last saved record in table(TblMainAndPlaner) by a specific SiteId.
what type of date is siteID? long? text? something else?
how are you identifying siteID? from another form? user input? something else?
when might the last record have been saved? a moment ago? last week?
what field in your table identifies last? timestamp? something else?

Note that in the context of databases, Last means nothing without also stating an order since data is stored randomly in a table.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:17
Joined
May 7, 2009
Messages
19,246
if table TblMainAndPlaner has an Autonumber (ID) field you can can use a Query or SQL statement as
your Form's recordsource:

Select TblMainAndPlaner.* from TblMainAndPlaner Where ID = DMax("ID", "TblMainAndPlaner", "SiteID = 2");
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:17
Joined
Feb 28, 2001
Messages
27,308
First, if the idea is to open the form at a particular record as a starting point for your work, then OnLoad is as good a place as any.

Second, as arnelgp suggests, you can certainly do something to make the form select a record.

Third, though, is CJ London's question - from where would you get the identifying information? I know you said the fields are in the table. But why ID 2, and would it ever be ID 3 or ID 1 or something like that?

For that reason, your question is a little bit ambiguous. Please be just a LITTLE more specific about how to decide what you want at OnLoad time.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:17
Joined
Feb 19, 2002
Messages
43,457
What if you want to view siteID =3? This seems like a very inflexible requirement. I'm pretty sure you would want to do this on the form that opens the form we're talking about and use the Where argument.

By "last" we're going to assume either the highest autonumber or otherwise, you'll need to have a date added field that includes time and then "last" refers to the highest value for that field.
 

SzymonK

New member
Local time
Today, 08:17
Joined
Mar 29, 2022
Messages
14
What if you want to view siteID =3? This seems like a very inflexible requirement. I'm pretty sure you would want to do this on the form that opens the form we're talking about and use the Where argument.

By "last" we're going to assume either the highest autonumber or otherwise, you'll need to have a date added field that includes time and then "last" refers to the highest value for that field.
Thank you very much for all the hints, but unfortunately I still haven't picked up on the problem. Sorry to bother you, but I've only been in Access for 6 months.

I'm sorry it's my fault. I have never been able to explain exactly what I mean. So, this is: when the user opens access, The form is popup that allows him to choose the shift option.
1649664630123.png

Then, by pressing the "New Record" button, the user is directed to the "Form1", which is built based on the "TblMainAndPlaner" table. A new record is then opened using the macro button-> event-> on click-> open form->(Form1) Where condition -> = "[ShiftDate] =" & "#" & [Auto_Date] & "#".
the date automatically appears. Also the site ID (Text box) is set as default Data-> Default value = 3.
1649664860337.png
All this works fine if we are talking about opening a new record. The date and SiteID come from one table, "TblMainAndPlaner". Please see the screenshot.
1649664963664.png

However, I have a question, how can I construct the code for the "re-open current Shift" button
1649665029689.png

so that the user, in the event of accidentally closing the form, could return to the record he worked on. The macro button must contain the code that will open the last record where (which I do not understand how to do) SiteID = 3. "SideID = 3" I gave as an example, all Id is 20. So depending on the copy, I can change siteID. I entered this Makro code, but unfortunately, it does not work:
buton-> event-> on click-> open form->(Form1) Where condition -> = DMax ("SiteID", "TblMainAndPlaner", "SiteID = 3").

Please can you help me with this, I'm completely stuck :(
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:17
Joined
May 7, 2009
Messages
19,246
if you are using Query in your "Form1" (tblMainAndPlanner), you include UpdateID field.
change your condition:

Where UpdateID = DMax("UpdateID", "tblMainAndPlaner", "SiteID = 3")
 

SzymonK

New member
Local time
Today, 08:17
Joined
Mar 29, 2022
Messages
14
if you are using Query in your "Form1" (tblMainAndPlanner), you include UpdateID field.
change your condition:

Where UpdateID = DMax("UpdateID", "tblMainAndPlaner", "SiteID = 3")
Thank you for your help. Unfortunately, for some reason, it opens the form at the first record. I don't know what I'm doing wrong
1649667211502.png

1649667304803.png

1649667365023.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:17
Joined
Sep 21, 2011
Messages
14,424
Put that expression in the immediate window and see what it outputs.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:17
Joined
May 7, 2009
Messages
19,246
change the Where Condition"

="[UpdateID] = " & DMax("UpdateID","TblMainAndPlaner","SiteID= 3")
 

SzymonK

New member
Local time
Today, 08:17
Joined
Mar 29, 2022
Messages
14
if you are using Query in your "Form1" (tblMainAndPlanner), you include UpdateID field.
change your condition:

Where UpdateID = DMax("UpdateID", "tblMainAndPlaner", "SiteID = 3")
Thank you arnelgp!!!!
YOU'RE A STAR!!!!
Its works now.
Thank You.
 

Users who are viewing this thread

Top Bottom