Results 1 to 27 of 27

Thread: SQL Statement - UPDATE .... WHERE ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    SQL Statement - UPDATE .... WHERE ?

    Oki, so I have this Update Statement.

    Code:
    UPDATE Classes SET [Class Name] = @ClassName, [Class Code] = @ClassCode, [Class Room] = @ClassRoom, [Grade] = @Grade, [Teacher] = @Teacher
    Now, If I add WHERE ID = @ID to the END. And then add Parameters for it, like I did for the rest of the Columns. I get an Error.

    Code:
                    'oAdapter.UpdateCommand.Parameters.Add("@ID", OleDbType.Integer, 5, "ID")
                    oAdapter.UpdateCommand.Parameters.AddWithValue("@ClassName", .txtClassName.Text)
                    oAdapter.UpdateCommand.Parameters.AddWithValue("@ClassCode", .txtClassCode.Text)
                    oAdapter.UpdateCommand.Parameters.AddWithValue("@ClassRoom", .txtClassRoomNum.Text)
                    oAdapter.UpdateCommand.Parameters.AddWithValue("@Grade", .txtGrade.Text)
                    oAdapter.UpdateCommand.Parameters.AddWithValue("@Teacher", .txtTeacher.Text)

    So, why do I need the WHERE in the Update Statment, and how would I get it to work with this code:

    vb Code:
    1. Public Sub SaveMainDatabaseForEdit()
    2.         oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    3.         SQL = "UPDATE Classes SET [Class Name] = @ClassName, [Class Code] = @ClassCode, [Class Room] = @ClassRoom, [Grade] = @Grade, [Teacher] = @Teacher"
    4.         Using oCommand As New OleDbCommand(SQL, oConnect)
    5.             With frmEditClasses
    6.                 Dim oAdapter As New OleDbDataAdapter
    7.                 oAdapter.UpdateCommand = oCommand
    8.                 oConnect.Open()
    9.                 'oAdapter.UpdateCommand.Parameters.Add("@ID", OleDbType.Integer, 5, "ID")
    10.                 oAdapter.UpdateCommand.Parameters.AddWithValue("@ClassName", .txtClassName.Text)
    11.                 oAdapter.UpdateCommand.Parameters.AddWithValue("@ClassCode", .txtClassCode.Text)
    12.                 oAdapter.UpdateCommand.Parameters.AddWithValue("@ClassRoom", .txtClassRoomNum.Text)
    13.                 oAdapter.UpdateCommand.Parameters.AddWithValue("@Grade", .txtGrade.Text)
    14.                 oAdapter.UpdateCommand.Parameters.AddWithValue("@Teacher", .txtTeacher.Text)
    15.                 Dim DR As DataRow
    16.                 DR = oDataSet.Tables(0).Rows(.icmbClassesIndex)
    17.                 DR.BeginEdit()
    18.                 DR("Class Name") = .txtClassName.Text
    19.                 DR("Class Code") = .txtClassCode.Text
    20.                 DR("Class Room") = .txtClassRoomNum.Text
    21.                 DR("Grade") = .txtGrade.Text
    22.                 DR("Teacher") = .txtTeacher.Text
    23.                 DR.EndEdit()
    24.                 oAdapter.Update(oDataSet)
    25.                 oDataSet.AcceptChanges()
    26.             End With
    27.         End Using
    28.     End Sub

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: SQL Statement - UPDATE .... WHERE ?

    Since you are using access, you need to add the parameters in the order in which they appear in the query. Access really doesn't support named parameters.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Ok, so now How do I fix this error:

    "Parameter @ClassName has no default value."

    with this code:

    vb Code:
    1. Public Sub SaveMainDatabaseForEdit()
    2.         oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    3.         SQL = "UPDATE Classes SET [Class Name] = @ClassName, [Class Code] = @ClassCode, [Class Room] = @ClassRoom, [Grade] = @Grade, [Teacher] = @Teacher WHERE ID = @ID"
    4.         Using oCommand As New OleDbCommand(SQL, oConnect)
    5.             With frmEditClasses
    6.                 Dim oAdapter As New OleDbDataAdapter
    7.                 oAdapter.UpdateCommand = oCommand
    8.                 oConnect.Open()
    9.                 oAdapter.UpdateCommand.Parameters.Add("@ID", OleDbType.Integer, 5, "[ID]")
    10.                 oAdapter.UpdateCommand.Parameters.Add("@ClassName", OleDbType.VarChar, 50, "[Class Name]")
    11.                 oAdapter.UpdateCommand.Parameters.Add("@ClassCode", OleDbType.VarChar, 20, "[Class Code]")
    12.                 oAdapter.UpdateCommand.Parameters.Add("@ClassRoom", OleDbType.VarChar, 20, "[Class Room]")
    13.                 oAdapter.UpdateCommand.Parameters.Add("@Grade", OleDbType.VarChar, 10, "[Grade]")
    14.                 oAdapter.UpdateCommand.Parameters.Add("@Teacher", OleDbType.VarChar, 50, "[Teacher]")
    15.                 Dim DR As DataRow
    16.                 DR = oDataSet.Tables(0).Rows(.icmbClassesIndex)
    17.                 DR.BeginEdit()
    18.                 DR("Class Name") = "AB"
    19.                 DR("Class Code") = .txtClassCode.Text
    20.                 DR("Class Room") = .txtClassRoomNum.Text
    21.                 DR("Grade") = .txtGrade.Text
    22.                 DR("Teacher") = .txtTeacher.Text
    23.                 DR.EndEdit()
    24.                 oAdapter.Update(oDataSet)
    25.                 oDataSet.AcceptChanges()
    26.             End With
    27.         End Using
    28.     End Sub

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: SQL Statement - UPDATE .... WHERE ?

    Move this line:

    Code:
    oAdapter.UpdateCommand.Parameters.Add("@ID", OleDbType.Integer, 5, "[ID]")
    To the bottom of the parameters.add list.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Hmm... I did that, but it still coming up with the exact same error.

    EDIT: Now, it comes up with ID as no default value.

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

    Re: SQL Statement - UPDATE .... WHERE ?

    Quote Originally Posted by Wesley008 View Post
    So, why do I need the WHERE in the Update Statment
    Because if you don't have a WHERE clause every single record in your database will be affected by your UPDATE. That is sometimes the desired end result, but typically only certain records should be updated at any one time.

    I'm not sure what is going on with all of this Adapter stuff. Being the simpleton that I am, I usually go with something basic like this.
    vb.net Code:
    1. Imports System.Data.OleDb
    2.  
    3. Public Class Form1
    4.  
    5.     Private connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sConnectionString & "Classes.mdb;"
    6.     Private conn As OleDb.OleDbConnection
    7.  
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         conn = New OleDbConnection(connstring)
    10.         conn.Open()
    11.     End Sub
    12.  
    13.  
    14. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    15. Dim SQL As String = String.Empty
    16.  
    17. SQL = "UPDATE Classes SET [Class Name] = @ClassName, "
    18. SQL = SQL & "[Class Code] = @ClassCode, "
    19. SQL = SQL & "[Class Room] = @ClassRoom, "
    20. SQL = SQL & "[Grade] = @Grade, "
    21. SQL = SQL & "[Teacher] = @Teacher "
    22. SQL = SQL & "WHERE ID = @ID "
    23.  
    24. Dim command As New OleDBCommand(SQL, conn)
    25.  With command.Parameters
    26.      .AddWithValue("@ClassName", txtClassName.Text)
    27.      .AddWithValue("@ClassCode", txtClassCode.Text)
    28.      .AddWithValue("@ClassRoom", txtClassRoomNum.Text)
    29.      .AddWithValue("@Grade", txtGrade.Text)
    30.      .AddWithValue("@Teacher", txtTeacher.Text)
    31.      .AddWithValue("@ID",5)
    32.  End With
    33.  command.ExecuteNonQuery()
    34. End Sub
    35. End Class
    Does that work for you?
    Last edited by Hack; Mar 31st, 2009 at 07:02 AM.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    vb Code:
    1. oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    2.         SQL = "UPDATE Classes SET [Class Name] = @ClassName, [Class Code] = @ClassCode, [Class Room] = @ClassRoom, [Grade] = @Grade, [Teacher] = @Teacher WHERE ID = @ID"
    3.         Dim oCommand As New OleDbCommand(SQL, oConnect)
    4.         oConnect.Open()
    5.         With frmEditClasses
    6.             oCommand.Parameters.AddWithValue("@ClassName", .txtClassName.Text)
    7.             oCommand.Parameters.AddWithValue("@ClassCode", .txtClassCode.Text)
    8.             oCommand.Parameters.AddWithValue("@ClassRoom", .txtClassRoomNum.Text)
    9.             oCommand.Parameters.AddWithValue("@Grade", .txtGrade.Text)
    10.             oCommand.Parameters.AddWithValue("@Teacher", .txtTeacher.Text)
    11.             oCommand.Parameters.AddWithValue("@ID", 5)
    12.             oCommand.ExecuteNonQuery()
    13.             MsgBox(oConnect.ConnectionString)
    14.         End With

    Call me blind, but I did exactly what you said, and it still didn't work. I didn't recive no error message, but nothing happens. My Database doesn't get changed.

    EDIT: ID is a COUNTER in the SQL Statment, it will Increment every new Row Created( dunno if that makes a difference)

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

    Re: SQL Statement - UPDATE .... WHERE ?

    ID MUST match a value in your database or the query won't work.

    If this is not what should happen, then ID must be removed from your WHERE clause and something else used to determine what record receives the update.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Wow, I feel n00b. Thanks so much, I just made ID the Combo Box Selected Index + 1 - so if I'm on the First Row, my Index would be 0 + 1 so it would edit the first Row.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    Umm... that's not a good idea... it will work as long as you NEVER delete any data....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Ok, so then how do I fix this issue?

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    beats me... depends on how you are loading the data into the list box... but what I'd do is add the item to the list, then set the ItemData to the ID that corresponds with it... then use the value in the itemdata to set as the ID when updating.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Ok, so whats this ItemData? Can I get an Example?

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    Oh... wait... crap... for some reason I was thinking this was VB 6... it's not... it's .NET... even better. three lines of code... more or less.

    If you post the code you are using to get the data and populate the combo box, I'll see if I can tweak it to get what you need in a fairly easy and painless manner.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    vb Code:
    1. Public Sub SaveMainDatabaseForEdit()
    2.         SQL = "SELECT [ID] FROM Classes"
    3.         Dim oAdapter As New OleDbDataAdapter(SQL, oConnect)
    4.         oAdapter.Fill(oDataSet)
    5.         For Each DataRow As DataRow In oDataSet.Tables(0).Rows
    6.             MsgBox(DataRow(5).ToString)
    7.         Next
    8.         oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    9.         SQL = "UPDATE Classes SET [Class Name] = @ClassName, [Class Code] = @ClassCode, [Class Room] = @ClassRoom, [Grade] = @Grade, [Teacher] = @Teacher WHERE ID = @ID"
    10.         Using oCommand As New OleDbCommand(SQL, oConnect)
    11.             With frmEditClasses
    12.                 oCommand.Parameters.AddWithValue("@ClassName", .txtClassName.Text)
    13.                 oCommand.Parameters.AddWithValue("@ClassCode", .txtClassCode.Text)
    14.                 oCommand.Parameters.AddWithValue("@ClassRoom", .txtClassRoomNum.Text)
    15.                 oCommand.Parameters.AddWithValue("@Grade", .txtGrade.Text)
    16.                 oCommand.Parameters.AddWithValue("@Teacher", .txtTeacher.Text)
    17.                 oCommand.Parameters.AddWithValue("@ID", .icmbClassesIndex + 1)
    18.                 oConnect.Open()
    19.                 oCommand.ExecuteNonQuery()
    20.             End With
    21.         End Using
    22.     End Sub

    That code will Edit & Save to the Row the ComboBox Selected Index is selected.

    It's set to a variable and used here:

    vb Code:
    1. oCommand.Parameters.AddWithValue("@ID", .icmbClassesIndex + 1)




    ------

    I've been trying this way:

    vb Code:
    1. Public Sub RefillIDColumn()
    2.         oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    3.         SQL = "SELECT [ID] FROM Classes"
    4.         Using oAdapter As New OleDbDataAdapter(SQL, oConnect)
    5.             oConnect.Open()
    6.             oAdapter.Fill(oDataSet)
    7.             Dim i2 As Integer
    8.             For Each i As DataRow In oDataSet.Tables(0).Rows
    9.                 SQL = "INSERT INTO CLASSES ([ID]) VALUES (@ID)"
    10.                 Using oCommand As New OleDbCommand(SQL, oConnect)
    11.                     oCommand.Parameters.AddWithValue("@ID", i2)
    12.                     oCommand.ExecuteNonQuery()
    13.                 End Using
    14.                 '                MsgBox(oDataSet.Tables(0).Rows(i2).Item(0).ToString)
    15.                 i2 += 1
    16.             Next
    17.         End Using
    18.     End Sub

    What that is doing is just completly rewriting the ID Column.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    NVM, That method wont work, I still need something in the UPDATE .... WHERE area.

  17. #17
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    I was looking for the code where you LOAD the combo box.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    vb Code:
    1. Public Function FillComboBox(ByVal winForm As Form, ByVal cmbName As ComboBox) As Boolean
    2.         oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    3.         SQL = "SELECT [Class Name], [Class Code] FROM Classes"
    4.         Using oCommand As New OleDbCommand(SQL, oConnect)
    5.             oConnect.Open()
    6.             oReader = oCommand.ExecuteReader
    7.             With winForm
    8.                 cmbName.Items.Clear()
    9.                 While oReader.Read
    10.                     cmbName.Items.Add("[" & oReader(1) & "] " & oReader(0))
    11.                 End While
    12.             End With
    13.         End Using
    14.     End Function


    I made a Function because I call it twice in 2 different forms.

  19. #19
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    There's no reason to make it a function, you aren't returning anything. Also, there's no reason to pass in the form... just the combo box. You're using the With but then not taking advantage of it... see if this works for you:

    Code:
    	Public Sub FillComboBox(ByVal cmbName As ComboBox)
    		Dim dtData As New DataTable
    
    		oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    		Sql = "SELECT [Class Name], [Class Code] FROM Classes"
    		Using oCommand As New OleDbCommand(Sql, oConnect)
    			oConnect.Open()
    			oreader = oCommand.ExecuteReader
    
    			dtData.Load(oreader)
    			cmbName.DataSource = dtData
    			cmbName.DisplayMember = "fieldname to display"
    			cmbName.ValueMember = "fieldnname to use as value"
    		End Using
    	End Sub
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    How is that going help me Edit a Row if I start Deleting Rows how am I suppose to reference back to the Row? Since the ID Field is going to be Different.
    Last edited by Wesley008; Apr 1st, 2009 at 01:21 PM.

  21. #21
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    The IDs come from the database, right?
    So if I have this in the table:
    ID ... Name
    1 ..... Wesley008
    2 ..... TechGnome

    And I load it into the combo box like I showed... when you select your name, the selectedvalue property of the combo box will be 1. You can then use that ID to load your info, edit it, and update the database.

    If you then add a new user, say si_th_geek, then you'll end up with this:

    ID ... Name
    1 ..... Wesley008
    2 ..... TechGnome
    3 ..... si_the_geek

    If you were then to delete me, you get this:

    ID ... Name
    1 ..... Wesley008
    3 ..... si_the_geek

    And now, let's add Hack and mendhack:

    ID ... Name
    1 ..... Wesley008
    3 ..... si_the_geek
    4 ..... hack
    5 ..... mendhack

    See? Even though 2 was removed, as long as you use the ID returned by the DB, you'll always be able to get back to the row. (until it too is deleted from the database, but then after that, it wouldn't be available to load back into the combo box anyways.)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Can you please explain this:

    cmbName.DisplayMember = "[Class Name]"
    cmbName.ValueMember = "[ID]"


    a bit more? Like what do I put their? I've never used that method before.

  23. #23
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    I did show where to put them... look at the FillCombo method I posted... it's in there.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Ok, so I got that, but when I'm setting up my parameters to Save the Edited Verison, Do I reference back to the cmbName.ValueMember? Like this?

    vb Code:
    1. Public Sub SaveMainDatabaseForEdit()
    2.         oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    3.         SQL = "UPDATE Classes SET [Class Name] = @ClassName, [Class Code] = @ClassCode, [Class Room] = @ClassRoom, [Grade] = @Grade, [Teacher] = @Teacher WHERE ID = @ID"
    4.         Using oCommand As New OleDbCommand(SQL, oConnect)
    5.             With frmEditClasses
    6.                 oCommand.Parameters.AddWithValue("@ClassName", .txtClassName.Text)
    7.                 oCommand.Parameters.AddWithValue("@ClassCode", .txtClassCode.Text)
    8.                 oCommand.Parameters.AddWithValue("@ClassRoom", .txtClassRoomNum.Text)
    9.                 oCommand.Parameters.AddWithValue("@Grade", .txtGrade.Text)
    10.                 oCommand.Parameters.AddWithValue("@Teacher", .txtTeacher.Text)
    11.                 oCommand.Parameters.AddWithValue("@ID", .cmbClasses.ValueMember)
    12.                 oConnect.Open()
    13.                 oCommand.ExecuteNonQuery()
    14.             End With
    15.         End Using
    16.     End Sub

  25. #25
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    Almost...
    AddWithValue("@ID", .cmbClasses.ValueMember)

    Should be:
    AddWithValue("@ID", .cmbClasses.SelectedValue)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: SQL Statement - UPDATE .... WHERE ?

    Thanks!

    It kinda works, I'm still running into a big issue.

    When I add say 2 Items, and I edit one of them, It's editing all of them.

    You have the code used for Saving the Edited Verison above.

    And heres how I retrive it to fill into text boxes just in case you need it.

    vb Code:
    1. Public Sub RetriveMainDatabaseForEdit()
    2.         oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
    3.         SQL = "SELECT [Class Name], [Class Code], [Class Room], [Grade], [Teacher] FROM Classes"
    4.         Using oAdapter As New OleDbDataAdapter(SQL, oConnect)
    5.             oConnect.Open()
    6.             oAdapter.Fill(oDataSet)
    7.             With frmEditClasses
    8.                 .txtClassName.Text = oDataSet.Tables(0).Rows(.icmbClassesIndex).Item("Class Name")
    9.                 .txtClassCode.Text = oDataSet.Tables(0).Rows(.icmbClassesIndex).Item("Class Code")
    10.                 .txtClassRoomNum.Text = oDataSet.Tables(0).Rows(.icmbClassesIndex).Item("Class Room")
    11.                 .txtGrade.Text = oDataSet.Tables(0).Rows(.icmbClassesIndex).Item("Grade")
    12.                 .txtTeacher.Text = oDataSet.Tables(0).Rows(.cmbClasses.SelectedIndex).Item("Teacher")
    13.             End With
    14.         End Using
    15.     End Sub

    BTW. Thank you very much this has been great. And also, your website, make a request for a song, doesn't work (atleast in FF)


    EDIT:
    Am I going have to change all the DisplayValue and STuff for the Text Boxes like I did with the Combo Box? or is it something different. Its not the display issue I'm having, its just that. anything I edit get's changed for all the data.

  27. #27
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement - UPDATE .... WHERE ?

    Looks like the sql is off... it's pulling all of the items in the database, then you're only displaying the first item in the table.

    Might want to add a where clause similar to the one in your update. That way, you pull in the one you actually selected. Also your use of a dataset is using a hammer to kill a fly... it's overkill and there's no real reason to do so. If you use a datatable instead, then you can le .... OH.... wait ... hold on.... I see what you've done. Ahh.... OK ... yeah.. there's a better way.

    Try this:
    Code:
          Public Sub RetriveMainDatabaseForEdit()
                  oConnect = New OleDbConnection(sConnectionString & "Classes.mdb")
                  SQL = "SELECT [Class Name], [Class Code], [Class Room], [Grade], [Teacher] FROM Classes WHERE ID = " & frmEditClasses.cmbClasses.SelectedValue
                  'You might get some flak for using a concatenated string for the SQL (I'm usually the one to give the flak) but for simplicity, roll with it
    
                  Using oAdapter As New OleDbDataAdapter(SQL, oConnect)
                      oConnect.Open()
                      oAdapter.Fill(oDataTable) ' Use a datatable here... it's smaller, faster and easier for your needs
                      dim oDataRow As DataRow = oDataTable.Rows(0) 'Get the first and only row
                      With frmEditClasses
                          .txtClassName.Text = oDataRow("Class Name").Tostring
                          .txtClassCode.Text = oDataRow("Class Code").Tostring
                          .txtClassRoomNum.Text = oDataRow("Class Room").Tostring
                          .txtGrade.Text = oDataRow("Grade").Tostring
                          .txtTeacher.Text = oDataRow("Teacher").Tostring
                      End With
                  End Using
              End Sub

    I'm not able to test it, but that should be close to what you need. Basically, rather than loading the entire table, only load the row you need.

    -tg

    edit - thanks, but it does work... but you have to register first... which I don't suggest jsut yet as it's still a work in progress and I plan to flush the users table eventually... right now it's full of people who have tried to spam the place.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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