473,442 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DMax Function Not Responding on Pasted Records

132 100+
Hi all!
I set ID field to Dmax function to generate the next record number at the BeforeInsert of the MainForm(on datasheetView). It Works fine if the records is entered one by one, but when you paste the Records at once, you endup with similar ID at the ID field (not incremented as expected).
My users are always looking for shortcuts to paste records from somewhere!
Here is the Code
Expand|Select|Wrap|Line Numbers
  1. Me.[ID] = Nz(DMax("[ID]", "[MytableName]"), 0) + 1
  2.  
Please help!!!
Aug 27 '11 #1

✓ answered by NeoPa

Very strange. I tested it out on a system I knocked up and not only did I notice that the BeforeInsert event for the form was triggered again after the vale of Me.ID was set in the code (thus causing the double increment), but also that the value of ID was never actually changed either on the form or in the record.

With a minor change however, I was able to produce a routine that did what was required :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     Static lngLastID As Long
  3.     Dim lngID As Long
  4.  
  5.     If Not Me.NewRecord Then Exit Sub
  6.     lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
  7.     If lngID > lngLastID Then lngLastID = lngID
  8.     lngLastID = lngLastID + 1
  9.     Me.ID = lngLastID
  10. End Sub

11 2869
NeoPa
32,556 Expert Mod 16PB
I would guess that the addition of all the records together is treated as a single transaction. That is to say that none of the additions of those records will be visible until all of them have been added and the transaction committed.

You will need to handle this in your code. Something like the following may work :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2.     Static lngLastID As Long
  3.     Dim lngID As Long
  4.  
  5.     lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
  6.     If lngID > lngLastID Then lngLastID = lngID
  7.     lngLastID = lngLastID + 1
  8.     Me.ID = lngLastID
  9. End Sub
Aug 27 '11 #2
Mr Key
132 100+
I have tried to paste the code but it gives ODD Value Counting
1 3 5 7 9 ....
and not normal Counting as 1 2 3 4 5 6 .....
Aug 28 '11 #3
NeoPa
32,556 Expert Mod 16PB
Very strange. I tested it out on a system I knocked up and not only did I notice that the BeforeInsert event for the form was triggered again after the vale of Me.ID was set in the code (thus causing the double increment), but also that the value of ID was never actually changed either on the form or in the record.

With a minor change however, I was able to produce a routine that did what was required :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     Static lngLastID As Long
  3.     Dim lngID As Long
  4.  
  5.     If Not Me.NewRecord Then Exit Sub
  6.     lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
  7.     If lngID > lngLastID Then lngLastID = lngID
  8.     lngLastID = lngLastID + 1
  9.     Me.ID = lngLastID
  10. End Sub
Aug 28 '11 #4
Mr Key
132 100+
I have tried but it couldnt work either, instead I have this code below to determine the number of records in a table that can be displayed in a form. In this code You just Set controlsource of unbound Text box to'= GetLineNumberForm([Form]).
But I couldnt save this number as my ID field in a table.


Expand|Select|Wrap|Line Numbers
  1. '********************************************
  2. 'Only USE GetLineNumberForm  and   Serialize
  3. '********************************************
  4.  
  5.  
  6. 'Stephen Lebans with help from Chris Bergmans
  7. ' Updated by Allen
  8. 'Production version of GetLineNumberForm
  9. 'Works in Form or SubForm mode
  10. 'Set controlsource of unbound Text box to
  11. '= GetLineNumberForm([Form])
  12. 'Type exactly as above
  13.  
  14. Function GetLineNumberForm(f As Form)
  15.     Dim rs As Recordset
  16.     Dim frmMain As Form
  17.     Dim frmCur As Form
  18.     Dim strName As String
  19.     Dim IsItSubForm As Boolean
  20.     Dim strFName As String
  21.  
  22.     'is the form referenced in the parameter currently
  23.     'loaded as a SubForm?
  24.     'Check it parent property to find out.
  25.  
  26.     On Error Resume Next
  27.     strFName = f.Parent.NAME
  28.     IsItSubForm = (Err = 0)
  29.  
  30.     'Point to our error handler
  31.     On Error GoTo Err_GetLineNumber
  32.  
  33.     If IsItSubForm Then
  34.         'its a SubForm
  35.         Set frmMain = f.Parent.Form
  36.         strName = f.NAME
  37.         Set frmCur = frmMain(strName).Form
  38.         Set rs = frmCur.RecordsetClone
  39.  
  40.     Else
  41.         'It's not a SubForm
  42.         Set rs = f.RecordsetClone
  43.         Set frmCur = f
  44.     End If
  45.  
  46.         ' Find the current record.
  47.         rs.Bookmark = frmCur.Bookmark
  48.         GetLineNumberForm = rs.AbsolutePosition + 1
  49.  
  50. Bye_GetLineNumber:
  51.        Set rs = Nothing
  52.        Set frmMain = Nothing
  53.        Set frmCur = Nothing
  54.        Exit Function
  55.  
  56. Err_GetLineNumber:
  57.        Resume Bye_GetLineNumber
  58.  
  59. End Function
  60.  
