Do not use on any
shared computer
September 5, 2008 08:59pm pdt
 
[x]
Attachment Details

How do I programmatically change an existing Field's data type?

I use the attached code snippet to change field names and, optionally, to change the field's description.  How can I modify the code below to optionally change the field's data type too?  Assume there are no errors in the data conversion.  For example, an existing field labeled strMyTextField, only has numbers and I want to change it to the Integer data type and rename it intMyTextField.  In other words, when I change the data type manually in table design view from Text to Number, there are no errors.  The DAO Field type reference, i.e. the Constant names and Decimal values are shown below (source: http://allenbrowne.com/ser-49.html):
DAO constant                       DAO Decimal
dbBoolean                                1
dbByte                                2
dbInteger                                3
dbLong                                4
dbCurrency                                5
dbSingle                                6
dbDouble                                7
dbDate                                8
dbBinary                                9
dbText                               10
dbLongBinary                               11
dbMemo                               12
dbMemo with attributes              12
dbGUID                               15
dbDecimal                               20
dbAttachment                                101
dbComplexByte                                102
dbComplexInteger      103
dbComplexLong                                 104
dbComplexSingle      105
dbComplexDouble      106
dbComplexGUID                                 107
dbComplexDecimal      108
dbComplexText                                 109
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
Public Function ChangeFieldName(strTableName As String, strOldFieldName As String, strNewFieldName As String, Optional strNewFieldDescription As String)      
    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim prp As Property
    
    Set db = CurrentDb
    Set tdf = db.TableDefs(strTableName)
    
    On Error GoTo ErrHandler
    
    If Not IsNull(strNewFieldDescription) And strNewFieldDescription <> "" Then
        With tdf.Fields(strOldFieldName)
            .Properties("Description").Value = strNewFieldDescription
        End With
    End If
    
    tdf(strOldFieldName).Name = strNewFieldName
   
ErrHandler:
    Select Case Err
            
        Case 3265&  'Table or Field name invalid
            MsgBox tdf.Name & " table or field doesn't exist"
                
        Case 3270
            tdf.Fields(strOldFieldName).Properties.Append _
            tdf.Fields(strOldFieldName).CreateProperty("Description", dbText, strNewFieldDescription)
            
            tdf(strOldFieldName).Name = strNewFieldName
    
            Err.Clear
                        
        Case Else
            Debug.Print "    AppendDeleteField() Error " & Err & ": " & Error
    End Select
   
    Set prp = Nothing
    Set tdf = Nothing
    Set db = Nothing
End Function
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Microsoft
Question Asked By: bobbat
Solution Provided By: capricorn1
Participating Experts: 2
Solution Grade: A
Views: 26
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by jppinto
Expert Comment by jppinto:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by capricorn1

Rank: Savant

Accepted Solution by capricorn1:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by capricorn1

Rank: Savant

Expert Comment by capricorn1:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by capricorn1

Rank: Savant

Expert Comment by capricorn1:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by bobbat
Author Comment by bobbat:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628