Results 1 to 7 of 7

Thread: What is wrong with my SQL string?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    8

    What is wrong with my SQL string?

    I get a syntax error while trying to update a datatable using this syntax string:
    "SELECT * FROM ClientContacts WHERE MembershipID = '" & stringVariable & "' "

    I can't figure out what is wrong with it

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: What is wrong with my SQL string?

    There doesn't seem to be anything wrong with the query string, at least not if your ClientContacts table has a MembershipID. So what is the syntax error you're getting? Maybe its the way you use it, can you post some more of your code please.

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

    Re: What is wrong with my SQL string?

    If that SQL code is indeed the issue then the only explanation is that there's something amiss with the value you're inserting into it. For that reason (and others) you should never use string concatenation to build SQL strings. You should always use parameters. For more information, follow the last link in my signature.
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    8

    Re: What is wrong with my SQL string?

    The error is:

    System.Data.OleDb.OleDbException was unhandled
    Message="Syntax error (missing operator) in query expression '((MembershipID = ?) AND ((? = 1 AND First Name IS NULL) OR (First Name = ?)) AND ((? = 1 AND Last Name IS NULL) OR (Last Name = ?)) AND ((? = 1 AND Phone IS NULL) OR (Phone = ?)) AND ((? = 1 AND Street IS NULL) OR (Street = ?)) AND ((? = 1 AND City IS NUL'."
    Source="Microsoft Office Access Database Engine"
    ErrorCode=-2147217900

    My button event saves changes made to a datagridview
    dt and connStr are declared properly at a class level and I get no errors or exceptions from the Visual Studio.

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim search = Form2.txtID.Text.ToUpper
    Dim changes As Integer

    'Open a connection to the database for updating

    Dim dataAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM ClientContacts WHERE MembershipID = '" & search & "' ", connStr)
    Dim commandBuilder As New OleDb.OleDbCommandBuilder(dataAdapter)

    'Update the database with changes from the data table

    changes = dataAdapter.Update(dt)
    dataAdapter.Dispose()
    'Display the number of changes made
    If changes > 0 Then
    MessageBox.Show(changes & " changed rows were stored in the database.")
    Else
    MessageBox.Show("No changes made.")
    End If
    End Sub

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

    Re: What is wrong with my SQL string?

    Sigh... the problem is your field names.... you can't (or more accurately SHOULDN'T) use spaces in the middle of field names... "first name" should be "firstname" .... that's what the problem is...

    solve it one of two ways: 1) change your field names so that they don't have spaces in them. 2) in your select, EXPLICITLY specify your fields, and fields with spaces, surround them with square brackets [First Name]

    -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??? *

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

    Re: What is wrong with my SQL string?

    OK, so you're not getting a syntax error in the SQL you posted at all. This is why it's important to provide a FULL and CLEAR description of EXACTLY what's happening, which includes the code you're using, the error message and EXACTLY where the error occurs.

    The problem is that the command builder is basing the SQL code it generates off your query and some of your column names contain spaces. The best solution is to rename those columns by removing the spaces and NEVER use spaces in column names again, e.g. 'Last Name' becomes 'LastName'. If that's not possible for some reason then you have two choices:

    1. Don't use a wildcard for the column list in your query. Enter the name of each column explicitly, in which case you'll be forced to escape those troublesome names, i.e. wrap them in brackets. The command builder will then follow your lead.

    2. Don't use a command builder. Create your own DELETE, INSERT and/or UPDATE statements and escape the column names as required.
    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
    New Member
    Join Date
    Aug 2010
    Posts
    8

    Re: What is wrong with my SQL string?

    Thanks so much guys, I figured it out right as I refreshed the page and saw your comments.

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