L
Les
Hi, is it possible to select the last record entered, if so how ?
Thanks in advance
Thanks in advance
Allen Browne said:How do you define "last"?
Is it the highest AutoNumber value?
Or is there a date/time field?
Perhaps something like this:
Dim rs As DAO.Database
Dim strSql As String
strSql = "SELECT TOP 1 Table1.* ORDER BY [ID] DESC;"
Set rs = dbEngine(0)(0).OpenRecordset(strSql)
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Reply to group, rather than allenbrowne at mvps dot org.
Les said:Hi, is it possible to select the last record entered, if so how ?
Thanks in advance
Les said:Hi Allen, i have tried to adapt the code i got on another site but have
made
a mistake somewhere, could you help me please
Function GetReqText() As String
On Error GoTo Err_Handler
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strInfo As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tbl_CIANumberRequest. * order by [dtAdded]
DESC", dbOpenForwardOnly, dbReadOnly)
While strInfo = ""
strInfo = strInfo & rst!Requestor & vbTab
strInfo = strInfo & rst!RequestorDept & vbTab
strInfo = strInfo & rst!Amount
Wend
GetReqText = strInfo
Exit_Handler:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
If Not dbs Is Nothing Then
Set dbs = Nothing
End If
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End Function
Sub SendMail()
Dim strBody
strBody = GetReqText()
If Len(strBody) = 0 Then
strBody = "Nothing shipped today"
Else
Dim strmessage As String, stDocName
Dim stremail As String
Dim strsubject As String
strmessage = strBody
stDocName = "rpt_CIAEmail"
stremail = "[email protected]"
strsubject = "CIA Number Request"
DoCmd.SendObject , stDocName, , stremail, , , strsubject, strmessage,
True
End If
End Sub
--
Les
Allen Browne said:How do you define "last"?
Is it the highest AutoNumber value?
Or is there a date/time field?
Perhaps something like this:
Dim rs As DAO.Database
Dim strSql As String
strSql = "SELECT TOP 1 Table1.* ORDER BY [ID] DESC;"
Set rs = dbEngine(0)(0).OpenRecordset(strSql)
Les said:Hi, is it possible to select the last record entered, if so how ?
Thanks in advance
Allen Browne said:There are several things wrong, e.g. no SELECT in the SQL statement, and the
loop makes no sense.
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Reply to group, rather than allenbrowne at mvps dot org.
Les said:Hi Allen, i have tried to adapt the code i got on another site but have
made
a mistake somewhere, could you help me please
Function GetReqText() As String
On Error GoTo Err_Handler
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strInfo As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tbl_CIANumberRequest. * order by [dtAdded]
DESC", dbOpenForwardOnly, dbReadOnly)
While strInfo = ""
strInfo = strInfo & rst!Requestor & vbTab
strInfo = strInfo & rst!RequestorDept & vbTab
strInfo = strInfo & rst!Amount
Wend
GetReqText = strInfo
Exit_Handler:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
If Not dbs Is Nothing Then
Set dbs = Nothing
End If
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End Function
Sub SendMail()
Dim strBody
strBody = GetReqText()
If Len(strBody) = 0 Then
strBody = "Nothing shipped today"
Else
Dim strmessage As String, stDocName
Dim stremail As String
Dim strsubject As String
strmessage = strBody
stDocName = "rpt_CIAEmail"
stremail = "[email protected]"
strsubject = "CIA Number Request"
DoCmd.SendObject , stDocName, , stremail, , , strsubject, strmessage,
True
End If
End Sub
--
Les
Allen Browne said:How do you define "last"?
Is it the highest AutoNumber value?
Or is there a date/time field?
Perhaps something like this:
Dim rs As DAO.Database
Dim strSql As String
strSql = "SELECT TOP 1 Table1.* ORDER BY [ID] DESC;"
Set rs = dbEngine(0)(0).OpenRecordset(strSql)
Hi, is it possible to select the last record entered, if so how ?
Thanks in advance
Allen Browne said:There are several things wrong, e.g. no SELECT in the SQL statement, and the
loop makes no sense.
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Reply to group, rather than allenbrowne at mvps dot org.
Les said:Hi Allen, i have tried to adapt the code i got on another site but have
made
a mistake somewhere, could you help me please
Function GetReqText() As String
On Error GoTo Err_Handler
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strInfo As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tbl_CIANumberRequest. * order by [dtAdded]
DESC", dbOpenForwardOnly, dbReadOnly)
While strInfo = ""
strInfo = strInfo & rst!Requestor & vbTab
strInfo = strInfo & rst!RequestorDept & vbTab
strInfo = strInfo & rst!Amount
Wend
GetReqText = strInfo
Exit_Handler:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
If Not dbs Is Nothing Then
Set dbs = Nothing
End If
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End Function
Sub SendMail()
Dim strBody
strBody = GetReqText()
If Len(strBody) = 0 Then
strBody = "Nothing shipped today"
Else
Dim strmessage As String, stDocName
Dim stremail As String
Dim strsubject As String
strmessage = strBody
stDocName = "rpt_CIAEmail"
stremail = "[email protected]"
strsubject = "CIA Number Request"
DoCmd.SendObject , stDocName, , stremail, , , strsubject, strmessage,
True
End If
End Sub
--
Les
Allen Browne said:How do you define "last"?
Is it the highest AutoNumber value?
Or is there a date/time field?
Perhaps something like this:
Dim rs As DAO.Database
Dim strSql As String
strSql = "SELECT TOP 1 Table1.* ORDER BY [ID] DESC;"
Set rs = dbEngine(0)(0).OpenRecordset(strSql)
Hi, is it possible to select the last record entered, if so how ?
Thanks in advance
Les said:Hello Allen, i have made the following changes and it works ok, with the
exception of the loop, it just keeps looping ??
Function GetReqText() As String
On Error GoTo Err_Handler
Dim dbs As DAO.Database, rst As DAO.Recordset
Dim strReq As String, strReqD As String, strAmnt As String, strSql As
String
Dim strGams As String, strProj As String
strSql = "SELECT * FROM tbl_CIANumberRequest ORDER BY dtAdded DESC"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSql, dbOpenForwardOnly, dbReadOnly)
While Not rst.EOF '<========= JUst keeps looping ??
strReq = rst!Requestor
strReqD = rst!RequestorDept
strAmnt = rst!Amount
strGams = rst!gAMSNo
strProj = rst!ProjectNo
Wend
' GetReqText
Exit_Handler:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
If Not dbs Is Nothing Then
Set dbs = Nothing
End If
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End Function
Sub SendMail()
Dim strBody
strBody = GetReqText()
If Len(strBody) = 0 Then
strBody = "Nothing shipped today"
Else
Dim strmessage As String, stDocName
Dim stremail As String
Dim strsubject As String
strmessage = strBody
stDocName = "rpt_CIAEmail"
stremail = "[email protected]"
strsubject = "CIA Number Request"
DoCmd.SendObject , stDocName, , stremail, , , strsubject, strmessage,
True
End If
End Sub
--
Les
Allen Browne said:There are several things wrong, e.g. no SELECT in the SQL statement, and
the
loop makes no sense.
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Reply to group, rather than allenbrowne at mvps dot org.
Les said:Hi Allen, i have tried to adapt the code i got on another site but have
made
a mistake somewhere, could you help me please
Function GetReqText() As String
On Error GoTo Err_Handler
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strInfo As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tbl_CIANumberRequest. * order by
[dtAdded]
DESC", dbOpenForwardOnly, dbReadOnly)
While strInfo = ""
strInfo = strInfo & rst!Requestor & vbTab
strInfo = strInfo & rst!RequestorDept & vbTab
strInfo = strInfo & rst!Amount
Wend
GetReqText = strInfo
Exit_Handler:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
If Not dbs Is Nothing Then
Set dbs = Nothing
End If
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End Function
Sub SendMail()
Dim strBody
strBody = GetReqText()
If Len(strBody) = 0 Then
strBody = "Nothing shipped today"
Else
Dim strmessage As String, stDocName
Dim stremail As String
Dim strsubject As String
strmessage = strBody
stDocName = "rpt_CIAEmail"
stremail = "[email protected]"
strsubject = "CIA Number Request"
DoCmd.SendObject , stDocName, , stremail, , , strsubject,
strmessage,
True
End If
End Sub
--
Les
:
How do you define "last"?
Is it the highest AutoNumber value?
Or is there a date/time field?
Perhaps something like this:
Dim rs As DAO.Database
Dim strSql As String
strSql = "SELECT TOP 1 Table1.* ORDER BY [ID] DESC;"
Set rs = dbEngine(0)(0).OpenRecordset(strSql)
Hi, is it possible to select the last record entered, if so how ?
Thanks in advance