473,396 Members | 2,029 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,396 software developers and data experts.

Setrecordid() - Dynamic autonumber.

I am attempting a set record ID to make a number field dynamic. 1st - doe the field have to be an "autonumber" or can it be a number field. Also, when I set the query in the stringobject, it gives me an Expected Identifier ) error... I am using a query named "qry_mergefile Query" and the string field is: "0 Item_Number". Any help is appreciated if this is workable, or if other coding is available.
Expand|Select|Wrap|Line Numbers
  1. Function setRecordID(strObject As String, strField As String)
  2.     Dim myWS As DAO.Workspace
  3.     Dim mydb As DAO.Database
  4.     Dim myRS As DAO.Recordset
  5.     On Error GoTo err_setRecordID
  6.  
  7.     Set myWS = DBEngine(0)
  8.     Set mydb = CurrentDb
  9.     Set myRS = mydb.OpenRecordset(strObject, dbOpenDynaset)
  10.  
  11.     myWS.BeginTrans
  12.         ' Do bulk changes to Recordset.
  13.         With myRS
  14.             .MoveFirst
  15.             Do While Not .EOF
  16.                 .Edit
  17.                 .Fields(strField) = .AbsolutePosition + 1
  18.                 .Update
  19.                 .MoveNext
  20.             Loop
  21.         End With
  22.         ' If all updates done successfully, commit the transaction.
  23.     myWS.CommitTrans
  24.     Exit Function
  25.  
  26. err_setRecordID:
  27.     Select Case Err.Number
  28.         Case 3061
  29.             'Error in the function arguments
  30.             MsgBox "Error in your Object name or your Field name"
  31.         Case Else
  32.             'If any error occur while making the updates
  33.             'all of the changes will be rolled back (not saved).
  34.             MsgBox "Start the function again, updating problems"
  35.             myWS.Rollback
  36.     End Select
  37.  
  38.     Exit Function
  39.  
  40. End Function
Oct 24 '07 #1
2 1629
ADezii
8,834 Expert 8TB
I am attempting a set record ID to make a number field dynamic. 1st - doe the field have to be an "autonumber" or can it be a number field. Also, when I set the query in the stringobject, it gives me an Expected Identifier ) error... I am using a query named "qry_mergefile Query" and the string field is: "0 Item_Number". Any help is appreciated if this is workable, or if other coding is available.

Function setRecordID(strObject As String, strField As String)
Dim myWS As DAO.Workspace
Dim mydb As DAO.Database
Dim myRS As DAO.Recordset
On Error GoTo err_setRecordID

Set myWS = DBEngine(0)
Set mydb = CurrentDb
Set myRS = mydb.OpenRecordset(strObject, dbOpenDynaset)

myWS.BeginTrans
' Do bulk changes to Recordset.
With myRS
.MoveFirst
Do While Not .EOF
.Edit
.Fields(strField) = .AbsolutePosition + 1
.Update
.MoveNext
Loop
End With
' If all updates done successfully, commit the transaction.
myWS.CommitTrans
Exit Function

err_setRecordID:
Select Case Err.Number
Case 3061
'Error in the function arguments
MsgBox "Error in your Object name or your Field name"
Case Else
'If any error occur while making the updates
'all of the changes will be rolled back (not saved).
MsgBox "Start the function again, updating problems"
myWS.Rollback
End Select

Exit Function

