|
-
Jun 29th, 2010, 08:13 PM
#1
Thread Starter
New Member
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()
-
Jun 29th, 2010, 10:26 PM
#2
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|