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 -
Me.[ID] = Nz(DMax("[ID]", "[MytableName]"), 0) + 1
-
Please help!!!
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 : - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Static lngLastID As Long
-
Dim lngID As Long
-
-
If Not Me.NewRecord Then Exit Sub
-
lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
-
If lngID > lngLastID Then lngLastID = lngID
-
lngLastID = lngLastID + 1
-
Me.ID = lngLastID
-
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 : -
Private Sub Form_BeforeInsert(Cancel As Integer)
-
Static lngLastID As Long
-
Dim lngID As Long
-
-
lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
-
If lngID > lngLastID Then lngLastID = lngID
-
lngLastID = lngLastID + 1
-
Me.ID = lngLastID
-
End Sub
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 .....
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 : - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Static lngLastID As Long
-
Dim lngID As Long
-
-
If Not Me.NewRecord Then Exit Sub
-
lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
-
If lngID > lngLastID Then lngLastID = lngID
-
lngLastID = lngLastID + 1
-
Me.ID = lngLastID
-
End Sub
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. -
'********************************************
-
'Only USE GetLineNumberForm and Serialize
-
'********************************************
-
-
-
'Stephen Lebans with help from Chris Bergmans
-
' Updated by Allen
-
'Production version of GetLineNumberForm
-
'Works in Form or SubForm mode
-
'Set controlsource of unbound Text box to
-
'= GetLineNumberForm([Form])
-
'Type exactly as above
-
-
Function GetLineNumberForm(f As Form)
-
Dim rs As Recordset
-
Dim frmMain As Form
-
Dim frmCur As Form
-
Dim strName As String
-
Dim IsItSubForm As Boolean
-
Dim strFName As String
-
-
'is the form referenced in the parameter currently
-
'loaded as a SubForm?
-
'Check it parent property to find out.
-
-
On Error Resume Next
-
strFName = f.Parent.NAME
-
IsItSubForm = (Err = 0)
-
-
'Point to our error handler
-
On Error GoTo Err_GetLineNumber
-
-
If IsItSubForm Then
-
'its a SubForm
-
Set frmMain = f.Parent.Form
-
strName = f.NAME
-
Set frmCur = frmMain(strName).Form
-
Set rs = frmCur.RecordsetClone
-
-
Else
-
'It's not a SubForm
-
Set rs = f.RecordsetClone
-
Set frmCur = f
-
End If
-
-
' Find the current record.
-
rs.Bookmark = frmCur.Bookmark
-
GetLineNumberForm = rs.AbsolutePosition + 1
-
-
Bye_GetLineNumber:
-
Set rs = Nothing
-
Set frmMain = Nothing
-
Set frmCur = Nothing
-
Exit Function
-
-
Err_GetLineNumber:
-
Resume Bye_GetLineNumber
-
-
End Function
-
How can I save this numbers on a my table?
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.
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!!!!
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.
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 -
Private Sub Form_BeforeInsert(Cancel As Integer)
-
Static lngLastID As Long
-
Dim lngID As Long
-
-
If Not Me.NewRecord Then Exit Sub
-
lngID = Nz(DMax("[ID]", "[MytableName]"), 0)
-
If lngID > lngLastID Then lngLastID = lngID
-
lngLastID = lngLastID + 1
-
Me.ID = lngLastID
-
End Sub
-
Yet if you paste the record, it gives very strange results, just have a look at it!!
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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:...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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: 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,...
|
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...
|
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...
| |