Results 1 to 5 of 5

Thread: auto increment [resolved]

Threaded View

  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.

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