Change column name?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JustJim
    Recognized Expert Contributor
    • May 2007
    • 407

    Change column name?

    Hi

    If all you can do with ALTER TABLE....ALTER COLUMN is change the datatype/size then how do I change the name of a field?

    I have several database files spread all over the state and some of the users (Darn users!) have changed a field from "Datum" to "MapDatum" and I need to change it back because it is causing problems with the front end.

    I suspect that I'm going to have to determine if the field MapDatum exists and if it does, make a new column, copy all the data over and drop the old column.

    Is there any way to just change the name in code? Otherwise it's probably going to be quicker (but nowhere near as elegant) to open each database, open the table in design view and change it there!

    Jim
  • Jim Doherty
    Recognized Expert Contributor
    • Aug 2007
    • 897

    #2
    Originally posted by JustJim
    Hi

    If all you can do with ALTER TABLE....ALTER COLUMN is change the datatype/size then how do I change the name of a field?

    I have several database files spread all over the state and some of the users (Darn users!) have changed a field from "Datum" to "MapDatum" and I need to change it back because it is causing problems with the front end.

    I suspect that I'm going to have to determine if the field MapDatum exists and if it does, make a new column, copy all the data over and drop the old column.

    Is there any way to just change the name in code? Otherwise it's probably going to be quicker (but nowhere near as elegant) to open each database, open the table in design view and change it there!

    Jim
    This will loop 'all' tables seeking the offending field and change it for the 'current' database Jim..... but I'm a little confused as to the 'all over the state' bit you speak about where is the table located obviously this code below does not target external databases in any logical sweep merely the current one?

    Code:
     Dim db As DAO.Database 
    Dim tbl As DAO.TableDef
    Dim fld As DAO.Field
    Set db = CurrentDb()
    For Each tbl In db.TableDefs
    For Each fld In tbl.Fields
    	 If fld.Name = "MapDatum" Then fld.Name = "Datum"
    Next
    Next
    Regards

    Jim :)

    Comment

    • JustJim
      Recognized Expert Contributor
      • May 2007
      • 407

      #3
      Originally posted by Jim Doherty
      This will loop 'all' tables seeking the offending field and change it for the 'current' database Jim..... but I'm a little confused as to the 'all over the state' bit you speak about where is the table located obviously this code below does not target external databases in any logical sweep merely the current one?

      Code:
       Dim db As DAO.Database 
      Dim tbl As DAO.TableDef
      Dim fld As DAO.Field
      Set db = CurrentDb()
      For Each tbl In db.TableDefs
      For Each fld In tbl.Fields
      	 If fld.Name = "MapDatum" Then fld.Name = "Datum"
      Next
      Next
      Regards

      Jim :)
      That will do nicely thanks Jim. "All over the state" means just that, on computers all over the state of Victoria, Australia. It's a front end/back end setup where the back ends are located on each and every users C:\ drive!

      Please, don't look at me like that, I didn't design it.

      The back ends do get sent in to the head office periodically and this is coming up soon so I'm writing a little fixer-upper code for the poor woman who administers this mess.

      Thanks for the code, I was actually getting pretty close. I can cut down one of the loops because I know what table I'm going to look at.

      Jim

      Comment

      • Jim Doherty
        Recognized Expert Contributor
        • Aug 2007
        • 897

        #4
        Originally posted by JustJim
        That will do nicely thanks Jim. "All over the state" means just that, on computers all over the state of Victoria, Australia. It's a front end/back end setup where the back ends are located on each and every users C:\ drive!

        Please, don't look at me like that, I didn't design it.

        The back ends do get sent in to the head office periodically and this is coming up soon so I'm writing a little fixer-upper code for the poor woman who administers this mess.

        Thanks for the code, I was actually getting pretty close. I can cut down one of the loops because I know what table I'm going to look at.

        Jim
        Fine..merely interested kinda knew you'd have no problem overall

        Jim :)

        Comment

        • JustJim
          Recognized Expert Contributor
          • May 2007
          • 407

          #5
          Originally posted by Jim Doherty
          Fine..merely interested kinda knew you'd have no problem overall

          Jim :)
          All installed, debugged and delivered. I get paid next Thursday and I hope it's the last I see of that application.

          How come I never get the jobs where you get to design an application from scratch? You know, all the 1NF, 2NF, 3NF stuff I learned in uni?

          Ah well...

          Jim

          Comment

          • Jim Doherty
            Recognized Expert Contributor
            • Aug 2007
            • 897

            #6
            Originally posted by JustJim
            All installed, debugged and delivered. I get paid next Thursday and I hope it's the last I see of that application.

            How come I never get the jobs where you get to design an application from scratch? You know, all the 1NF, 2NF, 3NF stuff I learned in uni?

            Ah well...

            Jim
            Hahaha yes seems a million miles away I guess

            Comment

            • missinglinq
              Recognized Expert Specialist
              • Nov 2006
              • 3533

              #7
              Originally posted by JustJim
              I get paid next Thursday and I hope it's the last I see of that application.
              Can you say "Not a chance in the world!" JustJim?

              I knew you could!

              Linq ;0)>

              Comment

              • JustJim
                Recognized Expert Contributor
                • May 2007
                • 407

                #8
                Originally posted by missinglinq
                Can you say "Not a chance in the world!" JustJim?

                I knew you could!

                Linq ;0)>
                Not even one tiny little chance in all the world? Sheesh!

                Comment

                Working...