Is there a way to change my primary key in Access using ASP?
My primary key looks like this:
1101001CXR001MS01-01 thru 1101001CXR001MS01-24
I want to change MS01 to MS04.
There were two bugs in the code as I published it. I just ran a compile test and fixed them.
Did you put the code in a module, and not a class module? - Public Function MakeNewLoc(ByVal loc As Variant) As String
-
''
-
'' Makes a new location based on the old one
-
''
-
''--local variables
-
Dim newLoc As String
-
-
''--process the input loc
-
If (IsEmpty(loc)) Then
-
'' either raise an exception here or return a reasonable value
-
newLoc = "Empty"
-
ElseIf (IsNull(loc)) Then
-
'' either raise an exception here or return a reasonable value
-
newLoc = "NULL"
-
ElseIf (IsObject(loc)) Then
-
Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, "Error: loc is an Object."
-
Else
-
''=======================================
-
''=======================================
-
'' Compute the newLoc value here in any way you want
-
''=======================================
-
newLoc = CStr(loc)
-
''=======================================
-
''=======================================
-
End If
-
-
''--error check(s)
-
''If ("" = result) Then
-
'' Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, "Error: EMPTY newLoc"
-
''End If
-
-
''--result assignment
-
MakeNewLoc = newLoc
-
End Function
And, here is the function I used to test the code. - Public Sub testMakeNewLoc()
-
Dim db As DAO.database
-
Dim sqlUPDATE As String
-
-
sqlUPDATE = "UPDATE [Persistent State]" & ASCII.NL & _
-
" SET [key] = MakeNewLoc([key]);"
-
-
Set db = Application.CurrentDb
-
-
db.Execute (sqlUPDATE)
-
End Sub
NOTE: ASCII is my ascii value data structure. The ASCII.NL simply inserts a line break into the SQL. You can write Chr$(10), if you prefer. You can also omit it from your test, if you want.
19 2304
Is there any reason why you can't just create an update statement and execute it? -
UPDATE panel
-
SET key=newValue
-
WHERE key=oldValue;
-
]
Getting a new error:
Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/paneldatatest/indiana/fdp/movepaneldefault.asp, line 65, column 18 -
'UPDATE panel
-
SET key = newlocvar
-
WHERE key = locvar;
-
@Jimqunkel,
My bad. I was thinking embedded SQL, like - EXEC SQL UPDATE panel SET key = :newlocvar WHERE key = :locvar
Which is definitely not ASP.
Try something like this: -
sqlUPDATE = "UPDATE panel SET key = " & newValue & " WHERE key = " & oldValue
-
MyConn.Execute sqlUPDATE
-
Getting a new error:
Microsoft JET Database Engine (0x80040E14)
Syntax error in UPDATE statement.
/paneldatatest/indiana/fdp/movepaneldefault.asp, line 65 -
sqlUPDATE = "UPDATE panel SET key = " & newlocvar & " WHERE key = " & locvar
-
MyConn.Execute sqlUPDATE
-
Line 65 is "MyConn.Execute sqlUPDATE"
Is key the name of your primary key? Also I think you want to use a replace function to alter your key. - sqlUPDATE ="UPDATE panel SET primary_key = REPLACE(primary_key, 'MS01', 'MS04')"
This would change MS01 in all primary keys to MS04.
Make sure to swap out primary_key for the name of your key in table panel.
If the key is a string, you'll need to add delimiters to the variables.
Something like this... - sqlUPDATE = "UPDATE panel SET key = '" & newlocvar & "' WHERE key = '" & locvar & "'"
Same error - locvar = Request.Form("loc")
-
-
newlocvar = Request.Form("loc")
-
-
locCount = 1
-
-
locSize = Request.Form("ps")
-
-
locSize = Cint(locSize)
-
-
Set MyConn = Server.CreateObject("ADODB.Connection") ' Establish the connection object
-
MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Paneldata\Databases\FDPIN.mdb;Jet OLEDB:Database Password=niemtel;"
-
-
Set rsUpdateComments = Server.CreateObject("ADODB.Recordset")
-
-
-
'Getting locbay info for locvar string
-
-
locBay = Request.Form("bay")
-
-
locBay = Ucase(locBay)
-
-
locvar = Request.Form ("loc") & Request.Form("rem") & locBay & "MS" & Request.Form("ms") & "-0" & locCount
-
-
strSql = "select * from panel where PlantLoc like '" & locvar & "%'" 'Set up a SQL command in a string variable
-
-
-
'Getting newlocbay info for newlocvar string
-
-
newlocBay = Request.Form("newbay")
-
-
newlocBay = Ucase(locBay)
-
-
newlocvar = Request.Form ("loc") & Request.Form("rem") & newlocBay & "MS" & Request.Form("newms") & "-0" & locCount
-
-
-
rsUpdateComments.CursorType = 2
-
-
rsUpdateComments.LockType = 3
-
-
rsUpdateComments.Open strSQL, MyConn
-
-
-
'Update records
-
-
do until locCount > locSize
-
-
'rsUpdateComments.Fields("PlantLoc")= newlocvar
-
-
'rsUpdateComments.Update
-
-
response.write newlocvar
-
-
'UPDATE panel
-
sqlUPDATE = "UPDATE panel SET key = '" & newlocvar & "' WHERE key = '" & locvar & "'"
-
MyConn.Execute sqlUPDATE
-
Ok, I'm confused.
I think you needed to change 'key' in my string to 'PlantLoc', like this: - sqlUPDATE = "UPDATE panel SET PlantLoc = '" & newlocvar & "' WHERE PlantLoc = '" & locvar & "'"
-
-
or
-
-
sqlUPDATE = "UPDATE panel SET PlantLoc="
-
sqlUpdate = sqlUPDATE & REPLACE(PlantLoc, '" & locvar & "', '" & newlocvar & "')"
-
sqlUpdate = sqlUpdate & " WHERE PlantLoc LIKE ' & locvar & "%'"
Although I must say that it looks like your loop should be doing the the same thing, without the subsequent UPDATE statement.
I'm not getting an error but the database is not being updated with the new key data "PlantLoc". -
<% @language="vbscript" %>
-
<% Response.buffer = true %>
-
-
<%
-
-
'************************************************************
-
'ADO is the keyword for google searches on help with DB stuff
-
'************************************************************
-
-
locvar = Request.Form("loc")
-
-
newlocvar = Request.Form("loc")
-
-
locCount = 1
-
-
locSize = Request.Form("ps")
-
-
locSize = Cint(locSize)
-
-
Set MyConn = Server.CreateObject("ADODB.Connection") ' Establish the connection object
-
MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Paneldata\Databases\FDPIN.mdb;Jet OLEDB:Database Password=niemtel;"
-
-
Set SQLupdate = Server.CreateObject("ADODB.Recordset")
-
-
-
'Getting locbay info for locvar string
-
-
locBay = Request.Form("bay")
-
-
locBay = Ucase(locBay)
-
-
locvar = Request.Form ("loc") & Request.Form("rem") & locBay & "MS" & Request.Form("ms") & "-0" & locCount
-
-
strSql = "select * from panel where PlantLoc like '" & locvar & "%'" 'Set up a SQL command in a string variable
-
-
-
'Getting newlocbay info for newlocvar string
-
-
newlocBay = Request.Form("newbay")
-
-
newlocBay = Ucase(locBay)
-
-
newlocvar = Request.Form ("loc") & Request.Form("rem") & newlocBay & "MS" & Request.Form("newms") & "-0" & locCount
-
-
-
sqlupdate.CursorType = 2
-
-
sqlupdate.LockType = 3
-
-
sqlupdate.Open strSQL, MyConn
-
-
-
'Update records
-
-
do until locCount > locSize
-
-
'response.write newlocvar
-
-
'UPDATE panel
-
-
sqlUPDATE = "UPDATE panel SET PlantLoc = '" & newlocvar & "' WHERE PlantLoc = '" & locvar & "'"
-
-
-
locCount = (locCount + 1)
-
-
LocSplit = Split(locvar, "-")
-
-
LocSplit(1) = CInt(LocSplit(1))
-
-
LocSplit(1) = (LocSplit(1) + 1)
-
-
LocSplit(1) = CStr(LocSplit(1))
-
-
if LocSplit(1) < 10 then
-
locvar = Join(LocSplit, "-0")
-
else
-
locvar = Join(LocSplit, "-")
-
end if
-
-
LocSplit = Split(newlocvar, "-")
-
-
LocSplit(1) = CInt(LocSplit(1))
-
-
LocSplit(1) = (LocSplit(1) + 1)
-
-
LocSplit(1) = CStr(LocSplit(1))
-
-
if LocSplit(1) < 10 then
-
newlocvar = Join(LocSplit, "-0")
-
else
-
newlocvar = Join(LocSplit, "-")
-
end if
-
-
loop
-
-
-
MyConn.close ' close the database connection
-
-
%>
-
Jim,
I don't see where you are executing the update in your code.
You build it in line 61.
But I just don't see the DB.Execute(sqlUPDATE)
Am I blind?
=============================
As an aside, we may be a bit disconnected. Are you opening the recordset as modifyable, with the goal of writing back to it as you visit each record?
If so, the UPDATE query is pointless, and we need to simplify the code considerably.
Also, we can be really slick and implement a function that computes the new key value based on its parameters and then just execute a single update that references that function to do the update in a single stroke.
======================================
Cheers!
Sorry - I was tring a few things and forgot to add it back in to the code. -
'UPDATE panel
-
-
sqlUPDATE = "UPDATE panel SET PlantLoc = '" & newlocvar & "' WHERE PlantLoc = '" & locvar & "'"
-
MyConn.Execute sqlUPDATE
-
-
I'm game to try a different way.
Another tack you might take is to create a "support" module with a function like this: - Public Function MakeNewLoc(ByVal loc As Variant) AS String
-
''
-
'' Makes a new location based on the old one
-
''
-
''--local variables
-
Dim newLoc As String
-
-
''--process the input loc
-
If (IsEmpty(loc)) Then
-
'' either raise an exception here or return a reasonable value
-
newLoc = "Empty"
-
ElseIf (IsNull(loc)) Then
-
'' either raise an exception here or return a reasonable value
-
first = "NULL"
-
ElseIf (IsObject(loc)) Then
-
Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, "Error: loc is an Object."
-
Else
-
''=======================================
-
''=======================================
-
'' Compute the newLoc value here in any way you want
-
''=======================================
-
newLoc = loc
-
''=======================================
-
''=======================================
-
End If
-
-
''--error check(s)
-
''If ("" = result) Then
-
'' Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, "Error: EMPTY newLoc"
-
''End If
-
-
''--result assignment
-
MakeNewLoc = result
-
End Function
And then, a single UPDATE should suffice: - UPDATE panel
-
SET PlantLoc = MakeNewLoc(PlantLoc);
Proviso: I do know that the trick of writing functions works for SELECT statements. I haven't actually tried it on an UPDATE.
Getting new error:
Microsoft VBScript compilation (0x800A03EA)
Syntax error
/paneldatatest/indiana/fdp/movepaneldefault.asp, line 61, column 7 -
Public Function MakeNewLoc(ByVal loc As Variant) AS String
-
This seems to have gotten very complicated...
It is my understanding that you have a primary key named PlantLoc.
PlantLoc looks like this: 1101001CXR001MS01-01 thru 1101001CXR001MS01-24
But you wanted it changed to: 1101001CXR001MS04-01 thru 1101001CXR001MS04-24
Instead of looping and executing multiple SQL statements why not just do one SQL statement that updates all 24 records? -
sqlUPDATE ="UPDATE panel SET PlantLoc = REPLACE(PlantLoc, 'MS01', 'MS04')"
-
There were two bugs in the code as I published it. I just ran a compile test and fixed them.
Did you put the code in a module, and not a class module? - Public Function MakeNewLoc(ByVal loc As Variant) As String
-
''
-
'' Makes a new location based on the old one
-
''
-
''--local variables
-
Dim newLoc As String
-
-
''--process the input loc
-
If (IsEmpty(loc)) Then
-
'' either raise an exception here or return a reasonable value
-
newLoc = "Empty"
-
ElseIf (IsNull(loc)) Then
-
'' either raise an exception here or return a reasonable value
-
newLoc = "NULL"
-
ElseIf (IsObject(loc)) Then
-
Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, "Error: loc is an Object."
-
Else
-
''=======================================
-
''=======================================
-
'' Compute the newLoc value here in any way you want
-
''=======================================
-
newLoc = CStr(loc)
-
''=======================================
-
''=======================================
-
End If
-
-
''--error check(s)
-
''If ("" = result) Then
-
'' Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, "Error: EMPTY newLoc"
-
''End If
-
-
''--result assignment
-
MakeNewLoc = newLoc
-
End Function
And, here is the function I used to test the code. - Public Sub testMakeNewLoc()
-
Dim db As DAO.database
-
Dim sqlUPDATE As String
-
-
sqlUPDATE = "UPDATE [Persistent State]" & ASCII.NL & _
-
" SET [key] = MakeNewLoc([key]);"
-
-
Set db = Application.CurrentDb
-
-
db.Execute (sqlUPDATE)
-
End Sub
NOTE: ASCII is my ascii value data structure. The ASCII.NL simply inserts a line break into the SQL. You can write Chr$(10), if you prefer. You can also omit it from your test, if you want.
@JKing,
You have the right of it.
My form is a little more general, because I'm used to my simplisitic ways.
What's important is that the replacement value computation is correctly done.
Thanks! I learned something.
JKing - The MS value is not a constant I used it as an example other info may need to change like 1099009FDP009MS01 to 1099009FDP008MS03 as an example. So a simple way may not work. I'm all for simple if possable but I need the flexability to change more than one peramiter. Sorry if I was to vague.
Jim,
Sounds like you're looking at the module with transformation method.
Give it a try and let's see how it works out.
Cheers!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nuno |
last post by:
How can i change the access modifier of a control from protected to public if
the designer code is not visible to the developer? I tryed declaring it on
the page but it throws a compile error...
|
by: sonal |
last post by:
Hi all,
I hv started with python just recently... and have been assigned to
make an utility which would be used for data validations...
In short we take up various comma separated data files
for...
|
by: x taol |
last post by:
i want to set multi columns to primary key using vba.
Set idx = tbl.CreateIndex("hh")
idx.Fields.Append idx.CreateField("fld2")
idx.Primary = True
idx.Unique = False
tbl.Indexes.Append idx
...
|
by: hariharanmca |
last post by:
How to Shift or Copy Data from Sqlserver to MS Access using Stored procedure
I am passing the clint MS Access file path in that i want to insert or update data in that MS Access file using...
|
by: VictorS |
last post by:
Hello everybody!
I’ve inherited a moderate size Employee database.
Main Employees table has a SSN as a primary key, but it is a “Text” data type.
I’m now told to delete social security numbers due...
|
by: fniles |
last post by:
Can I change the connection time out in VB.Net 2003 and MS Access (using
OLEDbConnection) ?
When I use Connect Timeout = 60 in the connectionstring, I get an error
"Multiple-step OLE DB operation...
|
by: ofilha |
last post by:
I need to format a column in excel but would like to do it from Access using VB. I have hacked around and found a few ways to get to the workbook but have a problem getting to the sheet i want. But,...
|
by: hotflash |
last post by:
Hi Mark et All,
I understand that you created a very professional document and a similar issue forum out there regarding to Inserting the checkbox value into MS Access using ASP however; I am so...
|
by: shivapadma |
last post by:
is it possible to access MS ACCESS using c++?
if yes,then how can i access?
is it similar to java language?
i.e,
1.loading
2.connecting
3.using
|
by: macca |
last post by:
hi,
Does anybody know if it's possible to log into Outlook Web Access
using PHP?
I mean, if I have the username, password and email address I need to
have a link that opens directly into...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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: 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: 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: 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: 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...
| |