How can I save this numbers on a my table?
Sep 13 '11 #5
NeoPa
32,556 Expert Mod 16PB
I never suggested you should. It looks like a thoroughly overcomplicated solution.

If you're interested in my help then you might like to explain what went wrong when you tried the code I suggested in post #4. It worked fine when I tested it so I'm confused as to what you could be doing that would cause it to fail. Certainly nothing you've told us about so far would explain it.
Sep 13 '11 #6
Mr Key
132 100+
Sorry, I dint explain how!!
First: Am using access 2010
Second: When you paste the record on the forms acDataSheetView, it count the same sequence, ODD values, 1, 3 , 5....
Thirdly: When you paste again new record, it leaves a blank line and then starts count again in the same sequece, 7,9,11...
May be something have to be done before I paste the code!!!!
Sep 14 '11 #7
NeoPa
32,556 Expert Mod 16PB
That's not what happened when I tested it :-S

Maybe I need to see your database. Unfortunately, I'm just going away for over a week now. I'll look at it for you when I get back though if you post a copy of your database. Make sure you follow the instructions in Attach Database (or other work) though. I still use Access 2003 on my main machine. I'll try to remember to dig this thread up when I get back on Saturday of next week.
Sep 14 '11 #8
Mr Key
132 100+
Ok! Thanks for your suggestion
Here is my copy of mydatabase,
I have set the table to be MytableName and the field to be incremented to be simple ID. At the afterInsert event of a form1 set to EventProcedure which have the following code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2.     Static lngLastID As Long
  3.     Dim lngID As Long
  4.  
  5.     If Not Me.NewRecord Then Exit Sub
  6.     lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
  7.     If lngID > lngLastID Then lngLastID = lngID
  8.     lngLastID = lngLastID + 1
  9.     Me.ID = lngLastID
  10. End Sub
  11.  
Yet if you paste the record, it gives very strange results, just have a look at it!!
Attached Files
File Type: zip db.zip (57.0 KB, 101 views)
Sep 22 '11 #9
NeoPa
32,556 Expert Mod 16PB
It doesn't appear that you have used the code I suggested. My code in post #4 handles the form's BeforeUpdate event, while yours is still using the BeforeInsert event (which, if you remember, we discovered does exactly what you're complaining of). Try my latest suggestion and we can move onwards from there.
Sep 24 '11 #10
Mr Key
132 100+
Thanks Sir for your help, finally it works. I dint recognize the change from BeforeInsert to BeforeUpdate
Thanks again!
I can now move to another problem of AfterInsert Event Not Triggered as Expected on Pasted Records
Thanks all for your contributed in this post!!!
Sep 26 '11 #11
NeoPa
32,556 Expert Mod 16PB
You're welcome Mr Key.

I should point out though, that the last post in the other thread is four weeks old and requires more from you before anyone is likely to post anything further there.
Sep 26 '11 #12

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

Similar topics

1
by: Sid | last post by:
Hi, I have a VB function that calculates a spatial point from a bearing and distance. I need to be able to update point co-ordinates (Lat and Longs) in the Db with the function. I can call the...
9
by: MLH | last post by:
I have a database (datatrek.mdb) with a table named DATA. The table has a date/time field with default value = Now(). It has 100 records in it entered over a 50-minute period. I would like the...
2
by: Mihir | last post by:
I have a quick question. I am trying the following This is my table..tStudents Name Grades Ram 20 Ram 30 Ram 50
3
by: Cindy | last post by:
I'm trying to use the NEWID function in dynamic SQL and get an error message Incorrect syntax near the keyword 'ORDER'. Looks like I can't do an insert with an Order by clause. Here's the code:...
2
by: ontherun | last post by:
Hi, I have a form called Customer based on the table tblCustomer and another form called Job based on the table Job when the user enter the details for the Customer i have a button at the...
2
by: clarencelai | last post by:
Hi.. I have a question regarding the DoCmd.OutputTo function. I am automating some email scheduling with file attachments generated from OutPutTo function. However, some of the daily...
1
by: maccount | last post by:
I'm having a simple problem (or at least I believe it to be), one that has stumped me. I have a simple text box in my Access Report, page header. Dmin("ppID", "qry001") This report is based on...
3
by: Patrick Finnegan | last post by:
Running 8.2.8 on aix. Any reason why the dynamic cache snapshot function would return no records? db2 "SELECT * FROM TABLE (SQLCACHE_SNAPSHOT()) as SQLCACHE_SNAPSHOT" 0 record(s) selected....
1
by: nrtyme | last post by:
Hello, I need to add several new records to Table2 from Table1. Table2 contains a field called that needs to be the previous maximum value of incremented by 1. Below is my code but i keep...
21
by: DanicaDear | last post by:
I have a report named "rptHOTSTICKS_EXPIRING" based on a query named "HOSTICKS_SHIPPING_REPORT Query". The query contains these fields: ORDER_NUM (text) CUST_NUM (text) Name, address, contact...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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: 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...

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.