473,556 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rename field in Access DB

I'm manipulating an MS Access db via ADODB with win32com.client . I
want to rename a field within a table, but I don't know how to. I
assume there is a line of SQL which will do it, but nothing I've tried
(from searching) has worked.
Basic code:

import win32com.client
connection = win32com.client .Dispatch(r'ADO DB.Connection')
DSN = 'PROVIDER=Micro soft.Jet.OLEDB. 4.0;DATA SOURCE=dbfile.m db;'
connection.Open (DSN)
connection.Exec ute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Clos e()

Anyone know how to get this to work?

Iain
Jun 27 '08 #1
3 2698
Iain King wrote:
I'm manipulating an MS Access db via ADODB with win32com.client . I
want to rename a field within a table, but I don't know how to. I
assume there is a line of SQL which will do it, but nothing I've tried
(from searching) has worked.
Basic code:

import win32com.client
connection = win32com.client .Dispatch(r'ADO DB.Connection')
DSN = 'PROVIDER=Micro soft.Jet.OLEDB. 4.0;DATA SOURCE=dbfile.m db;'
connection.Open (DSN)
connection.Exec ute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Clos e()

Anyone know how to get this to work?
I don't think Access supports SQL on the DDL-side. Try looking into the
Access COM-Api, maybe you can get a reference to the table and modify it.

Diez
Jun 27 '08 #2
Iain King wrote:
I'm manipulating an MS Access db via ADODB with win32com.client . I
want to rename a field within a table, but I don't know how to. I
assume there is a line of SQL which will do it, but nothing I've tried
(from searching) has worked.
Basic code:

import win32com.client
connection = win32com.client .Dispatch(r'ADO DB.Connection')
DSN = 'PROVIDER=Micro soft.Jet.OLEDB. 4.0;DATA SOURCE=dbfile.m db;'
connection.Open (DSN)
connection.Exec ute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Clos e()
<code>
import os, sys
from win32com.client .gencache import EnsureDispatch as Dispatch

DATABASE_FILEPA TH = r"c:\temp\test. mdb"
CONNECTION_STRI NG = "Provider=Micro soft.Jet.OLEDB. 4.0; data Source=%s" % \
DATABASE_FILEPA TH

if os.path.exists (DATABASE_FILEP ATH):
os.remove (DATABASE_FILEP ATH)

adox = Dispatch ("ADOX.Catalog" )
adox.Create (CONNECTION_STR ING)
adox = None

db = Dispatch ('ADODB.Connect ion')
db.Open (CONNECTION_STR ING)
try:
db.Execute ('CREATE TABLE dtest (id INT, data INT)')
db.Execute ('INSERT INTO dtest (id, data) VALUES (1, 2)')

try:
db.Execute ('SELECT id, newdata FROM dtest')
except:
print "FAILED as expected"
else:
print "SUCCEEDED unexpectedly"

try:
db.Execute ('SELECT id, data FROM dtest')
except:
print "FAILED unexpectedly"
else:
print "SUCCEEDED as expected"

adox = Dispatch ("ADOX.Catalog" )
adox.ActiveConn ection = db
adox.Tables ("dtest").Colum ns ("data").Nam e = "newdata"
adox.Tables.Ref resh ()
finally:
db.Close ()

db = Dispatch ('ADODB.Connect ion')
db.Open (CONNECTION_STR ING)
try:

try:
db.Execute ('SELECT id, data FROM dtest')
except:
print "FAILED as expected"
else:
print "SUCCEEDED unexpectedly"

try:
db.Execute ('SELECT id, newdata FROM dtest')
except:
print "FAILED unexpectedly"
else:
print "SUCCEEDED as expected"

finally:
db.Close ()

</code>

TJG
Jun 27 '08 #3
On May 14, 4:29 pm, Tim Golden <m...@timgolden .me.ukwrote:
Iain King wrote:
I'm manipulating an MS Access db via ADODB with win32com.client . I
want to rename a field within a table, but I don't know how to. I
assume there is a line of SQL which will do it, but nothing I've tried
(from searching) has worked.
Basic code:
import win32com.client
connection = win32com.client .Dispatch(r'ADO DB.Connection')
DSN = 'PROVIDER=Micro soft.Jet.OLEDB. 4.0;DATA SOURCE=dbfile.m db;'
connection.Open (DSN)
connection.Exec ute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Clos e()

<code>
import os, sys
from win32com.client .gencache import EnsureDispatch as Dispatch

