Populate Field Based on Value of Previous Record

M

Marie

Hello,
I am working on a project that keeps track of our fleet of buses. One thing
we need to keep track of is each time a bus fuels up, we need the previous
odometer reading, the current odometer reading, and the amount of gallons
used to full the tank. I then have a calculated field on the form only to
show the amount of miles driven between each fuel up. Right now, I have a
Vehicle table, and a Mileage table. It is one to many. I have a form for the
vehicles, and a subform for the mileage. On the subform, these are the
fields: FillupDate, PreviousODO, CurrentODO, and Gallons. I would like to
have the Current ODO populate the next record for the same vehicle in the
PreviousODO field. I have looked for the answer on this forum, but so far
nothing has worked for me. The VehID field is text, based on the VIN number.
I appreciate any help that you can give me.
 
J

Jeanette Cunningham

Hi Marie,
I suggest that you make a small change to the Mileage table.
Remove the field called PreviousODO - you can always find the value for
ProviousODO without storing it twice in that table.
It is stored twice because it is first entered as CurrentODO, then in the
next record it is repeated again but this time called PreviousODO.

When you need to find the PreviousODO to use for your calculation, you can
look it up using DLookup and the most recent date.

Create a query based on the mileage table, using the primary key field, the
CurrentODO and the FillupDate.
Change the query to a totals query and in the totals row under FillupDate,
put Max.
This will give you the previous ODO for the most recent date.
Post back if you need help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
M

Marie

Jeanette Cunningham said:
Hi Marie,
I suggest that you make a small change to the Mileage table.
Remove the field called PreviousODO - you can always find the value for
ProviousODO without storing it twice in that table.
It is stored twice because it is first entered as CurrentODO, then in the
next record it is repeated again but this time called PreviousODO.

When you need to find the PreviousODO to use for your calculation, you can
look it up using DLookup and the most recent date.

Create a query based on the mileage table, using the primary key field, the
CurrentODO and the FillupDate.
Change the query to a totals query and in the totals row under FillupDate,
put Max.
This will give you the previous ODO for the most recent date.
Post back if you need help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia





Thank you for your reply, Jeanette. When I did as you suggested, the datasheet did not show beneath the titles.. Also, where do I put the dlookup? Thanks.
 
J

Jeanette Cunningham

You will need to change the query that the form is based on - it is probably
looking for the value for previous odo and can't find it.
Get the datasheet to work properly, then we can help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
M

Marie

Jeanette Cunningham said:
You will need to change the query that the form is based on - it is probably
looking for the value for previous odo and can't find it.
Get the datasheet to work properly, then we can help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
I deleted PreviousODO from the Mileage table and created a new query. I then
based the subform on the new query. If I disengage the totals button, the
daasheet shows. As soon as I add totals, the datasheet disappears. The main
query is based on the Vehicles table.
 
J

Jeanette Cunningham

Mmm ..
base the data sheet on the normal query, not the totals query.
The totals query is used to get the previous odo.

Here is code you can put into the Load event for your subform.

Private Sub Form_Load()
dim dtePrev As Date
Dim lngPrevOdo as Long
Dim strCriteria as String

dtePrev = Nz(DLookup("[NameOfTotalsDateField]", "NameOfTotalsQuery"),0)
Debug.Print dtePrev
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & ""
Debug.Print strCriteria
lngPrevOdo = DLookup("[NameOfOdoField]", "[NameOfMileageTable]",
strCriteria)
Debug.Print lngPrevOdo
End Sub


Use the value for lngPrevOdo in your calculation for mileage.
If you wish to show the previous odo on the form, you can make an unbound
textbox and put its control source
=Nz(lngPrevOdo,0)

Replace the obvious with your object and control names.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
J

Jeanette Cunningham

Oops,
you also need a function to get SQLDate

Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler
pstrProc = "SQLDate"

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


--
Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Jeanette Cunningham said:
Mmm ..
base the data sheet on the normal query, not the totals query.
The totals query is used to get the previous odo.

Here is code you can put into the Load event for your subform.

Private Sub Form_Load()
dim dtePrev As Date
Dim lngPrevOdo as Long
Dim strCriteria as String

