Solved

error 102

Posted on 2013-10-30
4
301 Views
Last Modified: 2013-10-31
Problem using Execute Reader
in this part of the program I want to see if there are similar names to  (vemp)  in a table.
 
Get Error number 102  " incorrect syntax near '%'  

vemp = InputBox("Digit Company name", "")
        If vemp.ToString <> "" Then

            Dim sSql As String
            Dim objConn As SqlConnection
            Dim objCmd As SqlCommand
            Dim DR As SqlDataReader
            Dim strConn As String = "server=.\SqlExpress; database=mala2; Trusted_Connection=yes"
            objConn = New SqlConnection(strConn)
            sSql = "select * from md1 where empresa like " & vemp & "%"
            objCmd = New SqlCommand(sSql, objConn)
            Try
                objConn.Open()
            Catch exc As Exception
                MessageBox.Show("Error in opening the  connection")
                Exit Sub
            End Try

ok up to here

            Try
                DR = objCmd.ExecuteReader
            Catch exc As SqlException
                Dim erro As String
                error = exc.Number.ToString + " - " + exc. Message
                MessageBox.Show(error)
                Exit Sub
            End Try

            If DR.HasRows Then  ' there are similar names, show table
                Me.MD1_CombTableAdapter.FillBy1(Me.Mala2DataSet.MD1_Comb, vemp.ToString)
                If MsgBox("Please see similar existing names, want to continue?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                    Exit Sub
                Else
            .........
                End If

                 .........
            Else
                ........

            End If
            DR.Close()
0
Comment
Question by:dovk179
  • 2
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39611332
Assuming that empresa is a string, you need to pass single quotes around the vemp variable and wildcard %, like...

sSql = "select * from md1 where empresa like '" & vemp & "%'"

Open in new window

0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 39611339
You missed the single quotes around the variable

sSql = "select * from md1 where empresa like '" & vemp & "%'"

When debugging these things, output the sSql string to the screen and then copy and paste it into SQL to test it.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39612626
dovk179 - Curiosity overwhelms me ... did you try my solution?
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39613542
Wow yeah. jimhorn kinda beat me to it by a few minutes. He should get the points, and to be honest the points should be a grade A as that solved your problem completely with example code.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question