Results 1 to 2 of 2

Thread: Adding a Column to an Access Database

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    2

    Adding a Column to an Access Database

    My program has an access database with a table called "Contacts". This table stores many values. I have a seperate table that stores surveys. Each contact should have a column for every survey, indicating whether or not the survey has been completed. However, this means I need to add columns to the database while the program is running. I tried the following code, which runs WITHOUT errors, but does not seem to actually add the column. Could someone please tell me what I am doing wrong?

    Code:
    contactsDataAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM Contacts", frmAdmin.con)
        contactsCommandBuilder = New OleDb.OleDbCommandBuilder(contactsDataAdapter)
        dtContacts.Clear()
        contactsDataAdapter.Fill(dtContacts)
        drContacts = dtContacts.Rows(0)
        Dim intRowPosition = 0
    
        surveysDataAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM Surveys", frmAdmin.con)
        surveysCommandBuilder = New OleDb.OleDbCommandBuilder(surveysDataAdapter)
        dtSurveys.Clear()
        surveysDataAdapter.Fill(dtSurveys)
        Dim intRowPosition2 = 0
    
        frmAdmin.con.Open()
    
        Dim Survey As DataColumn = New DataColumn(txtName.Text)
        'declaring a column named Survey
        Survey.DataType = System.Type.GetType("System.Boolean")
        'setting the datatype for the column
        dtContacts.Columns.Add(Survey)
        'adding the column to table
    
        dtContacts.AcceptChanges()
        contactsDataAdapter.Update(dtContacts)
    
        While dtContacts.Rows.Count <> intRowPosition
          drContacts = dtContacts.Rows(intRowPosition)
          drContacts(txtName.Text) = False
          intRowPosition += 1
        End While
    
        Dim temp As Integer
        drSurveys = dtSurveys.NewRow()
        drSurveys.Item("ID") = txtName.Text
        temp = txtGold.Text
        drSurveys.Item("GoldReward") = temp
        temp = txtExperience.Text
        drSurveys.Item("ExperienceReward") = temp
        drSurveys.Item("Expiration") = dtpExpiration.Value.Date
    
        dtSurveys.Rows.Add(drSurveys)
    
        contactsDataAdapter.Update(dtContacts)
        surveysDataAdapter.Update(dtSurveys)
    
        frmAdmin.con.Close()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding a Column to an Access Database

    You're going about things the wrong way. You never add indeterminate numbers of columns like that. You only ever add rows, not columns. If you need to be able to relate every Contact to every Survey then you need to add a new table. It will have at least two columns: one for ContactID and one for SurveyID. Whenever you add a new Contact you will add a new row to that table for each Survey and that Contact. Whenever you add a new Survey you add a new row to that table for each Contact and that Survey.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width