Solved Text Box Default Value (1 Viewer)

Emma35

Registered User.
Local time
Today, 00:21
Joined
Sep 18, 2012
Messages
467
Hi All,
I've got a form and subform as below. The main form contains details of a piece of machinery, and the subform has details of each occasion it was serviced. When i'm entering a new record on the subform, is it possible to make the date that particular machine was last serviced appear in a text box on the subform as the default value ? So really, i'd like to see the last value in the ServiceDate field for that machine displayed in the text box

Thanks

tbl_EquipmentList
EquipID PK (Short Text)
EquipmentName (Short Text)
Manufacturer (Short Text)

tbl_ServiceRecords
ServiceID PK (Autonumber)
EquipID FK (Short Text)
ServiceDate (Date)
ServicedBy (Short Text)
PassFail (Yes/No)
 

bob fitz

AWF VIP
Local time
Today, 08:21
Joined
May 23, 2011
Messages
4,727
Try something like:

=DMax("ServiceDate","tbl_ServiceRecords","EquipID FK = '" & Me.[EquipID FK] & "'")

as the Default Value property setting of the text box
 

Emma35

Registered User.
Local time
Today, 00:21
Joined
Sep 18, 2012
Messages
467
Thanks bob......i gave it a try and a getting the #name? error in the txtbox
 

bob fitz

AWF VIP
Local time
Today, 08:21
Joined
May 23, 2011
Messages
4,727
Thanks bob......i gave it a try and a getting the #name? error in the txtbox
Perhaps:
=DMax("ServiceDate","tbl_ServiceRecords","[EquipID FK] = '" & Me.[EquipID FK] & "'")
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:21
Joined
May 7, 2009
Messages
19,247
you may also use the subform's Current event:

Private Sub Form_Current()
Me.ServiceDate.DefaultValue = Nz(DMax("ServiceDate","tbl_ServiceRecords","EquipID FK = '" & Me.[EquipID FK] & "'"), Date())
End Sub
 

Emma35

Registered User.
Local time
Today, 00:21
Joined
Sep 18, 2012
Messages
467
bob....still the same error
Gasman...thanks i did that
arnelgp......i put your code into the Form Current event and the text box now displays the date 30/12/1899 ?
 

bob fitz

AWF VIP
Local time
Today, 08:21
Joined
May 23, 2011
Messages
4,727
Emma35

I think arnelgp is on the right track but I think the code needs a pair of square brackets because you have a space in the field name.

Private Sub Form_Current()
Me.ServiceDate.DefaultValue = Nz(DMax("ServiceDate","tbl_ServiceRecords","[EquipID FK] = '" & Me.[EquipID FK] & "'"), Date())
End Sub
 

Emma35

Registered User.
Local time
Today, 00:21
Joined
Sep 18, 2012
Messages
467
bob....i probably should have said earlier but my field name is EquipID. The Fk was just when i laid out the table structure i was saying this was the foreign key. I've just been altering the code to delete it. I tried the square brackets anyway and am still getting that weird date
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:21
Joined
May 7, 2009
Messages
19,247
what is the textbox name for EquipID?, you put it on the expression

Private Sub Form_Current()
Me.ServiceDate.DefaultValue = Nz(DMax("ServiceDate","tbl_ServiceRecords", "EquipID = '" & Me.txtEquipID & "'"), Date())
End Sub
 

Emma35

Registered User.
Local time
Today, 00:21
Joined
Sep 18, 2012
Messages
467
It's just called txtEquipID. One odd thing i've noticed is that the brackets you have at the end of the code are never there when i open up the code window ? The very last one is there but the two before it (after the word Date) are always missing
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:21
Joined
May 7, 2009
Messages
19,247
Access automatically remove it.
 

Dreamweaver

Well-known member
Local time
Today, 08:21
Joined
Nov 28, 2005
Messages
2,466
Me.ServiceDate.DefaultValue = "#" & Nz(DMax("ServiceDate","tbl_ServiceRecords","[EquipID FK] = '" & Me.[EquipID FK] & "'"), Date()) & "#"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:21
Joined
Feb 28, 2001
Messages
27,209
the text box now displays the date 30/12/1899 ?

The reason you got that is because that is the Access Epoch date (reference date for the method Access uses to represent dates). So that means that something came back as 0.

If you can get to a code window you can use the menu View option to open up the Immediate Window. Do a Debug.Print of the expression to see what it returns.

Code:
Debug.Print DMax("ServiceDate","tbl_ServiceRecords","[EquipID FK] = '" & Me.[EquipID FK] & "'")

Or instead of using Me.[EquipID FK] you could provide an appropriate value. If you can't even get something back with Debug.Print then you have a problem in one of the field names in the DMax function. (Which would actually be consistent with the #name error.)
 

Dreamweaver

Well-known member
Local time
Today, 08:21
Joined
Nov 28, 2005
Messages
2,466
the text box now displays the date 30/12/1899 ?
That can also be because the value is not formated correctly which I had in another post a week or so ago I did this

Code:
Dim MyDate As Date
MyDate = #7/1/2019#
Me![Text13].DefaultValue = Format(MyDate, "\#mm\/dd\/yyyy\#")

Please note the above is set for US dates change it to \#dd\/mm\/yyyy\# for UK
 

Emma35

Registered User.
Local time
Today, 00:21
Joined
Sep 18, 2012
Messages
467
MickJav...i thought your suggestion had worked but then i tried to add a new record and the date in the Last Calibrated txt box wasn't correct. I think the best thing to do at this point is for me to strip the database and add leave the relevant forms for you guys to have a look at. I'll be about an hour or so...thanks again
 

Dreamweaver

Well-known member
Local time
Today, 08:21
Joined
Nov 28, 2005
Messages
2,466
Post a stipped down copy of the tables involved, we should be able to sort it for you
mick
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:21
Joined
May 7, 2009
Messages
19,247
on the subform's Current Event:

Code:
Private Sub Form_Current
Me.ServiceDate.DefaultValue = Format(Nz(DMax("ServiceDate", "tbl_ServiceRecords", "EquipID = '" & Me!EquipID & "'"), Date), "\#mm\/dd\/yyyy\#")
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:21
Joined
Sep 21, 2011
Messages
14,336
What is the date meant to be for the very first entry.?
Are you using an EquipID that alread exists?
 

Users who are viewing this thread

Top Bottom