Results 1 to 5 of 5

Thread: auto increment [resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66

    Resolved auto increment [resolved]

    In my Ms Access Database i have set the "Id" Column to Auto Increment. Now i have the following code:

    VB Code:
    1. Private Sub DataAdd()
    2.         Dim oAdapter As OleDbDataAdapter
    3.         Dim oBuild As OleDbCommandBuilder
    4.         Dim oDR As DataRow
    5.         Dim strSQL As String
    6.         Dim strConn As String
    7.  
    8.         ' Create New DataRow Object From DataSet
    9.         oDR = moDS.Tables("Gegevens").NewRow()
    10.         oDR.BeginEdit()
    11.  
    12.         ' Load new data into row
    13.         'Firts Tab: Persoonlijk
    14.         oDR("Voorletters") = txtVoorletters.Text
    15.         oDR("Tussenvoegsel") = txtTussenvoegsel.Text
    16.         oDR("Achternaam") = txtAchternaam.Text
    17.         oDR("Aanspreeknaam") = txtAanspreeknaam.Text
    18.         oDR("Geslacht") = txtGeslacht.Text
    19.         oDR("Adres") = txtAdres.Text
    20.         oDR("Postcode") = txtPostcode.Text
    21.         oDR("Plaats") = txtPlaats.Text
    22.         oDR("Provincie") = txtProvincie.Text
    23.         If txtGeboortedatum.Text <> "" Then
    24.             oDR("Geboortedatum") = txtGeboortedatum.Text
    25.         End If
    26.         oDR("Notities") = txtNotities.Text
    27.  
    28.         'Second Tab: Verdere Info
    29.         oDR("Telefoon_Prive") = txtTelefoon_Prive.Text
    30.         oDR("Mobiele_Telefoon") = txtTelefoon_Mobiel.Text
    31.         oDR("Telefoon_Werk") = txtTelefoon_Werk.Text
    32.         oDR("Toestelnummer_Werk") = txtToestelnummer.Text
    33.         oDR("Faxnummer") = txtFaxnummer.Text
    34.         oDR("Email_Adres") = txtEmailAdres.Text
    35.  
    36.         'Third Tab: KTC Info
    37.         oDR("Functie") = txtFunctie.Text
    38.         oDR("Soort_Lid") = txtSoortLid.Text
    39.         oDR("Lidnummer") = txtLidNummer.Text
    40.         oDR("Ftk_Nummer") = txtFtkNummer.Text
    41.         If txtLidSinds.Text <> "" Then
    42.             oDR("Lid_Sinds") = txtLidSinds.Text
    43.         End If
    44.         oDR("Kosten_Lid_Methode") = txtBetaalMethode.Text
    45.         oDR("Kosten_Lid") = txtKostenLid.Text
    46.  
    47.         'Fourth Tab: Kilometer Registratie
    48.         oDR("Kilometer_1982") = txt1982.Text
    49.         oDR("Kilometer_1983") = txt1983.Text
    50.         oDR("Kilometer_1984") = txt1984.Text
    51.         oDR("Kilometer_1985") = txt1985.Text
    52.         oDR("Kilometer_1986") = txt1986.Text
    53.         oDR("Kilometer_1987") = txt1987.Text
    54.         oDR("Kilometer_1988") = txt1988.Text
    55.         oDR("Kilometer_1989") = txt1989.Text
    56.         oDR("Kilometer_1990") = txt1990.Text
    57.  
    58.         oDR("Kilometer_1991") = txt1991.Text
    59.         oDR("Kilometer_1992") = txt1992.Text
    60.         oDR("Kilometer_1993") = txt1993.Text
    61.         oDR("Kilometer_1994") = txt1994.Text
    62.         oDR("Kilometer_1995") = txt1995.Text
    63.         oDR("Kilometer_1996") = txt1996.Text
    64.         oDR("Kilometer_1997") = txt1997.Text
    65.         oDR("Kilometer_1998") = txt1998.Text
    66.         oDR("Kilometer_1999") = txt1999.Text
    67.         oDR("Kilometer_2000") = txt2000.Text
    68.  
    69.         oDR("Kilometer_2001") = txt2001.Text
    70.         oDR("Kilometer_2002") = txt2002.Text
    71.         oDR("Kilometer_2003") = txt2003.Text
    72.         oDR("Kilometer_2004") = txt2004.Text
    73.         oDR("Kilometer_2005") = txt2005.Text
    74.         oDR("Kilometer_2006") = txt2006.Text
    75.         oDR("Kilometer_2007") = txt2007.Text
    76.         oDR("Kilometer_2008") = txt2008.Text
    77.         oDR("Kilometer_2009") = txt2009.Text
    78.         oDR("Kilometer_2010") = txt2010.Text
    79.  
    80.         ' Tell DataRow you are done adding data
    81.         oDR.EndEdit()
    82.         ' Add DataRow to DataSet
    83.         moDS.Tables("Gegevens").Rows.Add(oDR)
    84.  
    85.         Try
    86.             ' Get Connection String
    87.             strConn = ConnectionString()
    88.             ' Build SQL String
    89.             strSQL = "SELECT * FROM Gegevens "
    90.             ' Create New DataAdapter
    91.             oAdapter = _
    92.              New OleDbDataAdapter(strSQL, strConn)
    93.             ' Create CommandBuilder for Adapter
    94.             ' This will build INSERT, UPDATE and DELETE SQL
    95.             oBuild = New OleDbCommandBuilder(oAdapter)
    96.  
    97.             ' Get Insert Command Object
    98.             oAdapter.InsertCommand = oBuild.GetInsertCommand()
    99.  
    100.             ' Submit INSERT statement through Adapter
    101.             oAdapter.Update(moDS, "Gegevens")
    102.             ' Tell DataSet changes to data source are complete
    103.             moDS.AcceptChanges()
    104.  
    105.             ' Reload the list box
    106.             ListLoad()
    107.  
    108.         Catch oException As Exception
    109.             MessageBox.Show(oException.Message)
    110.  
    111.         End Try
    112.     End Sub

    This updates the database trough a dataset, but how do auto increment the Id field. If i now run this code it tell me that "Column 'Id' does not allow nulls"

    So i think that i have to do something with an auto increment but how do i do that?
    Last edited by digita; Nov 22nd, 2004 at 08:59 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I just rushed through your code. Set the ID field's value to blank.

    oDR("ID") = ""

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66
    yeah, i also thought that would to the trick, but that didn't work


    gives me the error

    Code:
    Input string was not in a correct format.Couldn't store <> in Id Column.  Expected type is Int32.
    Last edited by digita; Nov 20th, 2004 at 12:04 PM.

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Haven't tried, just a guess here. I also don't know Access, just Sql Server.

    When you have an auto-incrementing column, you just leave out that column name in your insert statement, and the DB takes care of it. Since your sql is "SELECT * FROM Gegevens", of course you're getting the ID field, and when you tell the command builder to make the insert statement for you, it gets included - which you do not want.

    I should mention that it is considered bad practice to use SELECT *, instead, you should select the columns you need.

    So I think you can do two things. One, specify all the columns you need in your select statement (that is, do not include your ID column). That way, the command builder will not include that column in the insert statement it creates. Two, specify your own insert statement, rather than let the command builder create it for you.

    Either way, what you need to do is make sure your insert statement does not include the auto-incrementing ID column name.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66
    Thanks for the tips mike, but it didn't work. I did find a solution. in my dataset i needed to state that that the field id is incremental like this:

    VB Code:
    1. Private Sub DataSetCreate()
    2.         Dim oAdapter As OleDbDataAdapter
    3.         Dim strSQL As String
    4.         Dim strConn As String
    5.  
    6.         ' Get Connection String
    7.         strConn = ConnectionString()
    8.  
    9.         ' Build SQL String
    10.         strSQL = "SELECT *, Achternaam + ', ' + Voorletters AS FullName FROM Gegevens ORDER BY Achternaam ASC"
    11.  
    12.  
    13.         moDS = New DataSet
    14.         Try
    15.             ' Create New Data Adapter
    16.             oAdapter = New OleDbDataAdapter(strSQL, strConn)
    17.             ' Fill DataSet From Adapter and give it a name
    18.             oAdapter.Fill(moDS, "Gegevens")
    19.             ' Create a Primary Key
    20.             With moDS.Tables("Gegevens")
    21.                 .PrimaryKey = New DataColumn() _
    22.                  {.Columns("Id")}
    23.                 .Columns("Id").AutoIncrement = True
    24.                 .Columns("Id").AutoIncrementStep = 1
    25.             End With
    26.  
    27.  
    28.         Catch oExcept As Exception
    29.             MessageBox.Show(oExcept.Message)
    30.  
    31.         End Try
    32.     End Sub

    As you see it was a very simple solution

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