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

How can I modify fields types via VBA code?

Hi to everyone! I'm new in this NG. I'm posting this message just for
learning how can I modify the type of a field in a table via VBA code.
Is that possible? And is anyone so kind to post me some VBA example code?
I'd like to change a numeric field into a text one but I'd like to
create an automatic procedure to do this..Maybe I can do it via SQL
query but I don't know if ALTER TABLE or something similar is supported
in Access.
Thanks a lot in advance to everyone.

Giuseppe
Nov 13 '05 #1
6 18412
ALTER TABLE is supported in JET 4 (Access 2000 and later), and is the
simplest way to alter a field type or size.

This example converts a field to Text type, 100 characters in size:
strSql = "ALTER TABLE MyTable ALTER COLUMN MyField TEXT(100);"

Execute the query statement under DAO like this:
dbEngine(0)(0).Execute strSql, dbFailOnError
or under ADO like this:
CurrentProject.Connection.Execute strSql

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Giuseppe Chielli" <gi******@NOSPAMlibero.it> wrote in message
news:d5**********@balena.cs.interbusiness.it...
Hi to everyone! I'm new in this NG. I'm posting this message just for
learning how can I modify the type of a field in a table via VBA code.
Is that possible? And is anyone so kind to post me some VBA example code?
I'd like to change a numeric field into a text one but I'd like to create
an automatic procedure to do this..Maybe I can do it via SQL query but I
don't know if ALTER TABLE or something similar is supported in Access.
Thanks a lot in advance to everyone.

Giuseppe

Nov 13 '05 #2
Allen Browne wrote:
ALTER TABLE is supported in JET 4 (Access 2000 and later), and is the
simplest way to alter a field type or size.

This example converts a field to Text type, 100 characters in size:
strSql = "ALTER TABLE MyTable ALTER COLUMN MyField TEXT(100);"

Thanks a lot. It works properly.But I have a new question now: how can I
insert a new field into a table before every other field (so as the
first field)?

Thanks in advance.

Giuseppe
Nov 13 '05 #3
Use DAO to set the OrdinalPosition of the Fields in the TableDef.

Of course, it may not matter, i.e. you can use a query as the source for
whatever needs the fields arranged in a certain way.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Giuseppe Chielli" <gi******@NOSPAMlibero.it> wrote in message
news:d5**********@carabinieri.cs.interbusiness.it. ..
Allen Browne wrote:
ALTER TABLE is supported in JET 4 (Access 2000 and later), and is the
simplest way to alter a field type or size.

This example converts a field to Text type, 100 characters in size:
strSql = "ALTER TABLE MyTable ALTER COLUMN MyField TEXT(100);"

Thanks a lot. It works properly.But I have a new question now: how can I
insert a new field into a table before every other field (so as the first
field)?

Thanks in advance.

Giuseppe

Nov 13 '05 #4
Allen Browne wrote:
Use DAO to set the OrdinalPosition of the Fields in the TableDef.

And what about using ADO? Can I modify the fields collection?
Nov 13 '05 #5
No, I don't see an ADO/ADOX property for doing this.

Of course, DAO is the library designed for Access.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Giuseppe Chielli" <gi******@NOSPAMlibero.it> wrote in message
news:d5**********@balena.cs.interbusiness.it...
Allen Browne wrote:
Use DAO to set the OrdinalPosition of the Fields in the TableDef.

And what about using ADO? Can I modify the fields collection?

Nov 13 '05 #6
Allen Browne wrote:
No, I don't see an ADO/ADOX property for doing this.

Of course, DAO is the library designed for Access.


Ok, I see. Thanks a lot from Italy! :)
Nov 13 '05 #7

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

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
4
by: John | last post by:
I am using code provided by Mr. Steele that allows for my MDB to dynamically connect to remote SQL server databases. The code works fine as follows: Type TableDetails TableName As String...
3
by: mrhicks | last post by:
Hello all, I have a question regarding efficeny and how to find the best approach when trying to find flag with in a structure of bit fields. I have several structures which look similar to ...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
0
by: JDMils | last post by:
I am having trouble finding the AutoNumber field of my database with this code. The code is used to replicate a specific table, reproducing all columns including indexes and Primary Keys (there is...
4
by: lesperancer | last post by:
it looks like this will save many versions of a relationship window, but based on the fact that the same tables are displayed in the relationship window and it will restore versions of what was...
4
by: Bob | last post by:
Hi all, I'm trying to import data, modify the data then insert it into a new table. The code below works fine for it but it takes a really long time for 15,000 odd records. Is there a way I...
23
by: no1zson | last post by:
I have been adding buttons to my GUI to manipulate list data. I added a Delete button this morning in case I decide I no longer needed a particular element. I am now working on a modify button, in...
14
by: Thurston Manson | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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 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.