DATABASE_FILEPA TH = r"c:\temp\test. mdb"
CONNECTION_STRI NG = "Provider=Micro soft.Jet.OLEDB. 4.0; data Source=%s" % \
DATABASE_FILEPA TH

if os.path.exists (DATABASE_FILEP ATH):
os.remove (DATABASE_FILEP ATH)

adox = Dispatch ("ADOX.Catalog" )
adox.Create (CONNECTION_STR ING)
adox = None

db = Dispatch ('ADODB.Connect ion')
db.Open (CONNECTION_STR ING)
try:
db.Execute ('CREATE TABLE dtest (id INT, data INT)')
db.Execute ('INSERT INTO dtest (id, data) VALUES (1, 2)')

try:
db.Execute ('SELECT id, newdata FROM dtest')
except:
print "FAILED as expected"
else:
print "SUCCEEDED unexpectedly"

try:
db.Execute ('SELECT id, data FROM dtest')
except:
print "FAILED unexpectedly"
else:
print "SUCCEEDED as expected"

adox = Dispatch ("ADOX.Catalog" )
adox.ActiveConn ection = db
adox.Tables ("dtest").Colum ns ("data").Nam e = "newdata"
adox.Tables.Ref resh ()
finally:
db.Close ()

db = Dispatch ('ADODB.Connect ion')
db.Open (CONNECTION_STR ING)
try:

try:
db.Execute ('SELECT id, data FROM dtest')
except:
print "FAILED as expected"
else:
print "SUCCEEDED unexpectedly"

try:
db.Execute ('SELECT id, newdata FROM dtest')
except:
print "FAILED unexpectedly"
else:
print "SUCCEEDED as expected"

finally:
db.Close ()

</code>

TJG

Excellent, many thanks.

Iain
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4281
by: TroelsM | last post by:
Hi there ! I am trying to rename a column i Access Set Conn1 = Server.CreateObject("ADODB.Connection") Conn1.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\fpdb\currency.mdb") sSQL = "ALTER TABLE DatoCurrency " sSQL = sSQL & " COLUMN " & ucase(retcur) & " RENAME TO " &...
3
1541
by: campbellbrian2001 | last post by:
This is probably quite simple to the VB veterans... here goes, I have a database of product images with the name and server address hardcoded into the field value (e.g. \\PC9\imageFiles\8965341.jpg) whereas \\PC9\imageFiles was the old location of the .jpg 8965341). We have switched the server names and now all the images need to be renamed...
2
9759
by: Gary T. | last post by:
I have a subform (that is linked to a table) in a blank form. When the table is viewed in its subform state in the form, the headings are not user friendly. ie. CustN I would want it to read in the subform heading column as Customer Name without affecting the actually field heading in the table itself..... if that makes sense. Any...
1
3270
by: solar | last post by:
In my subform i have controls which i want to rename but i do not want to change the real names of the fields in the tables,nor add the name to the Caption in the field of the table.Is it possible ? For example, in the table i have a field called Quantity.I want to rename it to Pieces.I want to do it in the subform,and not in the table...
4
1595
by: Lyle Fairfield | last post by:
I had a Sproc which returned a field called Details. I had the Form Wizard create a continuous form with the Sproc as the RecordSource (as a shell for me to modify). I renamed the Details textbox to txtDetails. Except I didn't. I grabbed DETAIL from the pulldown and renamed it to TxtDetail.
3
5424
by: ineedahelp | last post by:
Hi, can anyone help me with the code necessary to rename a field in an access table. I have tried the ALTER TABLE method but it doesn't like my variable names. Here is the code and thank you for any help! ALTER TABLE tblClientTable RENAME COLUMN fldRateField TO "MarketPrice"
3
1959
by: severitt | last post by:
I am a new Access user. I have a field in one table labled DocID that I need to rename the values contained in that field. Once the values in this field are renamed, how do I update the values of this field in a seperate table automatically? Can someone please give me step by step instructions.
1
6023
by: EricRogers | last post by:
Thanks for any help in advance! Here 5 different cmdStrings that I have tried but they all have a syntax error in it that I cannot spot: (cmdString is dimmed as a String) cmdString = "ALTER TABLE DealData CHANGE F&IManager FIManager VARCHAR(50)" cmdString = "ALTER TABLE DealData CHANGE F&IManager FIManager" cmdString = "ALTER TABLE...
11
6486
by: ABC | last post by:
How to rename files with support wildcard ?
0
8060
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7588
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5452
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3596
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
865
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.