473,322 Members | 1,259 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,322 software developers and data experts.

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'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=dbfile.mdb;'
connection.Open(DSN)
connection.Execute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Close()

Anyone know how to get this to work?

Iain
Jun 27 '08 #1
3 2676
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'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=dbfile.mdb;'
connection.Open(DSN)
connection.Execute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Close()

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'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=dbfile.mdb;'
connection.Open(DSN)
connection.Execute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Close()
<code>
import os, sys
from win32com.client.gencache import EnsureDispatch as Dispatch

DATABASE_FILEPATH = r"c:\temp\test.mdb"
CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0; data Source=%s" % \
DATABASE_FILEPATH

if os.path.exists (DATABASE_FILEPATH):
os.remove (DATABASE_FILEPATH)

adox = Dispatch ("ADOX.Catalog")
adox.Create (CONNECTION_STRING)
adox = None

db = Dispatch ('ADODB.Connection')
db.Open (CONNECTION_STRING)
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.ActiveConnection = db
adox.Tables ("dtest").Columns ("data").Name = "newdata"
adox.Tables.Refresh ()
finally:
db.Close ()

db = Dispatch ('ADODB.Connection')
db.Open (CONNECTION_STRING)
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'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=dbfile.mdb;'
connection.Open(DSN)
connection.Execute("ALTER TABLE tablename CHANGE from to") #this sql
doesn't work
connection.Close()

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

DATABASE_FILEPATH = r"c:\temp\test.mdb"
CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0; data Source=%s" % \
DATABASE_FILEPATH

if os.path.exists (DATABASE_FILEPATH):
os.remove (DATABASE_FILEPATH)

adox = Dispatch ("ADOX.Catalog")
adox.Create (CONNECTION_STRING)
adox = None

db = Dispatch ('ADODB.Connection')
db.Open (CONNECTION_STRING)
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.ActiveConnection = db
adox.Tables ("dtest").Columns ("data").Name = "newdata"
adox.Tables.Refresh ()
finally:
db.Close ()

db = Dispatch ('ADODB.Connection')
db.Open (CONNECTION_STRING)
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
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=" &...
3
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....
2
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...
1
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...
4
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...
3
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...
3
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...
1
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...
11
by: ABC | last post by:
How to rename files with support wildcard ?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.