473,799 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ 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 21459
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,579 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
14945
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. I'm trying to calculate a column based on the value of the previous record. I'm not very experienced with SQL-Server. I'm using the following table: CREATE TABLE tblPayment (
5
2227
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 (tbl_MainRegister) providing a unique identifier for documents and a means for identifying the docuement type. There are then 4 additional tables (tbl_Meetings, tbl_Documents, tbl_Project, tbl_Correspondence) which store the document details.
0
2699
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 multiple leases, which have multiple wells within them. Most wells can fit on one printed page (one record), but in the case of overspill, we want to do the following: -Move to a new record and auto fill the LeaseName, LeaseNumber, Operator,...
8
3197
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. I have tried a few ways but the main problem is that after typing a value and hitting Enter it still grabs the value from the previous record, trashing the value they just typed. Any ideas from you lateral thinkers.
1
4344
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 logic for a date field. i.e. theValue.defaultvalue = "#" & theValue.value & "#" I can't get this to work for some reason and I can't figure out why. Can anyone out there help? Thanks Repeat value of previous record field into current...
7
3480
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 recordset for this?
1
3186
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 the users can edit any record they want before saving them all in one click. Well, this is fine when doing common saves. But, in my program, I have placed validations for each particular fields. When I add or edit a record then save, the...
16
6536
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 calculating the value in the new field by increasing the value in the previous record by a percentage. However, I am unsure of how to select the previous record. I though that by having a query in the background which finds the maximum record ID would work,...
5
3947
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 from sql,i check that if form is empty,if it is then i pull the last record form the table based on Max(id) which is autonumber. Now, because people would be deleting record as well,there is autonumber not in any consecutive order. Once i have...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
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 we have to send another system
2
3759
muto222
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.