Results 1 to 11 of 11

Thread: [RESOLVED] Update single row

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Resolved [RESOLVED] Update single row

    I am trying to update a single row in an Access DB, with values from an array of 19 text boxes.

    Table name is tbl_Membership
    MembID is the row to be updated

    Is this Update statement along the right lines?

    vb Code:
    1. Set cn = New ADODB.Connection
    2. cn.ConnectionString = strConnection & App.Path & "\Membership.mdb"
    3. cn.Open
    4.    
    5. strSQL = "UPDATE tblMembership Where MembID = " & lngSelection & _
    6.             "Set Firstname = " & txtMembership(0).Text
    7.             "Set Middle_Name = " & txtMembership(1).Text
    8.             'next 17 values inserted here
    9.  
    10. cn.Execute strSQL
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Update single row

    If the membId is unique, you will only affect one row.

    Also, you need to use your connection object to execute it.
    Code:
    cn.Execute strSQL

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Update single row

    Quote Originally Posted by Hack
    If the membId is unique, you will only affect one row.

    Also, you need to use your connection object to execute it.
    Code:
    cn.Execute strSQL
    I edited that post straight away, so you must have been quick posting

    Yes MembID is the primary key in the database, so does that mean I need to use a different method to identify the row to be updated?
    If so, what is the best way to do this?

    As for the SQL statement, is that the correct structure?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Update single row

    An Update statement should look like this:

    Update tableName Set
    ColumnName1 = Value1,
    ColumnName2 = Value2,
    .......
    Where PrimaryKey (or some other field) = Value

    Yous should look like this:

    "UPDATE tblMembership Set "
    "Firstname = " & txtMembership(0).Text & ',"
    "Middle_Name = " & txtMembership(1).Text & "',"
    ....next values
    "Where Where MembID = " & lngSelection
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Update single row

    Quote Originally Posted by aikidokid
    so does that mean I need to use a different method to identify the row to be updated?
    No, you should be fine there
    As for the SQL statement, is that the correct structure?
    No. I'll guess that you have some kind of control array, else the index numbers on txtMembership don't make sense.
    Anyway, you need to surround text values with single quotes in Access. If the SQL is actually on separate lines, you need more & _'s. You also need spaces, commas, etc, along with only one SET statement.
    Code:
    strSQL = "UPDATE tblMembership  " & _
                "SET Firstname = '" & txtMembership(0).Text & "', " & _
                "Middle_Name = '" & txtMembership(1).Text & "', " & _
                'next 17 values inserted here
                "WHERE MembID = " & lngSelection
    Tengo mas preguntas que contestas

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Update single row

    Quote Originally Posted by GaryMazzone
    An Update statement should look like this:

    Update tableName Set
    ColumnName1 = Value1,
    ColumnName2 = Value2,
    .......
    Where PrimaryKey (or some other field) = Value

    Yous should look like this:

    "UPDATE tblMembership Set "
    "Firstname = " & txtMembership(0).Text & ',"
    "Middle_Name = " & txtMembership(1).Text & "',"
    ....next values
    "Where Where MembID = " & lngSelection
    I completely missed the fact that he had the WHERE before the SET.

  7. #7

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Update single row

    Thanks everbody for the help

    I am still getting the error:

    Syntax error in UPDATE statement

    vb Code:
    1. Set cn = New ADODB.Connection
    2.     cn.ConnectionString = strConnection & App.Path & "\Membership.mdb"
    3.     cn.Open
    4.    
    5.     strSQL = "UPDATE tblMembership SET " & _
    6.         "Firstname = '" & txtMembership(0).Text & "', " & _
    7.         "Middle_Name = '" & txtMembership(1).Text & "', " & _
    8.         "Surname = '" & txtMembership(2).Text & "', " & _
    9.         "Street = '" & txtMembership(3).Text & "', " & _
    10.         "Town = '" & txtMembership(4).Text & "', " & _
    11.         "City = '" & txtMembership(5).Text & "', " & _
    12.         "County = '" & txtMembership(6).Text & "', " & _
    13.         "Post_Code = '" & txtMembership(7).Text & "', " & _
    14.         "Home_Tel = '" & txtMembership(8).Text & "', " & _
    15.         "Mobile_Tel = '" & txtMembership(9).Text & "', " & _
    16.         "Email = '" & txtMembership(10).Text & "', " & _
    17.         "DOB = '" & txtMembership(11).Text & "', " & _
    18.         "Date_Joined = '" & txtMembership(12).Text & "', " & _
    19.         "Insurance_Due = '" & txtMembership(13).Text & "', " & _
    20.         "Grade = '" & txtMembership(14).Text & "', " & _
    21.         "Last_Graded = '" & txtMembership(15).Text & "', " & _
    22.         "Membership_Number = '" & txtMembership(16).Text & "', " & _
    23.         "Occupation = '" & txtMembership(17).Text & "', " & _
    24.         "Notes = '" & txtMembership(18).Text & "', " & _
    25.         "Where MembID = " & lngSelection
    26.              
    27.     cn.Execute strSQL
    28.     cn.Close
    29.     Set cn = Nothing

    Do I need to add any properties after the strSQL part of the execute line, for example:

    vb Code:
    1. cn.Execute strSQL , , adcmdText
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Update single row

    There should not be a comma after the last field update.
    Code:
    "Notes = '" & txtMembership(18).Text & "', " &
    'should be
    "Notes = '" & txtMembership(18).Text & "' "

  9. #9

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Update single row

    Thanks Hack.
    I have changed this but now I am getting the error:

    No value given for one or more required parameters

    I can't see what is missing!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Update single row

    That sounds like one of the field names is wrong in the update statement.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Update single row

    Quote Originally Posted by GaryMazzone
    That sounds like one of the field names is wrong in the update statement.
    That was it thanks, I had MembID instead of Memb_ID.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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