PC Review


Reply
Thread Tools Rate Thread

How can I change the column order using VBA

 
 
Balbina
Guest
Posts: n/a
 
      6th Jan 2010
I have written code that appends data to a table. Occasionally, I need to
add more columns. To do this I am using a bubble sort to compare with my
existing table weather i already have the column or not. Because of this, I
make my new table in alphabetical order, but I would like to be able to
reorder the columns into something other than alphabetical. Is there a column
order property for tables? Any suggestions?

Thank you.
 
Reply With Quote
 
 
 
 
Banana
Guest
Posts: n/a
 
      6th Jan 2010
Balbina wrote:
> I have written code that appends data to a table. Occasionally, I need to
> add more columns. To do this I am using a bubble sort to compare with my
> existing table weather i already have the column or not. Because of this, I
> make my new table in alphabetical order, but I would like to be able to
> reorder the columns into something other than alphabetical. Is there a column
> order property for tables? Any suggestions?
>
> Thank you.


I can't help but think the table is not designed correctly. It is not
usual to add a column to a table and it should be designed such that you
add a new row, rather than a column.

May I suggest you google and read up on 'database normalization'? If
you're not sure how to apply to your model, post a brief description of
table and its structure and we can make suggestions how to optimize it.

I know that is not the answer you were asking for, but believe me- going
down this path would be much more painful than doing it right.

HTH.
 
Reply With Quote
 
KARL DEWEY
Guest
Posts: n/a
 
      6th Jan 2010
Following on what Banana wrote, if your database was 'normalized' then a
crosstab query would put the columns in alpha order (or any order you wanted).
--
Build a little, test a little.


"Banana" wrote:

> Balbina wrote:
> > I have written code that appends data to a table. Occasionally, I need to
> > add more columns. To do this I am using a bubble sort to compare with my
> > existing table weather i already have the column or not. Because of this, I
> > make my new table in alphabetical order, but I would like to be able to
> > reorder the columns into something other than alphabetical. Is there a column
> > order property for tables? Any suggestions?
> >
> > Thank you.

>
> I can't help but think the table is not designed correctly. It is not
> usual to add a column to a table and it should be designed such that you
> add a new row, rather than a column.
>
> May I suggest you google and read up on 'database normalization'? If
> you're not sure how to apply to your model, post a brief description of
> table and its structure and we can make suggestions how to optimize it.
>
> I know that is not the answer you were asking for, but believe me- going
> down this path would be much more painful than doing it right.
>
> HTH.
> .
>

 
Reply With Quote
 
Balbina
Guest
Posts: n/a
 
      6th Jan 2010


"KARL DEWEY" wrote:

> Following on what Banana wrote, if your database was 'normalized' then a
> crosstab query would put the columns in alpha order (or any order you wanted).
> --
> Build a little, test a little.
>
>
> "Banana" wrote:
>
> > Balbina wrote:
> > > I have written code that appends data to a table. Occasionally, I need to
> > > add more columns. To do this I am using a bubble sort to compare with my
> > > existing table weather i already have the column or not. Because of this, I
> > > make my new table in alphabetical order, but I would like to be able to
> > > reorder the columns into something other than alphabetical. Is there a column
> > > order property for tables? Any suggestions?
> > >
> > > Thank you.

> >
> > I can't help but think the table is not designed correctly. It is not
> > usual to add a column to a table and it should be designed such that you
> > add a new row, rather than a column.
> >
> > May I suggest you google and read up on 'database normalization'? If
> > you're not sure how to apply to your model, post a brief description of
> > table and its structure and we can make suggestions how to optimize it.
> >
> > I know that is not the answer you were asking for, but believe me- going
> > down this path would be much more painful than doing it right.
> >
> > HTH.
> > .

Okay, let's say I did want to go down the complicated path, is there a
property for column order? What I was really hoping for was that I could get
at it like I can with column type and defined size etc.

I created a data type to hold my column information that I need to modify