dtePrev = Nz(DLookup("[NameOfTotalsDateField]", "NameOfTotalsQuery"),0)
Debug.Print dtePrev
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & ""
Debug.Print strCriteria
lngPrevOdo = DLookup("[NameOfOdoField]", "[NameOfMileageTable]",
strCriteria)
Debug.Print lngPrevOdo
End Sub


Use the value for lngPrevOdo in your calculation for mileage.
If you wish to show the previous odo on the form, you can make an unbound
textbox and put its control source
=Nz(lngPrevOdo,0)

Replace the obvious with your object and control names.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Marie said:
I deleted PreviousODO from the Mileage table and created a new query. I
then
based the subform on the new query. If I disengage the totals button, the
daasheet shows. As soon as I add totals, the datasheet disappears. The
main
query is based on the Vehicles table.
 
J

Jeanette Cunningham

Just remembered I need to change that function, because I have written it to
use my own error handler.

Here is the changed code:
Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

Jeanette Cunningham said:
Oops,
you also need a function to get SQLDate

Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler
pstrProc = "SQLDate"

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


--
Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Jeanette Cunningham said:
Mmm ..
base the data sheet on the normal query, not the totals query.
The totals query is used to get the previous odo.

Here is code you can put into the Load event for your subform.

Private Sub Form_Load()
dim dtePrev As Date
Dim lngPrevOdo as Long
Dim strCriteria as String

dtePrev = Nz(DLookup("[NameOfTotalsDateField]", "NameOfTotalsQuery"),0)
Debug.Print dtePrev
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & ""
Debug.Print strCriteria
lngPrevOdo = DLookup("[NameOfOdoField]", "[NameOfMileageTable]",
strCriteria)
Debug.Print lngPrevOdo
End Sub


Use the value for lngPrevOdo in your calculation for mileage.
If you wish to show the previous odo on the form, you can make an unbound
textbox and put its control source
=Nz(lngPrevOdo,0)

Replace the obvious with your object and control names.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Marie said:
:

You will need to change the query that the form is based on - it is
probably
looking for the value for previous odo and can't find it.
Get the datasheet to work properly, then we can help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



:

Hi Marie,
I suggest that you make a small change to the Mileage table.
Remove the field called PreviousODO - you can always find the value
for
ProviousODO without storing it twice in that table.
It is stored twice because it is first entered as CurrentODO, then
in the
next record it is repeated again but this time called PreviousODO.

When you need to find the PreviousODO to use for your calculation,
you
can
look it up using DLookup and the most recent date.

Create a query based on the mileage table, using the primary key
field,
the
CurrentODO and the FillupDate.
Change the query to a totals query and in the totals row under
FillupDate,
put Max.
This will give you the previous ODO for the most recent date.
Post back if you need help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Hello,
I am working on a project that keeps track of our fleet of buses.
One
thing
we need to keep track of is each time a bus fuels up, we need the
previous
odometer reading, the current odometer reading, and the amount of
gallons
used to full the tank. I then have a calculated field on the form
only
to
show the amount of miles driven between each fuel up. Right now, I
have
a
Vehicle table, and a Mileage table. It is one to many. I have a
form
for
the
vehicles, and a subform for the mileage. On the subform, these are
the
fields: FillupDate, PreviousODO, CurrentODO, and Gallons. I would
like
to
have the Current ODO populate the next record for the same vehicle
in
the
PreviousODO field. I have looked for the answer on this forum, but
so
far
nothing has worked for me. The VehID field is text, based on the
VIN
number.
I appreciate any help that you can give me.



Thank you for your reply, Jeanette. When I did as you suggested, the
datasheet did not show beneath the titles.. Also, where do I put the
dlookup? Thanks.


I deleted PreviousODO from the Mileage table and created a new query. I
then
based the subform on the new query. If I disengage the totals button,
the
daasheet shows. As soon as I add totals, the datasheet disappears. The
main
query is based on the Vehicles table.
 
M

Marie

Jeanette Cunningham said:
Just remembered I need to change that function, because I have written it to
use my own error handler.

Here is the changed code:
Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

