Results 1 to 7 of 7

Thread: [02/03] MS Access Update Statement

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    [02/03] MS Access Update Statement

    I posted this question in the Database section, but that forum is like a ghost town.

    This Update statement stopped working after I added the second and third parameters from the bottom. I get an error message that says there is something wrong with my Update syntax. I checked the db table and everything is ok on that end. Any ideas? I wish Access used T-SQL.

    Code:
    ' CREATE COMMAND AND SET PARAMETERS
            invCmd = New OleDbCommand("UPDATE invMain SET Description2=@desc2, Type=@type, " & _
                "Inventory=@inv, Trigger=@trig, LeadTime=@lead, Size=@size, Cartridge=@cart " & _
                "WHERE (Description1=@part)", invCon)
            invCmd.Parameters.Add("@desc2", OleDb.OleDbType.Char)
            invCmd.Parameters.Add("@type", OleDb.OleDbType.Char)
            invCmd.Parameters.Add("@inv", OleDb.OleDbType.Char)
            invCmd.Parameters.Add("@trig", OleDb.OleDbType.Char)
            invCmd.Parameters.Add("@lead", OleDb.OleDbType.Char)
            invCmd.Parameters.Add("@size", OleDb.OleDbType.Char)
            invCmd.Parameters.Add("@cart", OleDb.OleDbType.Char)
            invCmd.Parameters.Add("@part", OleDb.OleDbType.Char)
            invCmd.Parameters("@desc2").Value = "d2" & Me.uiDescriptionTextBox.Text
            invCmd.Parameters("@type").Value = "ty" & Me.uiTypeTextBox.Text
            invCmd.Parameters("@inv").Value = Me.uiOnHandTextBox.Text
            invCmd.Parameters("@trig").Value = Me.uiTriggerTextBox.Text
            invCmd.Parameters("@lead").Value = Me.uiLeadTextBox.Text
            invCmd.Parameters("@size").Value = Me.uiSizeTextBox.Text
            invCmd.Parameters("@cart").Value = Me.uiCartridgeComboBox.Text
            invCmd.Parameters("@part").Value = "d1" & Me.uiPartNumberLabel.Text
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  2. #2
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: [02/03] MS Access Update Statement

    it looks like its right after your where clause
    See Below:

    "UPDATE invMain SET Description2=@desc2, Type=@type, Inventory=@inv, " &_
    "Trigger=@trig, LeadTime=@lead, Size=@size, Cartridge=@cart " & _
    "WHERE Description1=@part",

    I think that should be right

  3. #3

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [02/03] MS Access Update Statement

    I tried removing the parenthesis in the WHERE clause like you did, but still got the same error.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: [02/03] MS Access Update Statement

    after taking a close look i don't know if i wouldn't just declare variables and set them equal to the textboxes

    VB Code:
    1. dim @description as string = me.txtdescription.text
    2.  
    3. dim sql as string = "update table set description = @description where id = @id"

    i think that should work

  5. #5

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [02/03] MS Access Update Statement

    I tried that too, except when you use variables you have to concatenate them into your string. In your example, @description would throw an exception because it is not identified as a parameter or a value.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] MS Access Update Statement

    When you get a syntax error in what looks like a good SQL statement it is usually because one of your column names is a reserved word. There are several likely candidates in there including Trigger, Size and Type. These are just the sort of words that are likely to have special meaning in a programming language, especially Trigger in SQL. Try putting square brackets around the suspicious column names.

    Also, you're writing considerably more code than is necessary. This:
    VB Code:
    1. invCmd.Parameters.Add("@desc2", OleDb.OleDbType.Char)
    2. invCmd.Parameters("@desc2").Value = "d2" & Me.uiDescriptionTextBox.Text
    can be reduced to this:
    VB Code:
    1. invCmd.Parameters.Add("@desc2", OleDb.OleDbType.Char).Value = "d2" & Me.uiDescriptionTextBox.Text
    because Add returns a reference to the parameter just added. Having said that, there's no need to specify the parameter type because it will implicitly take on the appropriate type for the value you specify. As you're specifying a string for each Value and each parameter is a text type there's no need to specify a type. Your code then reduces further to this:
    VB Code:
    1. invCmd.Parameters.Add("@desc2", "d2" & Me.uiDescriptionTextBox.Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [02/03] MS Access Update Statement

    Thanks JM. I'll give this a shot when I get back in the office. I appreciate the optimized code example.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

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