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
2 1629
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). - 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
-
-
myRS.Close
-
myWS.Close
-
Set myRS = Nothing
-
Set myWS = Nothing
-
-
Exit_setRecordID:
-
Exit Function 'should be a 'single' exit point
-
-
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
-
myRS.Close
-
myWS.Close
-
Set myRS = Nothing
-
Set myWS = Nothing
-
Resume Exit_setRecordID
-
End Function
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). - 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
-
-
myRS.Close
-
myWS.Close
-
Set myRS = Nothing
-
Set myWS = Nothing
-
-
Exit_setRecordID:
-
Exit Function 'should be a 'single' exit point
-
-
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
-
myRS.Close
-
myWS.Close
-
Set myRS = Nothing
-
Set myWS = Nothing
-
Resume Exit_setRecordID
-
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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: 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...
|
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,...
| |