dbTalk Databases Forums  

Redefine field data type

comp.databases.ms-access comp.databases.ms-access


Discuss Redefine field data type in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Josh
 
Posts: n/a

Default Redefine field data type - 11-30-2004 , 09:59 AM






Hi,

I am trying to write a function in a module in MS Access 2000 that
will change the data type of a field called 'Start' in table
'bo_cpm_CS01ALL'. Here is the code that I have done so far but when I
run it nothing happens...no errors or changes to the table. The code
finds the table and field, creates a new field called 'temp' then
copys 'Start' to 'temp' then deletes 'Start' and renames 'temp'. The
field 'Start' has data type dbDouble.

Any help would be great!!

Public Function ChangeFieldType()

'Purpose: Changes a field's datatype

Dim db As DAO.Database
Dim tdef As DAO.TableDef 'Table to modify
Dim fldOld As DAO.Field 'Field to modify
Dim fldNew As DAO.Field 'Destination field
Dim Property As DAO.Property 'Field property
Dim strSQL As String 'SQL string to move the data

Set db = CurrentDb

'Get the table definition
Set tdef = db.TableDefs("bo_cpm_CS01ALL")

'Get the original field
Set fldOld = tdef.Fields("Start")

'Create the new field
Set fldNew = tdef.CreateField("temp", dbDate)

'Append the field
tdef.Fields.Append fldNew

'Copy the data
strSQL = "UPDATE bo_cpm_CS01ALL Set bo_cpm_CS01ALL.Start = temp"
db.Execute strSQL, dbFailOnError

'Delete the original field
tdef.Fields.Delete "Start"

'Rename the new field
fldNew.Name = "Start"

End Function

Reply With Quote
  #2  
Old   
Allen Browne
 
Posts: n/a

Default Re: Redefine field data type - 11-30-2004 , 10:28 AM






Is your update query backwards:
strSQL = "UPDATE bo_cpm_CS01ALL Set temp = Start;"

In Access 2000 and later, you can change the field type on the fly:
strSql = "ALTER TABLE bo_cpm_CS01ALL ALTER COLUMN Start DATE;"
dbEngine(0)(0).Execute strSql, dbFailOnError
--
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.

"Josh" <joshua.paquin (AT) tdsecurities (DOT) com> wrote

Quote:
I am trying to write a function in a module in MS Access 2000 that
will change the data type of a field called 'Start' in table
'bo_cpm_CS01ALL'. Here is the code that I have done so far but when I
run it nothing happens...no errors or changes to the table. The code
finds the table and field, creates a new field called 'temp' then
copys 'Start' to 'temp' then deletes 'Start' and renames 'temp'. The
field 'Start' has data type dbDouble.

Any help would be great!!

Public Function ChangeFieldType()

'Purpose: Changes a field's datatype

Dim db As DAO.Database
Dim tdef As DAO.TableDef 'Table to modify
Dim fldOld As DAO.Field 'Field to modify
Dim fldNew As DAO.Field 'Destination field
Dim Property As DAO.Property 'Field property
Dim strSQL As String 'SQL string to move the data

Set db = CurrentDb

'Get the table definition
Set tdef = db.TableDefs("bo_cpm_CS01ALL")

'Get the original field
Set fldOld = tdef.Fields("Start")

'Create the new field
Set fldNew = tdef.CreateField("temp", dbDate)

'Append the field
tdef.Fields.Append fldNew

'Copy the data
strSQL = "UPDATE bo_cpm_CS01ALL Set bo_cpm_CS01ALL.Start = temp"
db.Execute strSQL, dbFailOnError

'Delete the original field
tdef.Fields.Delete "Start"

'Rename the new field
fldNew.Name = "Start"

End Function



Reply With Quote
  #3  
Old   
Chris Mills
 
Posts: n/a

Default Re: Redefine field data type - 12-01-2004 , 02:42 AM



Excellent, Allen!

Would that apply to upgrading a BE via an FE, assuming one had design perms to
the table?

However, the originally posted method also works, if done properly, and is
compatible with all versions.

My code, similar to that posted, appends a new field and everything is fine
(for me). Maybe they are trying to write to it before it is "updated/stored"?
I can't see any other reason.

Chris