Jeanette Cunningham said:
Oops,
you also need a function to get SQLDate

Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler
pstrProc = "SQLDate"

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


--
Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Jeanette Cunningham said:
Mmm ..
base the data sheet on the normal query, not the totals query.
The totals query is used to get the previous odo.

Here is code you can put into the Load event for your subform.

Private Sub Form_Load()
dim dtePrev As Date
Dim lngPrevOdo as Long
Dim strCriteria as String

dtePrev = Nz(DLookup("[NameOfTotalsDateField]", "NameOfTotalsQuery"),0)
Debug.Print dtePrev
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & ""
Debug.Print strCriteria
lngPrevOdo = DLookup("[NameOfOdoField]", "[NameOfMileageTable]",
strCriteria)
Debug.Print lngPrevOdo
End Sub


Use the value for lngPrevOdo in your calculation for mileage.
If you wish to show the previous odo on the form, you can make an unbound
textbox and put its control source
=Nz(lngPrevOdo,0)

Replace the obvious with your object and control names.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia




:

You will need to change the query that the form is based on - it is
probably
looking for the value for previous odo and can't find it.
Get the datasheet to work properly, then we can help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



:

Hi Marie,
I suggest that you make a small change to the Mileage table.
Remove the field called PreviousODO - you can always find the value
for
ProviousODO without storing it twice in that table.
It is stored twice because it is first entered as CurrentODO, then
in the
next record it is repeated again but this time called PreviousODO.

When you need to find the PreviousODO to use for your calculation,
you
can
look it up using DLookup and the most recent date.

Create a query based on the mileage table, using the primary key
field,
the
CurrentODO and the FillupDate.
Change the query to a totals query and in the totals row under
FillupDate,
put Max.
This will give you the previous ODO for the most recent date.
Post back if you need help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Hello,
I am working on a project that keeps track of our fleet of buses.
One
thing
we need to keep track of is each time a bus fuels up, we need the
previous
odometer reading, the current odometer reading, and the amount of
gallons
used to full the tank. I then have a calculated field on the form
only
to
show the amount of miles driven between each fuel up. Right now, I
have
a
Vehicle table, and a Mileage table. It is one to many. I have a
form
for
the
vehicles, and a subform for the mileage. On the subform, these are
the
fields: FillupDate, PreviousODO, CurrentODO, and Gallons. I would
like
to
have the Current ODO populate the next record for the same vehicle
in
the
PreviousODO field. I have looked for the answer on this forum, but
so
far
nothing has worked for me. The VehID field is text, based on the
VIN
number.
I appreciate any help that you can give me.



Thank you for your reply, Jeanette. When I did as you suggested, the
datasheet did not show beneath the titles.. Also, where do I put the
dlookup? Thanks.


I deleted PreviousODO from the Mileage table and created a new query. I
then
based the subform on the new query. If I disengage the totals button,
the
daasheet shows. As soon as I add totals, the datasheet disappears. The
main
query is based on the Vehicles table.

OK, I followed your instructions, and I now have the datasheet back to
normal. I created the module and named it SQLDate, and I put the code you
wrote in the LoadEvent of the subform. This is the error message I received
when I opened the form:
Compile Error
Expected variable or procedure, not module
This is the line it referred to:
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & "")
and it had the SQLDate word highlighted.
Also, I did use the corrected module you included. I wish I could figure out
what the problem is.........
 
J

Jeanette Cunningham

There are some errors on this line
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & "")
Sorry, I did not test this code, and now I see a typo.

strCriteria = "[FillupDate] = " & SQLDate(dtePrev) & ""


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Marie said:
Jeanette Cunningham said:
Just remembered I need to change that function, because I have written it
to
use my own error handler.

Here is the changed code:
Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively
by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

Jeanette Cunningham said:
Oops,
you also need a function to get SQLDate

Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively
by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler
pstrProc = "SQLDate"

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


--
Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Mmm ..
base the data sheet on the normal query, not the totals query.
The totals query is used to get the previous odo.

Here is code you can put into the Load event for your subform.

Private Sub Form_Load()
dim dtePrev As Date
Dim lngPrevOdo as Long
Dim strCriteria as String

