PC Review


Reply
Thread Tools Rate Thread

Change Field size with VBA

 
 
=?Utf-8?B?U2NvdHQgQnVya2U=?=
Guest
Posts: n/a
 
      30th Mar 2006
I have a table "Employees" with a field "Emplcode".

Can I change the Field size of "Emplcode" form 3 to 6 with VBA?
Without loosing existing information!

Scott Burke
 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGljayBE?=
Guest
Posts: n/a
 
      30th Mar 2006
Yes, you can increase the field size over-riding the field size in the table
definitions without losing existing text. However, the field size cannot
exceed the field size in the table definition.
--
Hopefully helpful,


"Scott Burke" wrote:

> I have a table "Employees" with a field "Emplcode".
>
> Can I change the Field size of "Emplcode" form 3 to 6 with VBA?
> Without loosing existing information!
>
> Scott Burke

 
Reply With Quote
 
=?Utf-8?B?U2NvdHQgQnVya2U=?=
Guest
Posts: n/a
 
      30th Mar 2006
Hi Dick. Maybe I am reading this wrong. Did you just say "yes" you can but
you cant go larger than max size of a text field?
If so can you give me a sample code on how to change the field size?

Thank You.
Scott Burke

"Dick D" wrote:

> Yes, you can increase the field size over-riding the field size in the table
> definitions without losing existing text. However, the field size cannot
> exceed the field size in the table definition.
> --
> Hopefully helpful,
>
>
> "Scott Burke" wrote:
>
> > I have a table "Employees" with a field "Emplcode".
> >
> > Can I change the Field size of "Emplcode" form 3 to 6 with VBA?
> > Without loosing existing information!
> >
> > Scott Burke

 
Reply With Quote
 
chris.nebinger@gmail.com
Guest
Posts: n/a
 
      30th Mar 2006
You can't change the size of an existing field without generating an
error, AFAIK. You can, however, create a temp field of the right size,
copy data over, delete the old one, create the new one, copy the data
over (to preserve field names), and delete the temp field. Any indexes
or relationships will generate and error, however.

Sub FieldChange()
'This will fail if the field is a part of any index or
relationship.....
'Additional code is needed if that is a possibility
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set dbs = CurrentDb
Set tdf = dbs.TableDefs("Contacts")
Set fld = tdf.CreateField("LastName1", dbText, 255)
tdf.Fields.Append fld
dbs.Execute "Update Contacts set lastname1 = lastname"
tdf.Fields.Delete "LastName"
tdf.Fields.Refresh
Set fld = tdf.CreateField("LastName", dbText, 255)
tdf.Fields.Append fld
dbs.Execute "Update Contacts set lastname = lastname1"
tdf.Fields.Delete "LastName1"
tdf.Fields.Refresh
Set fld = Nothing
Set tdf = Nothing
Set dbs = Nothing
End Sub



Chris Nebinger

 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      30th Mar 2006
Run the following DDL:

ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)

If your database isn't split, you could use:

CurrentDb.Execute "ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)",
dbFailOnError

If your database is split, you need to run it against the backend, not the
current database:

Dim dbBackend As DAO.Database
Dim strBackend As String
Dim strDDL As String

strBackend = "full path to backend database"
strDDL = "ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)"

Set dbBackend = OpenDatabase(strBackend)
dbBackend.Execute strDDL, dbFailOnError





--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Scott Burke" <(E-Mail Removed)> wrote in message
news:E93AC641-DE79-4099-83AF-(E-Mail Removed)...
>I have a table "Employees" with a field "Emplcode".
>
> Can I change the Field size of "Emplcode" form 3 to 6 with VBA?
> Without loosing existing information!
>
> Scott Burke



 
Reply With Quote
 
=?Utf-8?B?U2NvdHQgQnVya2U=?=
Guest
Posts: n/a
 
      31st Mar 2006
Hi Douglas. Nice try but It does not work on link tables. It Figures!

Chris, I have not tried your ideal yet. Maybe on Monday.

Have a good weekend.
Scott Burke



"Douglas J. Steele" wrote:

