Results 1 to 7 of 7

Thread: [RESOLVED] Having trouble with SQL statement

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Resolved [RESOLVED] Having trouble with SQL statement

    I am geeting an invalid syntax near keyword 'INNER' error on this.

    Code:
    Dim SQL2 As New SqlClient.SqlCommand("SELECT MACHINETOOL[TOOL], MACHINE[NAME] INNER JOIN MACHINETOOL[MACHINE] ON MACHINETOOL[MACHINE] = MACHINE[MACHINE] WHERE LTRIM(RTRIM(MACHINETOOL[TOOL])) = " & "'" & Trim(cboTool.Text) & "'")
    Any ideas?
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

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

    Re: Having trouble with SQL statement

    A couple of problems that I see.

    First there is no FROM clause. You go straight from the Select to Inner Join... that is not valid.
    Second you need a period ( . ) between the table names and the field name in the select , inner join and where clauses

    like this for the SQL:

    sql Code:
    1. SELECT
    2.     MACHINETOOL.[TOOL],
    3.     MACHINE.[NAME]
    4. FROM MACHINETOOL
    5. INNER JOIN MACHINETOOL [MACHINE]
    6.     ON MACHINETOOL.[MACHINE] = MACHINE.[MACHINE]
    7. WHERE LTRIM(RTRIM(MACHINETOOL.[TOOL])) =
    Last edited by GaryMazzone; Dec 15th, 2010 at 12:27 PM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Having trouble with SQL statement

    Thread moved to 'Database Development' forum - which is where you should always post SQL questions (while SQL can be used in VB, it is certainly not specific to VB)

  4. #4

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Having trouble with SQL statement

    So I have this now:
    Code:
                Dim READER2 As SqlDataReader
                Dim SQL2 As New SqlClient.SqlCommand
                SQL2.CommandText = "SELECT MACHINETOOL.[TOOL], MACHINE.[NAME] FROM MACHINETOOL "
                SQL2.CommandText += " INNER JOIN MACHINE ON MACHINE.[MACHINE] = MACHINETOOL.[MACHINE] "
                SQL2.CommandText += " WHERE(LTrim(RTrim(MACHINETOOL.[TOOL])) = " & "'" & Trim(cboTool.Text) & "'"
                SQL2.Connection = Cnxn
                SQL2.CommandType = CommandType.Text
                READER2 = SQL2.ExecuteReader
                Using READER2
                    If READER2.HasRows Then
                        While READER2.Read
                            lstMachine.Items.Add(READER2("machine") & " - " & READER2("Name"))
                        End While
                    Else
                        lstMachine.Items.Add("No machine found")
                    End If
                End Using
                SQL2.Dispose()
                '===========================
    I select a tool (A-106). I get an error " Incorrect syntax near 'A-106' "
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

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

    Re: Having trouble with SQL statement

    Set a breakpoint after you set the SQL Statement. Then See what is in the actual Statement and postit here.

    With a quick look it looks to be missing a final close paren ) after the added text from the combobox
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Having trouble with SQL statement

    You are trying to access READER2("machine"), but there is no column named "machine" in the Reader2.
    Also, a space is required after WHERE in " WHERE(LTrim ..."
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Having trouble with SQL statement

    It is working now. Thanks. Here is the final code. Yes, I needed a ) and I needed to select 'machine' as well. You guys are good; :-)

    Code:
    Dim READER2 As SqlDataReader
                Dim SQL2 As New SqlClient.SqlCommand
                SQL2.CommandText = "SELECT MACHINETOOL.[TOOL], MACHINE.[MACHINE], MACHINE.[NAME] FROM MACHINETOOL "
                SQL2.CommandText += " INNER JOIN MACHINE ON MACHINE.[MACHINE] = MACHINETOOL.[MACHINE] "
                SQL2.CommandText += " WHERE(LTrim(RTrim(MACHINETOOL.[TOOL]))) = " & "'" & Trim(cboTool.Text) & "'"
                SQL2.Connection = Cnxn
                SQL2.CommandType = CommandType.Text
                READER2 = SQL2.ExecuteReader
                Using READER2
                    If READER2.HasRows Then
                        While READER2.Read
                            lstMachine.Items.Add(READER2("machine") & " - " & READER2("Name"))
                        End While
                    Else
                        lstMachine.Items.Add("No machine found")
                    End If
                End Using
                SQL2.Dispose()
                '===========================
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

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