473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reference a previous record

1 New Member
In crystal reports when I write a formula I can state a condition of say if the current field is blank, then print the content of the same field but from the previous record. The function is Previous () and it works beautifully. I need this same function in Access.

There has to be a way that I can do this in Microsoft Access. I am not a VBA developer but if someone points me in the right direction as to how to state this in an Access expression I think I can get it to work for me. I must say I am somewhat perturbed that it is not a straight forward out-of-the-box function in MS Access.
Jan 4 '10 #1
4 21298
topher23
234 Recognized Expert New Member
A DLookup could be useful here. Use Help in the VB window and look it up. You'd want to do something like

Expand|Select|Wrap|Line Numbers
  1. DLookup("myfield","mytable","RecordID = " & Me.RecordID -1)
This assumes a field called RecordID as your Primary Key field and that you don't delete records. It should get you started.
Jan 4 '10 #2
ADezii
8,834 Recognized Expert Expert
I took the liberty of re-creating a Previous() Function for you that will return the previous Value in a Field should it be NULL. Simply pass to the Function the Name of your Form and also the Field Name you wish to check. Execute the code in the Current() Event of the Form. Any questions, feel free to ask. For this example I used the frmEmployees Form containing the [Title] Field of the Northwind Sample Database. The big advantage of this approach is that the code is portable.
  1. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function Previous(strFormName As String, strFieldName As String)
    2. On Error GoTo Err_Previous
    3. Dim rstClone As DAO.Recordset
    4. Dim strBookmark As String
    5. Dim frm As Form
    6.  
    7. Set frm = Forms(strFormName)
    8.  
    9. Set rstClone = frm.RecordsetClone           'Create a Cloned Recordset
    10.  
    11. strBookmark = frm.Bookmark                  'Store Original Bookmark
    12.  
    13. rstClone.Bookmark = frm.Bookmark            'Sync Bookmarks
    14.  
    15. rstClone.MovePrevious                       'Move to the Previous Record in the Clone
    16.  
    17. Previous = rstClone.Fields(strFieldName)    'Retrieve Value from Field in Clone
    18.  
    19. frm.Bookmark = strBookmark                  'Resync Bookmarks
    20.  
    21. Exit_Previous:
    22.   Exit Function
    23.  
    24. Err_Previous:
    25.   If Err.Number <> 3021 Then    'Not at the 1st or New Record
    26.     MsgBox Err.Description, vbExclamation, "Error in Previous()"
    27.   End If
    28.     Resume Exit_Previous
    29. End Function
  2. Sample Function Usage:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Current()
    2.   If IsNull(Me![Title]) Then
    3.     Me![Title] = Previous("frmEmployees", "Title")
    4.   End If
    5. End Sub
Jan 5 '10 #3
NeoPa
32,556 Recognized Expert Moderator MVP
@falconerlf
Quite the contrary in fact. I'm guessing from this that your expertise is not in database work. SQL and the SQL engine are the heart of anything related to databases nowadays, and SQL doesn't support ordinals. It recognises no order as being relevant to its processes. This makes absolute sense in an RDBMS, though is not so intuitive to those that come from a more conventional coding background.

Crystal Reports (as well as Access of course) has the possibility of overlaying the basic database concepts with other code (processing through recordsets etc). In this way it is possible to provide the functionality that you're after - ADezii has provided just that for you - but this is not something natural to any RDBMS.
Jan 5 '10 #4
nico5038
3,080 Recognized Expert Specialist
I concur with NeoPa that this is no "logical" functionality in a RDBMS.
Functionally an empty field indicates "not known" and that's different from "value from previous record". Problem with "value from previous record" is the order the rows are processed and thus a different order can give an empty field different values.... :-(

Looks to me that the normalization standards don't apply to the data in your table(s) and that should be taken care of instead of using a "Previous" function in a report.
In the form you can take care that the previous value is filled as a default and ADezii gave you code to do that. (An alternative sample .mdb can be found at http://examples.oreilly.com/accesscook/CDROM/ the fifth sample of the chap09 download contains a "Carry Data Forward from Record to Record" form)

So when producing a report no tricks are needed.

Finally the Access reporting does have the "reverse" operation available, being the "Suppress duplicate values" option.

Nic;o)
Jan 5 '10 #5

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

Similar topics

5
by: Derek Cooper | last post by:
I hope you can help me. I posted this in the microsoft sql server newsgroup a few days ago and got no response so I thought I'd try here. If I can provide any clarification I'll be glad to do so....
5
by: Steve Strik | last post by:
My Problem: I have created a database here at work that is exhibiting some very strange behaviour. Essentially the database is structured in a manner where one table is a master record table...
0
by: KelHemp | last post by:
Greetings, I've been using this site for lots of access help in the past, and it's very helpful! I have a new complexity for you all. Reworking a form to record 70-80 years of oil production on...
8
by: Jeff | last post by:
A client wants a press of the Enter key in a field on a continuous form to grab the value of that field from the previous record. But if they have typed a value and then hit Enter it shouldn't. ...
1
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same...
7
by: thread | last post by:
Hi all i need to build progression calculator for a record and for this i need to have the possiblity to get the information for the previous record. is it posible to do it or i will need to use...
1
nev
by: nev | last post by:
Good day to all! I was trying to search the internet about this problem but I still can't seem to stumble upon an answer. I am using a BindingSource on my program and everything is ok except that...
16
by: zoeb | last post by:
Hi, I am a complete novice to Access VBA and looking for some help to select a record. I am looking to perform an operation on the previous record - i.e. adding a new blank field, and then...
5
by: usr123 | last post by:
I am trying to get the previous record from a table. Scenario is: I have a previous button on the form. User browse through the records one by one to go back/forward. For first time to pull data...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.