Adding a New Column in MS Access within VB

databases Egypt
  • 10 years ago

    The system I am working on involves sales transaction and I want it to show not only the total payments but also the smaller payments applied that made up the total. My solution would be that I'll make three columns, the first is the primary/reference key, the second column would be where the total payments would be saved, and the third woulld be where would I save the smaller payments. I would like the third column to be replicated (the added column should have the same properties as the third), although I'm not sure if adding a column can be done. I know the code Data.Recordset.Addnew adds a new row, but I don't know a code for adding a column.

     What could be the best solution for this?

  • 10 years ago

    Which version of VB are you using?

    I appreciate that this is C#, but it may help you:

    string commandString = "ALTER TABLE " + TableName + " ADD COLUMN " + FieldName + " " + Type;

    OleDbConnection Conn = new OleDbConnection( your connection );

    Conn.Open();

    OleDbCommand Command = new OleDbCommand(commandString, Conn);

    Command.ExecuteNonQuery();

    Where TableName, FieldName and Type are all strings.  More about Type can be found here:

     http://msdn.microsoft.com/en-us/library/ms714540(VS.85).aspx

    You may like to add an exception handler around the .ExecuteNonQuery and handle it accordingly.

    HTH

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon