Results 1 to 5 of 5

Thread: [RESOLVED] Syntax error in update statement?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    17

    Resolved [RESOLVED] Syntax error in update statement?

    Firstly, I am very new to programming and even more so to Visual Basic. So my code here is probably pretty bad. Anyway, I am getting a very strange error.

    This code does not work. The error is shown on line 111:
    VB Code:
    1. For i = 0 To 65000
    2.             If dsgames.Tables("games").Rows(i).Item(0) = draw Then
    3.                 For m = 0 To 4
    4.                     usernums(m) = dsgames.Tables("games").Rows(i).Item(m + 2)
    5.                 Next
    6.                 count = 0
    7.  
    8.                 ' Cycles through each user number, then cycles through each winning number (supp is position 5).
    9.                 ' Checks to see if the user number is equal to the winning number.
    10.                 ' Checks to see if the winning user numbers are supplementary or not.
    11.                 ' Supplementary numbers cause the supplement variable to be set to true.
    12.                 ' Other winning numbers increment a counter.
    13.                 For m = 0 To 4
    14.                     For p = 0 To 5
    15.                         If usernums(m) = winning(p) Then
    16.                             If p = 5 Then
    17.                                 supplement = True
    18.                             Else
    19.                                 count += 1
    20.                             End If
    21.                         End If
    22.                     Next
    23.                 Next
    24.  
    25.                 ' This case statement assigns the current row a division from 0 to 4 based on the number in the counter.
    26.                 ' It also counts the number of winners from each division.
    27.                 Select Case count
    28.                     Case 5
    29.                         dsgames.Tables("games").Rows(i).Item("Div") = 1
    30.                         first += 1
    31.                     Case 4
    32.                         dsgames.Tables("games").Rows(i).Item("Div") = 2
    33.                         second += 1
    34.                     Case 3
    35.                         If supplement = True Then
    36.                             dsgames.Tables("games").Rows(i).Item("Div") = 3
    37.                             third += 1
    38.  
    39.                         Else
    40.                             dsgames.Tables("games").Rows(i).Item("Div") = 4
    41.                             fourth += 1
    42.                         End If
    43.                     Case Else
    44.                         dsgames.Tables("games").Rows(i).Item("Div") = 0
    45.                 End Select
    46.                 supplement = False
    47.             End If
    48.         Next
    49.  
    50.  
    51.  
    52.  
    53.  
    54.         ' The following if statements are used to output the amount of money that each winner from each division receives.
    55.         If first > 0 Then
    56.             firstp = Int(div1 / first)
    57.             lstWinners.Items.Add("Division 1: " & first & " ($" & firstp & " each)")
    58.         Else
    59.             firstp = 0
    60.             lstWinners.Items.Add("Division 1: None")
    61.         End If
    62.  
    63.         If (second > 0) Then
    64.             secondp = Int(div2 / second)
    65.             lstWinners.Items.Add("Division 2: " & second & " ($" & secondp & " each)")
    66.         Else
    67.             secondp = 0
    68.             lstWinners.Items.Add("Division 2: None")
    69.         End If
    70.  
    71.         If (third > 0) Then
    72.             thirdp = Int(div3 / third)
    73.             lstWinners.Items.Add("Division 3: " & third & " ($" & thirdp & " each)")
    74.         Else
    75.             thirdp = 0
    76.             lstWinners.Items.Add("Division 3: None")
    77.         End If
    78.  
    79.         If (fourth > 0) Then
    80.             fourthp = Int(div4 / fourth)
    81.             lstWinners.Items.Add("Division 4: " & fourth & " ($" & fourthp & " each)")
    82.         Else
    83.             fourthp = 0
    84.             lstWinners.Items.Add("Division 4: None")
    85.         End If
    86.  
    87.  
    88.  
    89.         For i = 0 To 65000
    90.             Dim division As Integer = Int(dsgames.Tables("games").Rows(i).Item("Div"))
    91.  
    92.             Select Case division
    93.                 Case 4
    94.                     dsgames.Tables("games").Rows(i).Item("Money") = fourthp
    95.                 Case 3
    96.                     dsgames.Tables("games").Rows(i).Item("Money") = thirdp
    97.                 Case 2
    98.                     dsgames.Tables("games").Rows(i).Item("Money") = secondp
    99.                 Case 1
    100.                     dsgames.Tables("games").Rows(i).Item("Money") = firstp
    101.                 Case Else
    102.                     dsgames.Tables("games").Rows(i).Item("Money") = 0
    103.             End Select
    104.         Next
    105.  
    106.  
    107.         lstWinners.BackColor = Color.White
    108.  
    109.         ' Updates the games table with the new 'Div' values.
    110.         Dim cb As New OleDb.OleDbCommandBuilder(dagames)
    111.         dagames.Update(dsgames, "games") ' THE ERROR IS HERE "Syntax error in UPDATE statement."
    112.  
    113.  
    114.         ' Updates(the) 'Pool' table with the new pool value (after subtracting the winnings).
    115.         Dim cb2 As New OleDb.OleDbCommandBuilder(dapool)
    116.         dspool.Tables("Pool").Rows(0).Item(1) += 1
    117.         dspool.Tables("Pool").Rows(0).Item(0) = dspool.Tables("Pool").Rows(0).Item(0) - firstp - secondp - thirdp - fourthp
    118.         dapool.Update(dspool, "pool")

    This code DOES work. The only difference is that I moved the code from lines 109-111 to 51-53:
    VB Code:
    1. For i = 0 To 65000
    2.             If dsgames.Tables("games").Rows(i).Item(0) = draw Then
    3.                 For m = 0 To 4
    4.                     usernums(m) = dsgames.Tables("games").Rows(i).Item(m + 2)
    5.                 Next
    6.                 count = 0
    7.  
    8.                 ' Cycles through each user number, then cycles through each winning number (supp is position 5).
    9.                 ' Checks to see if the user number is equal to the winning number.
    10.                 ' Checks to see if the winning user numbers are supplementary or not.
    11.                 ' Supplementary numbers cause the supplement variable to be set to true.
    12.                 ' Other winning numbers increment a counter.
    13.                 For m = 0 To 4
    14.                     For p = 0 To 5
    15.                         If usernums(m) = winning(p) Then
    16.                             If p = 5 Then
    17.                                 supplement = True
    18.                             Else
    19.                                 count += 1
    20.                             End If
    21.                         End If
    22.                     Next
    23.                 Next
    24.  
    25.                 ' This case statement assigns the current row a division from 0 to 4 based on the number in the counter.
    26.                 ' It also counts the number of winners from each division.
    27.                 Select Case count
    28.                     Case 5
    29.                         dsgames.Tables("games").Rows(i).Item("Div") = 1
    30.                         first += 1
    31.                     Case 4
    32.                         dsgames.Tables("games").Rows(i).Item("Div") = 2
    33.                         second += 1
    34.                     Case 3
    35.                         If supplement = True Then
    36.                             dsgames.Tables("games").Rows(i).Item("Div") = 3
    37.                             third += 1
    38.  
    39.                         Else
    40.                             dsgames.Tables("games").Rows(i).Item("Div") = 4
    41.                             fourth += 1
    42.                         End If
    43.                     Case Else
    44.                         dsgames.Tables("games").Rows(i).Item("Div") = 0
    45.                 End Select
    46.                 supplement = False
    47.             End If
    48.         Next
    49.  
    50.  
    51.         ' Updates the games table with the new 'Div' values. ' I MOVED IT HERE
    52.         Dim cb As New OleDb.OleDbCommandBuilder(dagames) ' HERE
    53.         dagames.Update(dsgames, "games") ' HERE
    54.  
    55.  
    56.         ' The following if statements are used to output the amount of money that each winner from each division receives.
    57.         If first > 0 Then
    58.             firstp = Int(div1 / first)
    59.             lstWinners.Items.Add("Division 1: " & first & " ($" & firstp & " each)")
    60.         Else
    61.             firstp = 0
    62.             lstWinners.Items.Add("Division 1: None")
    63.         End If
    64.  
    65.         If (second > 0) Then
    66.             secondp = Int(div2 / second)
    67.             lstWinners.Items.Add("Division 2: " & second & " ($" & secondp & " each)")
    68.         Else
    69.             secondp = 0
    70.             lstWinners.Items.Add("Division 2: None")
    71.         End If
    72.  
    73.         If (third > 0) Then
    74.             thirdp = Int(div3 / third)
    75.             lstWinners.Items.Add("Division 3: " & third & " ($" & thirdp & " each)")
    76.         Else
    77.             thirdp = 0
    78.             lstWinners.Items.Add("Division 3: None")
    79.         End If
    80.  
    81.         If (fourth > 0) Then
    82.             fourthp = Int(div4 / fourth)
    83.             lstWinners.Items.Add("Division 4: " & fourth & " ($" & fourthp & " each)")
    84.         Else
    85.             fourthp = 0
    86.             lstWinners.Items.Add("Division 4: None")
    87.         End If
    88.  
    89.  
    90.  
    91.         For i = 0 To 65000
    92.             Dim division As Integer = Int(dsgames.Tables("games").Rows(i).Item("Div"))
    93.  
    94.             Select Case division
    95.                 Case 4
    96.                     dsgames.Tables("games").Rows(i).Item("Money") = fourthp
    97.                 Case 3
    98.                     dsgames.Tables("games").Rows(i).Item("Money") = thirdp
    99.                 Case 2
    100.                     dsgames.Tables("games").Rows(i).Item("Money") = secondp
    101.                 Case 1
    102.                     dsgames.Tables("games").Rows(i).Item("Money") = firstp
    103.                 Case Else
    104.                     dsgames.Tables("games").Rows(i).Item("Money") = 0
    105.             End Select
    106.         Next
    107.  
    108.  
    109.         lstWinners.BackColor = Color.White
    110.  
    111.  
    112.  
    113.  
    114.         ' Updates(the) 'Pool' table with the new pool value (after subtracting the winnings).
    115.         Dim cb2 As New OleDb.OleDbCommandBuilder(dapool)
    116.         dspool.Tables("Pool").Rows(0).Item(1) += 1
    117.         dspool.Tables("Pool").Rows(0).Item(0) = dspool.Tables("Pool").Rows(0).Item(0) - firstp - secondp - thirdp - fourthp
    118.         dapool.Update(dspool, "pool")

    Sorry for the huge walls of code, but they are necessary for me to explain what's happening here. Does anyone know why this is happening and how I can fix it? The code needs to be at the bottom like the first one.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Syntax error in update statement?

    What is the error message reported back?

    Use a Try/Catch to get the error message
    Code:
    Try
        ' statement which is failing
    Catch ex As Exception
        ' this is displayed in the IDE output window
        Console.WriteLine(ex.Message)
    End Try

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    17

    Re: Syntax error in update statement?

    Quote Originally Posted by kevininstructor View Post
    What is the error message reported back?

    Use a Try/Catch to get the error message
    Code:
    Try
        ' statement which is failing
    Catch ex As Exception
        ' this is displayed in the IDE output window
        Console.WriteLine(ex.Message)
    End Try
    A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

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

    Re: Syntax error in update statement?

    Quote Originally Posted by faxmunky View Post
    A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
    That is not the error message. That is telling you that an exception of that type was thrown, not what the Message of the caught exception was. You've already provided the error message in the thread title and, though it's a little harder to find, in your code snippet.

    If you get a syntax error when using a command builder, the reason is usually that one or more of your table and/or column names is a reserved word or contains special characters. To fix that, set the QuotePrefix and QuoteSuffix properties of your command builder so that the identifiers will be delimited appropriately. For Access, any many other databases, the values should be "[" and "]" respectively.
    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Posts
    17

    Re: Syntax error in update statement?

    Quote Originally Posted by jmcilhinney View Post
    That is not the error message. That is telling you that an exception of that type was thrown, not what the Message of the caught exception was. You've already provided the error message in the thread title and, though it's a little harder to find, in your code snippet.

    If you get a syntax error when using a command builder, the reason is usually that one or more of your table and/or column names is a reserved word or contains special characters. To fix that, set the QuotePrefix and QuoteSuffix properties of your command builder so that the identifiers will be delimited appropriately. For Access, any many other databases, the values should be "[" and "]" respectively.
    It turns out 'Money' is a reserved word.
    Thanks for the help!

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