Type TempColumns
strColumnName As String
intColumnType As Integer
intColumnAtrib As Integer
intDefinedSize As Integer
End Type

I then write the column info into an array of that data type that I named
typColumns.

Do Until intCounter = intColumns

With typColumns(intCounter)
.strColumnName = tbl.Columns.Item(intCounter)
.intColumnType = tbl.Columns.Item(intCounter).Type
.intColumnAtrib = tbl.Columns.Item(intCounter).Attributes
.intDefinedSize = tbl.Columns.Item(intCounter).DefinedSize
End With
intCounter = intCounter + 1

Loop

Ideally, I would just add another line that would read the original column
position and away I go. Then I could use it as is or change it at my whim. Is
this wishful thinking?

Thank you.
 
Reply With Quote
 
KARL DEWEY
Guest
Posts: n/a
 
      6th Jan 2010
You can use a union query to normalize your data and then the crosstab query.

--
Build a little, test a little.


"Balbina" wrote:

>
>
> "KARL DEWEY" wrote:
>
> > Following on what Banana wrote, if your database was 'normalized' then a
> > crosstab query would put the columns in alpha order (or any order you wanted).
> > --
> > Build a little, test a little.
> >
> >
> > "Banana" wrote:
> >
> > > Balbina wrote:
> > > > I have written code that appends data to a table. Occasionally, I need to
> > > > add more columns. To do this I am using a bubble sort to compare with my
> > > > existing table weather i already have the column or not. Because of this, I
> > > > make my new table in alphabetical order, but I would like to be able to
> > > > reorder the columns into something other than alphabetical. Is there a column
> > > > order property for tables? Any suggestions?
> > > >
> > > > Thank you.
> > >
> > > I can't help but think the table is not designed correctly. It is not
> > > usual to add a column to a table and it should be designed such that you
> > > add a new row, rather than a column.
> > >
> > > May I suggest you google and read up on 'database normalization'? If
> > > you're not sure how to apply to your model, post a brief description of
> > > table and its structure and we can make suggestions how to optimize it.
> > >
> > > I know that is not the answer you were asking for, but believe me- going
> > > down this path would be much more painful than doing it right.
> > >
> > > HTH.
> > > .

> Okay, let's say I did want to go down the complicated path, is there a
> property for column order? What I was really hoping for was that I could get
> at it like I can with column type and defined size etc.
>
> I created a data type to hold my column information that I need to modify
>
> Type TempColumns
> strColumnName As String
> intColumnType As Integer
> intColumnAtrib As Integer
> intDefinedSize As Integer
> End Type
>
> I then write the column info into an array of that data type that I named
> typColumns.
>
> Do Until intCounter = intColumns
>
> With typColumns(intCounter)
> .strColumnName = tbl.Columns.Item(intCounter)
> .intColumnType = tbl.Columns.Item(intCounter).Type
> .intColumnAtrib = tbl.Columns.Item(intCounter).Attributes
> .intDefinedSize = tbl.Columns.Item(intCounter).DefinedSize
> End With
> intCounter = intCounter + 1
>
> Loop
>
> Ideally, I would just add another line that would read the original column
> position and away I go. Then I could use it as is or change it at my whim. Is
> this wishful thinking?
>
> Thank you.

 
Reply With Quote
 
Paul Shapiro
Guest
Posts: n/a
 
      6th Jan 2010
Here's a routine I use when adding a new column to an existing table. Rather
than specifying the field's position directly, I specify the field name I
want it to follow. You could modify the code to do what you want. The DAO
property is field.OrdinalPosition.

Public Function FieldSetOrdinalPosition( _
tblModify As DAO.TableDef, _
strFieldnameToBePositioned As String, _
strFieldnameToPositionAfter As String _
) As Boolean
'In tblModify, move strFieldnameToBePositioned to the position _
immediately following strFieldnameToPositionAfter.
Dim fldNew As DAO.Field
Dim lngOrdinalPositionNew As Long