dtePrev = Nz(DLookup("[NameOfTotalsDateField]",
"NameOfTotalsQuery"),0)
Debug.Print dtePrev
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & ""
Debug.Print strCriteria
lngPrevOdo = DLookup("[NameOfOdoField]", "[NameOfMileageTable]",
strCriteria)
Debug.Print lngPrevOdo
End Sub


Use the value for lngPrevOdo in your calculation for mileage.
If you wish to show the previous odo on the form, you can make an
unbound
textbox and put its control source
=Nz(lngPrevOdo,0)

Replace the obvious with your object and control names.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia




:

You will need to change the query that the form is based on - it is
probably
looking for the value for previous odo and can't find it.
Get the datasheet to work properly, then we can help with the
dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



:

Hi Marie,
I suggest that you make a small change to the Mileage table.
Remove the field called PreviousODO - you can always find the
value
for
ProviousODO without storing it twice in that table.
It is stored twice because it is first entered as CurrentODO,
then
in the
next record it is repeated again but this time called
PreviousODO.

When you need to find the PreviousODO to use for your
calculation,
you
can
look it up using DLookup and the most recent date.

Create a query based on the mileage table, using the primary key
field,
the
CurrentODO and the FillupDate.
Change the query to a totals query and in the totals row under
FillupDate,
put Max.
This will give you the previous ODO for the most recent date.
Post back if you need help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Hello,
I am working on a project that keeps track of our fleet of
buses.
One
thing
we need to keep track of is each time a bus fuels up, we need
the
previous
odometer reading, the current odometer reading, and the amount
of
gallons
used to full the tank. I then have a calculated field on the
form
only
to
show the amount of miles driven between each fuel up. Right
now, I
have
a
Vehicle table, and a Mileage table. It is one to many. I have a
form
for
the
vehicles, and a subform for the mileage. On the subform, these
are
the
fields: FillupDate, PreviousODO, CurrentODO, and Gallons. I
would
like
to
have the Current ODO populate the next record for the same
vehicle
in
the
PreviousODO field. I have looked for the answer on this forum,
but
so
far
nothing has worked for me. The VehID field is text, based on
the
VIN
number.
I appreciate any help that you can give me.



Thank you for your reply, Jeanette. When I did as you suggested,
the
datasheet did not show beneath the titles.. Also, where do I put
the
dlookup? Thanks.


I deleted PreviousODO from the Mileage table and created a new query.
I
then
based the subform on the new query. If I disengage the totals button,
the
daasheet shows. As soon as I add totals, the datasheet disappears.
The
main
query is based on the Vehicles table.

OK, I followed your instructions, and I now have the datasheet back to
normal. I created the module and named it SQLDate, and I put the code you
wrote in the LoadEvent of the subform. This is the error message I
received
when I opened the form:
Compile Error
Expected variable or procedure, not module
This is the line it referred to:
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & "")
and it had the SQLDate word highlighted.
Also, I did use the corrected module you included. I wish I could figure
out
what the problem is.........
 
M

Marie

Jeanette Cunningham said:
There are some errors on this line
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & "")
Sorry, I did not test this code, and now I see a typo.

strCriteria = "[FillupDate] = " & SQLDate(dtePrev) & ""


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Marie said:
Jeanette Cunningham said:
Just remembered I need to change that function, because I have written it
to
use my own error handler.

Here is the changed code:
Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively
by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

Oops,
you also need a function to get SQLDate

Paste all the code below into a new module
'start of SQLDate code ------------------------------------
Option Compare Database
Option Explicit

Public Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively
by
JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time
component,
' or a date/time format if it does.
'Author: Allen Browne. (e-mail address removed), June 2006.
On Error GoTo Err_Handler
pstrProc = "SQLDate"

If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Function

'endof SQLDate code ------------------------------------


--
Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Mmm ..
base the data sheet on the normal query, not the totals query.
The totals query is used to get the previous odo.

Here is code you can put into the Load event for your subform.

Private Sub Form_Load()
dim dtePrev As Date
Dim lngPrevOdo as Long
Dim strCriteria as String