End Function
I've taken the liberty and made a few modifications to your code, it works fine. Make sure you are passing a valid Table/Query and Field Name to this Function. Also, make sure the Field that you are passing is not an AutoNumber Field (no can do).
Expand|Select|Wrap|Line Numbers
  1. Function setRecordID(strObject As String, strField As String)
  2. Dim myWS As DAO.Workspace
  3. Dim mydb As DAO.Database
  4. Dim myRS As DAO.Recordset
  5. On Error GoTo err_setRecordID
  6.  
  7. Set myWS = DBEngine(0)
  8. Set mydb = CurrentDb
  9. Set myRS = mydb.OpenRecordset(strObject, dbOpenDynaset)
  10.  
  11. myWS.BeginTrans
  12. ' Do bulk changes to Recordset.
  13.  
  14. With myRS
  15.   .MoveFirst
  16.   Do While Not .EOF
  17.     .Edit
  18.       .Fields(strField) = .AbsolutePosition + 1
  19.     .Update
  20.   .MoveNext
  21.   Loop
  22. End With
  23. ' If all updates done successfully, commit the transaction.
  24. myWS.CommitTrans
  25.  
  26. myRS.Close
  27. myWS.Close
  28. Set myRS = Nothing
  29. Set myWS = Nothing
  30.  
  31. Exit_setRecordID:
  32.   Exit Function     'should be a 'single' exit point
  33.  
  34. err_setRecordID:
  35.   Select Case Err.Number
  36.     Case 3061
  37.       'Error in the function arguments
  38.       MsgBox "Error in your Object name or your Field name"
  39.     Case Else
  40.       'If any error occur while making the updates
  41.       'all of the changes will be rolled back (not saved).
  42.       MsgBox "Start the function again, updating problems"
  43.         myWS.Rollback
  44.   End Select
  45.     myRS.Close
  46.     myWS.Close
  47.     Set myRS = Nothing
  48.     Set myWS = Nothing
  49.       Resume Exit_setRecordID
  50. End Function
Oct 24 '07 #2
Thank you, I will work on it and let you know if it works!! Snichols

I've taken the liberty and made a few modifications to your code, it works fine. Make sure you are passing a valid Table/Query and Field Name to this Function. Also, make sure the Field that you are passing is not an AutoNumber Field (no can do).
Expand|Select|Wrap|Line Numbers
  1. Function setRecordID(strObject As String, strField As String)
  2. Dim myWS As DAO.Workspace
  3. Dim mydb As DAO.Database
  4. Dim myRS As DAO.Recordset
  5. On Error GoTo err_setRecordID
  6.  
  7. Set myWS = DBEngine(0)
  8. Set mydb = CurrentDb
  9. Set myRS = mydb.OpenRecordset(strObject, dbOpenDynaset)
  10.  
  11. myWS.BeginTrans
  12. ' Do bulk changes to Recordset.
  13.  
  14. With myRS
  15.   .MoveFirst
  16.   Do While Not .EOF
  17.     .Edit
  18.       .Fields(strField) = .AbsolutePosition + 1
  19.     .Update
  20.   .MoveNext
  21.   Loop
  22. End With
  23. ' If all updates done successfully, commit the transaction.
  24. myWS.CommitTrans
  25.  
  26. myRS.Close
  27. myWS.Close
  28. Set myRS = Nothing
  29. Set myWS = Nothing
  30.  
  31. Exit_setRecordID:
  32.   Exit Function     'should be a 'single' exit point
  33.  
  34. err_setRecordID:
  35.   Select Case Err.Number
  36.     Case 3061
  37.       'Error in the function arguments
  38.       MsgBox "Error in your Object name or your Field name"
  39.     Case Else
  40.       'If any error occur while making the updates
  41.       'all of the changes will be rolled back (not saved).
  42.       MsgBox "Start the function again, updating problems"
  43.         myWS.Rollback
  44.   End Select
  45.     myRS.Close
  46.     myWS.Close
  47.     Set myRS = Nothing
  48.     Set myWS = Nothing
  49.       Resume Exit_setRecordID
  50. End Function
Oct 24 '07 #3

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

Similar topics

33
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and...
35
by: Traci | last post by:
If I have a table with an autonumber primary key and 100 records and I delete the last 50 records, the next record added would have a primary key of 101. Is there any way to have the primary key...
4
by: yf | last post by:
A KB article "http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells that the maximum number of records that a table may hold if the PRIMARY key data type is set to AUTONUMBER is...
1
by: jimfortune | last post by:
Sometimes I use Autonumber fields for ID fields. Furthermore, sometimes I use those same fields in orderdetail type tables. So it's important in that case that once an autonumber key value is...
8
by: Sandy Pittendrigh | last post by:
I have a how-to-do-it manual like site, related to fishing. I want to add a new interactive question/comment feature to each instructional page on the site. I want (registered) users to be able...
11
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any...
4
by: irkahs | last post by:
Hello all, I have a report which is connected to a query that collects data based on some filters. Now, my question is, how do I make an autonumber starting from 1 get generated...
6
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the...
1
by: lawitt | last post by:
I have a MakeTable query whose resulting table i'm using in a crosstab query. The fields in the crosstab are constantly changing (weekly), so my ultimate goal has been to somehow create a dynamic...
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: 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
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
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
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
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...
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,...

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.