'Get the ordinal position desired for the field
Set fldNew = tblModify.Fields(strFieldnameToPositionAfter)
lngOrdinalPositionNew = fldNew.OrdinalPosition + 1
Set fldNew = Nothing

'Increment ordinal positions and make space for the newly-assigned field
For Each fldNew In tblModify.Fields
If fldNew.Name = strFieldnameToBePositioned Then
fldNew.OrdinalPosition = lngOrdinalPositionNew
ElseIf fldNew.OrdinalPosition >= lngOrdinalPositionNew Then
fldNew.OrdinalPosition = fldNew.OrdinalPosition + 1
End If
Next


"Balbina" <(E-Mail Removed)> wrote in message
news:45537E97-3343-4CED-9036-(E-Mail Removed)...
>
>
> "KARL DEWEY" wrote:
>
>> Following on what Banana wrote, if your database was 'normalized' then a
>> crosstab query would put the columns in alpha order (or any order you
>> wanted).
>> --
>> Build a little, test a little.
>>
>>
>> "Banana" wrote:
>>
>> > Balbina wrote:
>> > > I have written code that appends data to a table. Occasionally, I
>> > > need to
>> > > add more columns. To do this I am using a bubble sort to compare with
>> > > my
>> > > existing table weather i already have the column or not. Because of
>> > > this, I
>> > > make my new table in alphabetical order, but I would like to be able
>> > > to
>> > > reorder the columns into something other than alphabetical. Is there
>> > > a column
>> > > order property for tables? Any suggestions?
>> > >
>> > > Thank you.
>> >
>> > I can't help but think the table is not designed correctly. It is not
>> > usual to add a column to a table and it should be designed such that
>> > you
>> > add a new row, rather than a column.
>> >
>> > May I suggest you google and read up on 'database normalization'? If
>> > you're not sure how to apply to your model, post a brief description of
>> > table and its structure and we can make suggestions how to optimize it.
>> >
>> > I know that is not the answer you were asking for, but believe me-
>> > going
>> > down this path would be much more painful than doing it right.
>> >
>> > HTH.
>> > .

> Okay, let's say I did want to go down the complicated path, is there a
> property for column order? What I was really hoping for was that I could
> get
> at it like I can with column type and defined size etc.
>
> I created a data type to hold my column information that I need to modify
>
> Type TempColumns
> strColumnName As String
> intColumnType As Integer
> intColumnAtrib As Integer
> intDefinedSize As Integer
> End Type
>
> I then write the column info into an array of that data type that I named
> typColumns.
>
> Do Until intCounter = intColumns
>
> With typColumns(intCounter)
> .strColumnName = tbl.Columns.Item(intCounter)
> .intColumnType = tbl.Columns.Item(intCounter).Type
> .intColumnAtrib = tbl.Columns.Item(intCounter).Attributes
> .intDefinedSize = tbl.Columns.Item(intCounter).DefinedSize
> End With
> intCounter = intCounter + 1
>
> Loop
>
> Ideally, I would just add another line that would read the original column
> position and away I go. Then I could use it as is or change it at my whim.
> Is
> this wishful thinking?
>
> Thank you.


 
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
Change column order in a datatable =?Utf-8?B?RGllZ28gRmRv?= Microsoft C# .NET 0 17th Jul 2007 11:46 PM
how can i change the order(ascending, descending) in a column? =?Utf-8?B?RGFubnk=?= Microsoft Excel Misc 4 23rd Apr 2007 04:54 AM
How do I change the order of the bars in a column chart? =?Utf-8?B?U3R1?= Microsoft Access Reports 1 28th Nov 2005 09:08 PM
change the order of column in a datatable victor Microsoft Dot NET Compact Framework 4 7th Mar 2005 05:17 PM
Change Order of Values in a Pivot Table Column Field =?Utf-8?B?Q2hyaXM=?= Microsoft Excel Worksheet Functions 2 17th Mar 2004 04:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:30 PM.