dtePrev = Nz(DLookup("[NameOfTotalsDateField]",
"NameOfTotalsQuery"),0)
Debug.Print dtePrev
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & ""
Debug.Print strCriteria
lngPrevOdo = DLookup("[NameOfOdoField]", "[NameOfMileageTable]",
strCriteria)
Debug.Print lngPrevOdo
End Sub


Use the value for lngPrevOdo in your calculation for mileage.
If you wish to show the previous odo on the form, you can make an
unbound
textbox and put its control source
=Nz(lngPrevOdo,0)

Replace the obvious with your object and control names.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia




:

You will need to change the query that the form is based on - it is
probably
looking for the value for previous odo and can't find it.
Get the datasheet to work properly, then we can help with the
dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



:

Hi Marie,
I suggest that you make a small change to the Mileage table.
Remove the field called PreviousODO - you can always find the
value
for
ProviousODO without storing it twice in that table.
It is stored twice because it is first entered as CurrentODO,
then
in the
next record it is repeated again but this time called
PreviousODO.

When you need to find the PreviousODO to use for your
calculation,
you
can
look it up using DLookup and the most recent date.

Create a query based on the mileage table, using the primary key
field,
the
CurrentODO and the FillupDate.
Change the query to a totals query and in the totals row under
FillupDate,
put Max.
This will give you the previous ODO for the most recent date.
Post back if you need help with the dlookup.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Hello,
I am working on a project that keeps track of our fleet of
buses.
One
thing
we need to keep track of is each time a bus fuels up, we need
the
previous
odometer reading, the current odometer reading, and the amount
of
gallons
used to full the tank. I then have a calculated field on the
form
only
to
show the amount of miles driven between each fuel up. Right
now, I
have
a
Vehicle table, and a Mileage table. It is one to many. I have a
form
for
the
vehicles, and a subform for the mileage. On the subform, these
are
the
fields: FillupDate, PreviousODO, CurrentODO, and Gallons. I
would
like
to
have the Current ODO populate the next record for the same
vehicle
in
the
PreviousODO field. I have looked for the answer on this forum,
but
so
far
nothing has worked for me. The VehID field is text, based on
the
VIN
number.
I appreciate any help that you can give me.



Thank you for your reply, Jeanette. When I did as you suggested,
the
datasheet did not show beneath the titles.. Also, where do I put
the
dlookup? Thanks.


I deleted PreviousODO from the Mileage table and created a new query.
I
then
based the subform on the new query. If I disengage the totals button,
the
daasheet shows. As soon as I add totals, the datasheet disappears.
The
main
query is based on the Vehicles table.

OK, I followed your instructions, and I now have the datasheet back to
normal. I created the module and named it SQLDate, and I put the code you
wrote in the LoadEvent of the subform. This is the error message I
received
when I opened the form:
Compile Error
Expected variable or procedure, not module
This is the line it referred to:
strCriteria = "[FillupDate] = " & SQLDate(detPrev0 & "")
and it had the SQLDate word highlighted.
Also, I did use the corrected module you included. I wish I could figure
out
what the problem is.........

I changed the typo's and I was then getting a Runtime Error 13, Type
Mismatch error. I then went in and changed the Totals Query Field to
MaxOfFuelFillupDate instead of FuelFillupDate. I am no longer getting a
runtime error, but the field that I created for the Previous ODO has #Name?
in it. Also, shouldn't there be some kind of reference to VehicleID since I
am looking for information about the vehicle?
 
J

Jeanette Cunningham

#Name error
To put the value for previous odo in the textbox you need a line of code.
Put it in the load event for the form if a single form,
(the current event if a continuous form)

Me.NameOfControl = lngPrevOdo

Yes, you will need a reference to the vehicle id in the criteria for the
Dlookup statement.

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = " & Me.VehicleID & ""

assuming VehicleID is a number.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
M

Marie

Jeanette Cunningham said:
#Name error
To put the value for previous odo in the textbox you need a line of code.
Put it in the load event for the form if a single form,
(the current event if a continuous form)

Me.NameOfControl = lngPrevOdo

Yes, you will need a reference to the vehicle id in the criteria for the
Dlookup statement.

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = " & Me.VehicleID & ""

assuming VehicleID is a number.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