"Allen Browne" <AllenBrowne (AT) SeeSig (DOT) Invalid> wrote in message
Quote:
In Access 2000 and later, you can change the field type on the fly:



Reply With Quote
  #4  
Old   
Chris Mills
 
Posts: n/a

Default Re: Redefine field data type - 12-01-2004 , 03:01 AM



A "Doevents", or some such delay/cleanup, might assist between running code
and trying to "execute" something. Who can know what order things are executed
in, if you don't place some "Doevents" amongst it.

Merely a guess!
Chris

"Josh" <joshua.paquin (AT) tdsecurities (DOT) com> wrote

Quote:
Hi,

I am trying to write a function in a module in MS Access 2000 that
will change the data type of a field called 'Start' in table
'bo_cpm_CS01ALL'. Here is the code that I have done so far but when I
run it nothing happens...no errors or changes to the table. The code
finds the table and field, creates a new field called 'temp' then
copys 'Start' to 'temp' then deletes 'Start' and renames 'temp'. The
field 'Start' has data type dbDouble.

Any help would be great!!

Public Function ChangeFieldType()

'Purpose: Changes a field's datatype

Dim db As DAO.Database
Dim tdef As DAO.TableDef 'Table to modify
Dim fldOld As DAO.Field 'Field to modify
Dim fldNew As DAO.Field 'Destination field
Dim Property As DAO.Property 'Field property
Dim strSQL As String 'SQL string to move the data

Set db = CurrentDb

'Get the table definition
Set tdef = db.TableDefs("bo_cpm_CS01ALL")

'Get the original field
Set fldOld = tdef.Fields("Start")

'Create the new field
Set fldNew = tdef.CreateField("temp", dbDate)

'Append the field
tdef.Fields.Append fldNew

'Copy the data
strSQL = "UPDATE bo_cpm_CS01ALL Set bo_cpm_CS01ALL.Start = temp"
db.Execute strSQL, dbFailOnError

'Delete the original field
tdef.Fields.Delete "Start"

'Rename the new field
fldNew.Name = "Start"

End Function



Reply With Quote
  #5  
Old   
Brendan Reynolds
 
Posts: n/a

Default Re: Redefine field data type - 12-01-2004 , 08:16 AM



If the code is making changes to the structure or content of the database, I
find that a DbEngine.Idle is sometimes required. For example, in this
situation I'd try a DbEngine.Idle between the Fields.Append and db.Execute
statements.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.


"Chris Mills" <phad_nospam (AT) cleardotnet (DOT) nz> wrote

Quote:
A "Doevents", or some such delay/cleanup, might assist between running code
and trying to "execute" something. Who can know what order things are
executed
in, if you don't place some "Doevents" amongst it.

Merely a guess!
Chris

"Josh" <joshua.paquin (AT) tdsecurities (DOT) com> wrote in message
news:ac1dc21f.0411300759.3e7126b4 (AT) posting (DOT) google.com...
Hi,

I am trying to write a function in a module in MS Access 2000 that
will change the data type of a field called 'Start' in table
'bo_cpm_CS01ALL'. Here is the code that I have done so far but when I
run it nothing happens...no errors or changes to the table. The code
finds the table and field, creates a new field called 'temp' then
copys 'Start' to 'temp' then deletes 'Start' and renames 'temp'. The
field 'Start' has data type dbDouble.

Any help would be great!!

Public Function ChangeFieldType()

'Purpose: Changes a field's datatype

Dim db As DAO.Database
Dim tdef As DAO.TableDef 'Table to modify
Dim fldOld As DAO.Field 'Field to modify
Dim fldNew As DAO.Field 'Destination field
Dim Property As DAO.Property 'Field property
Dim strSQL As String 'SQL string to move the data

Set db = CurrentDb

'Get the table definition
Set tdef = db.TableDefs("bo_cpm_CS01ALL")

'Get the original field
Set fldOld = tdef.Fields("Start")

'Create the new field
Set fldNew = tdef.CreateField("temp", dbDate)

'Append the field
tdef.Fields.Append fldNew

'Copy the data
strSQL = "UPDATE bo_cpm_CS01ALL Set bo_cpm_CS01ALL.Start = temp"
db.Execute strSQL, dbFailOnError

'Delete the original field
tdef.Fields.Delete "Start"

'Rename the new field
fldNew.Name = "Start"

End Function





Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.