473,668 Members | 2,355 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 2706
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
4290
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 " & UCASE(chkstring(request.form("cur"),"SQLString")) conn1.execute sSQL conn1.close
3
1546
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 \\F:\imageFiles. Obviously if I had not misstakingly hardcoded the path I could solve the problem in...
2
9768
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 suggestions? thanks
1
3281
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 itself, where i might have written the word Pieces in the field Caption.I have tried to rename the...
4
1598
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
5438
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
1964
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
6030
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 DealData RENAME COLUMN F&IManager TO FIManager" cmdString = "ALTER TABLE DealData RENAME COLUMN " &...
11
6502
by: ABC | last post by:
How to rename files with support wildcard ?
0
8459
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8577
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
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
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.