I changed the typo's and I was then getting a Runtime Error 13, Type
Mismatch error. I then went in and changed the Totals Query Field to
MaxOfFuelFillupDate instead of FuelFillupDate. I am no longer getting a
runtime error, but the field that I created for the Previous ODO has
#Name?
in it. Also, shouldn't there be some kind of reference to VehicleID since
I
am looking for information about the vehicle?

The VehicleID is text and numbers mixed.
 
J

Jeanette Cunningham

Ok, use this -->

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = """ & Me.VehicleID & """"


the end of the last line has 3 double quotes followed by
& Me.VehicleID &
followed by 4 double quotes.

here it is expanded for clarity:
" " " & Me.VehicleID & " " " "


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



Marie said:
Jeanette Cunningham said:
#Name error
To put the value for previous odo in the textbox you need a line of code.
Put it in the load event for the form if a single form,
(the current event if a continuous form)

Me.NameOfControl = lngPrevOdo

Yes, you will need a reference to the vehicle id in the criteria for the
Dlookup statement.

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = " & Me.VehicleID & ""

assuming VehicleID is a number.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

I changed the typo's and I was then getting a Runtime Error 13, Type
Mismatch error. I then went in and changed the Totals Query Field to
MaxOfFuelFillupDate instead of FuelFillupDate. I am no longer getting a
runtime error, but the field that I created for the Previous ODO has
#Name?
in it. Also, shouldn't there be some kind of reference to VehicleID
since
I
am looking for information about the vehicle?

The VehicleID is text and numbers mixed.
 
M

Marie

Jeanette Cunningham said:
Ok, use this -->

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = """ & Me.VehicleID & """"


the end of the last line has 3 double quotes followed by
& Me.VehicleID &
followed by 4 double quotes.

here it is expanded for clarity:
" " " & Me.VehicleID & " " " "


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



Marie said:
Jeanette Cunningham said:
#Name error
To put the value for previous odo in the textbox you need a line of code.
Put it in the load event for the form if a single form,
(the current event if a continuous form)

Me.NameOfControl = lngPrevOdo

Yes, you will need a reference to the vehicle id in the criteria for the
Dlookup statement.

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = " & Me.VehicleID & ""

assuming VehicleID is a number.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



I changed the typo's and I was then getting a Runtime Error 13, Type
Mismatch error. I then went in and changed the Totals Query Field to
MaxOfFuelFillupDate instead of FuelFillupDate. I am no longer getting a
runtime error, but the field that I created for the Previous ODO has
#Name?
in it. Also, shouldn't there be some kind of reference to VehicleID
since
I
am looking for information about the vehicle?

The VehicleID is text and numbers mixed.

I followed your instructions, and I'm happy to say, that after tweaking it a
little it works! Thank you so much for all your help.
 
J

Jeanette Cunningham

Great, result.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Marie said:
Jeanette Cunningham said:
Ok, use this -->

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = """ & Me.VehicleID & """"


the end of the last line has 3 double quotes followed by
& Me.VehicleID &
followed by 4 double quotes.

here it is expanded for clarity:
" " " & Me.VehicleID & " " " "


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



Marie said:
:

#Name error
To put the value for previous odo in the textbox you need a line of
code.
Put it in the load event for the form if a single form,
(the current event if a continuous form)

Me.NameOfControl = lngPrevOdo

Yes, you will need a reference to the vehicle id in the criteria for
the
Dlookup statement.

strCriteria = "[MaxOfFuelFillupDate] = " & SQLDate(detPrev) & " " _
& "AND [VehicleID] = " & Me.VehicleID & ""

assuming VehicleID is a number.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia



I changed the typo's and I was then getting a Runtime Error 13, Type
Mismatch error. I then went in and changed the Totals Query Field to
MaxOfFuelFillupDate instead of FuelFillupDate. I am no longer
getting a
runtime error, but the field that I created for the Previous ODO has
#Name?
in it. Also, shouldn't there be some kind of reference to VehicleID
since
I
am looking for information about the vehicle?




The VehicleID is text and numbers mixed.

I followed your instructions, and I'm happy to say, that after tweaking it
a
little it works! Thank you so much for all your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top