> Run the following DDL:
>
> ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)
>
> If your database isn't split, you could use:
>
> CurrentDb.Execute "ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)",
> dbFailOnError
>
> If your database is split, you need to run it against the backend, not the
> current database:
>
> Dim dbBackend As DAO.Database
> Dim strBackend As String
> Dim strDDL As String
>
> strBackend = "full path to backend database"
> strDDL = "ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)"
>
> Set dbBackend = OpenDatabase(strBackend)
> dbBackend.Execute strDDL, dbFailOnError
>
>
>
>
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "Scott Burke" <(E-Mail Removed)> wrote in message
> news:E93AC641-DE79-4099-83AF-(E-Mail Removed)...
> >I have a table "Employees" with a field "Emplcode".
> >
> > Can I change the Field size of "Emplcode" form 3 to 6 with VBA?
> > Without loosing existing information!
> >
> > Scott Burke

>
>
>

 
Reply With Quote
 
chris.nebinger@gmail.com
Guest
Posts: n/a
 
      31st Mar 2006
Linked tables are tougher. You have to change CurrentDB to
OpenDatabase(strFileName). Also, you will probably have to delete the
link and create it again.


Chris

 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      31st Mar 2006
Sure it does.

That's the second case I talked about:

>> Dim dbBackend As DAO.Database
>> Dim strBackend As String
>> Dim strDDL As String
>>
>> strBackend = "full path to backend database"
>> strDDL = "ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)"
>>
>> Set dbBackend = OpenDatabase(strBackend)
>> dbBackend.Execute strDDL, dbFailOnError


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Scott Burke" <(E-Mail Removed)> wrote in message
news:4B1BE5EC-F619-4E90-AC08-(E-Mail Removed)...
> Hi Douglas. Nice try but It does not work on link tables. It Figures!
>
> Chris, I have not tried your ideal yet. Maybe on Monday.
>
> Have a good weekend.
> Scott Burke
>
>
>
> "Douglas J. Steele" wrote:
>
>> Run the following DDL:
>>
>> ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)
>>
>> If your database isn't split, you could use:
>>
>> CurrentDb.Execute "ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)",
>> dbFailOnError
>>
>> If your database is split, you need to run it against the backend, not
>> the
>> current database:
>>
>> Dim dbBackend As DAO.Database
>> Dim strBackend As String
>> Dim strDDL As String
>>
>> strBackend = "full path to backend database"
>> strDDL = "ALTER TABLE Employees ALTER COLUMN Emplcode Text(6)"
>>
>> Set dbBackend = OpenDatabase(strBackend)
>> dbBackend.Execute strDDL, dbFailOnError
>>
>>
>>
>>
>>
>> --
>> Doug Steele, Microsoft Access MVP
>> http://I.Am/DougSteele
>> (no private e-mails, please)
>>
>>
>> "Scott Burke" <(E-Mail Removed)> wrote in message
>> news:E93AC641-DE79-4099-83AF-(E-Mail Removed)...
>> >I have a table "Employees" with a field "Emplcode".
>> >
>> > Can I change the Field size of "Emplcode" form 3 to 6 with VBA?
>> > Without loosing existing information!
>> >
>> > Scott Burke

>>
>>
>>



 
Reply With Quote
 
TC
Guest
Posts: n/a
 
      1st Apr 2006
Just for my interest: why do you wn to do this through code (as opposed
to manually)?

TC (MVP Access)
http://tc2.atspace.com

 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      1st Apr 2006
"TC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Just for my interest: why do you wn to do this through code (as opposed
> to manually)?


Seems like a reasonable request to me, TC.

If you've deployed your application to a client at a remote site, having a
script to make any table changes required for the next version of the
application seems logical. I just finished a two-part column for Access
Advisor about how to do this, so I hope my readers agree with me! <g>

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I change a field size? Tony Williams Microsoft Access Forms 2 15th Aug 2007 07:09 PM
You can't change the data type or field size of this field error. =?Utf-8?B?TWVsYW5pZQ==?= Microsoft Access 2 3rd Aug 2007 03:08 PM
change field size =?Utf-8?B?QXJpZWw=?= Microsoft Access VBA Modules 2 11th Dec 2006 03:15 AM
Change a Field Size =?Utf-8?B?SkEgUGVhY2hyb3Nl?= Microsoft Access 2 27th Sep 2005 09:33 PM
cannot change field size =?Utf-8?B?SlJT?= Microsoft Access 2 23rd Nov 2004 05:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:27 PM.