473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Find Last record in table by information in a textbox on a form

9
I am still learning access, vba and sql and am in need of some assistance. I have a table (tblCommercial) with the following these fields: ReferenceNumber(autonumber) AccountNumber, CompanyName, ServiceAddress, DumpsterSize, Frequency. I have a form(frmCommercialChange). The AccountNumber textbox on the form has an AfterUpdate control that populates the CompanyName, ServiceAddress, DumpsterSize and Frequency from the table. The form also has two additional textboxes for New Dumpster Size and New Frequency and when data is entered into these textboxes the information goes into the DumpsterSize and Frequency fields on the table. What I want it to do is to pull the last record with the same account number from the table. Here is the code I have
Expand|Select|Wrap|Line Numbers
  1. Private Sub AccountNumber_AfterUpdate()
  2. Dim SQL As String
  3. Dim db As Database
  4. Dim rs As DAO.Recordset
  5. SQL = "SELECT * from tblCommercial WHERE AccountNumber = " & Forms![frmCommercialChangeService]![AccountNumber] & ";"
  6. Set db = CurrentDb
  7. Set rs = db.OpenRecordset(SQL)
  8. If Not IsNull(Me.AccountNumber) Then
  9.     With Me.RecordsetClone
  10.     .FindLast "[AccountNumber]=" & Me.AccountNumber
  11.     End With
  12. Me.txtCompany.Value = rs!NameofCompany
  13. Me.txtServiceAddress.Value = rs!ServiceAddress
  14. Me.txtDumpsterSize.Value = rs!DumpsterSize
  15. Me.txtFrequency.Value = rs!FrequencyofService
  16. End If
  17. End Sub
I hope this make sense and any assistance would be great. Thank you.
Jan 15 '14 #1

✓ answered by ADezii

You could retrieve these Values using the DLast() Statement, as in:
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me![AccountNumber]) Then Exit Sub
  2.  
  3. With Me
  4.   .txtCompany = Nz(DLast("[NameofCompany]", "tblCommercial", "[AccountNumber] = " & _
  5.                 ![AccountNumber]), "N/A")
  6.   .txtServiceAddress = Nz(DLast("[ServiceAddress]", "tblCommercial", "[AccountNumber] = " & _
  7.                        ![AccountNumber]), "N/A")
  8.   .txtDumpsterSize = Nz(DLast("[DumpsterSize]", "tblCommercial", "[AccountNumber] = " & _
  9.                      ![AccountNumber]), "N/A")
  10.   .txtFrequency = Nz(DLast("[FrequencyofService]", "tblCommercial", "[AccountNumber] = " & _
  11.                   ![AccountNumber]), "N/A")
  12. End With

3 2927
zmbd
5,501 Expert Mod 4TB
You may want to rethink this... for the most part you shouldn't need to use vba to find related records; however, that's sometimes what it takes.

You should be able to filter or use a subform see if any of the following fit the bill (don't worry if these seem to be a tad heavy reading, if you don't follow something let us know (^.^)).
-filtering-The last entry may be the closest to what you are currently attempting to do.
Jan 15 '14 #2
ADezii
8,834 Expert 8TB
You could retrieve these Values using the DLast() Statement, as in:
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me![AccountNumber]) Then Exit Sub
  2.  
  3. With Me
  4.   .txtCompany = Nz(DLast("[NameofCompany]", "tblCommercial", "[AccountNumber] = " & _
  5.                 ![AccountNumber]), "N/A")
  6.   .txtServiceAddress = Nz(DLast("[ServiceAddress]", "tblCommercial", "[AccountNumber] = " & _
  7.                        ![AccountNumber]), "N/A")
  8.   .txtDumpsterSize = Nz(DLast("[DumpsterSize]", "tblCommercial", "[AccountNumber] = " & _
  9.                      ![AccountNumber]), "N/A")
  10.   .txtFrequency = Nz(DLast("[FrequencyofService]", "tblCommercial", "[AccountNumber] = " & _
  11.                   ![AccountNumber]), "N/A")
  12. End With
Jan 15 '14 #3
NeoPa
32,556 Expert Mod 16PB
I had a reply all set out then I realised you were probably not trying to do what the words in your question led me to believe. Your code references three recordsets and two controls with account numbers. What are you trying to do here?
Jan 15 '14 #4

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

Similar topics

1
by: sixsoccer | last post by:
I have built a database with a <Mainform> and a <Subform>. My problem is twofold. 1. My subform is set as a continuos form with AllowAddiotions set to NO (ie. a list of Issues to the client on...
3
by: BLUE WATER | last post by:
Hi, I have managed to simplify my problem but can't seem to get this to work. I want to open up a form from a click event of another form, but not only open the form when the button is pressed...
4
by: Rico | last post by:
Hi All, Just wondering, in vb code, how to if the last record on a cascading form is the current record? Thanks!
1
by: mcasaurabhsumit | last post by:
Hi friend, I am unable to get last record in sql server 2000. How to find last record in sql server 2000?
1
by: Simon | last post by:
Dear reader, How can I find a record in a sub form, the sub form type is DataSheet and the record collection in the subform are to many to show them all in the subform window.
3
by: bibek24 | last post by:
I have a continious subform which is only visible when the main form gets open. The subform has a text box which is locked for the user.In order to enter something into it, a user has to click a...
1
by: Blaikie | last post by:
Hello, I have a basic database to record all customer orders for the Skip Hire company I work for. At the moment I use the 'Find and Replace' function to search through the database so when I need...
19
by: aswgawlag | last post by:
Hello everyone. I am new to Microsoft Access and have been creating a database to store records, add new records, update records, delete records, search records, create reports from records, etc....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.