Results 1 to 30 of 30

Thread: Help Resolving Syntax Error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Help Resolving Syntax Error

    Hey All,

    Need some help resolving a Syntax error as title says, been a long day pretty sure it's a simple fix but i can't find the error.

    Code:
    If bStudentNumberExists Then
    
    
                sSaveSql = "update CUSTOMER set " _
                & "Surname = '" & oThisFormA.txtCusSurname.Text.Trim & "'," _
                & "Given = '" & oThisFormA.txtCustGiven.Text.Trim & "'," _
                & "DateOfBirth = '" & oThisFormA.txtCustDOB.Text.Trim & "', " _
                & "Sex = '" & oThisFormA.txtCustSex.Text.Trim & "'," _
                & "Phone = '" & oThisFormA.txtCustPhone.Text.Trim & "'," _
                & "Address = '" & oThisFormA.txtCustAddress.Text.Trim & "'," _
                & "Suburb = '" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
                & "State = '" & oThisFormA.txtCustState.Text.Trim & "'," _
                & "PostCode = '" & oThisFormA.txtCustPostCode.Text.Trim & "'," _
                & "where ID ='" & oThisFormA.txtCustID.Text.Trim & "';"
    
    
            End If

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

    Re: Help Resolving Syntax Error

    One of the best way to avoid syntax errors is to use a parameterized command. Below is an example doing an INSERT You can do the same for SELECT, UPDATE, DELETE etc.


    Code:
    Using cmd As OleDbCommand = New OleDbCommand With {.Connection = cn}
        cn.Open()
        Try
            cmd.CommandText = _
            <SQL>
                INSERT INTO Customers 
                    (
                        Identifier,
                        FirstName,
                        LastName
                    ) 
                VALUES (@Identifier,@FirstName, @LastName)
            </SQL>.Value
            
            cmd.Parameters.AddWithValue("@Identifier", CurrentID)
            cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text)
            cmd.Parameters.AddWithValue("@LastName", txtLastName.Text)
    
            cmd.ExecuteNonQuery()
    
        Catch OleDbExceptionErr As OleDbException
            MessageBox.Show(OleDbExceptionErr.Message)
        End Try
    
    End Using

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    One of the best way to avoid syntax errors is to use a parameterized command. Below is an example doing an INSERT You can do the same for SELECT, UPDATE, DELETE etc.


    Code:
    Using cmd As OleDbCommand = New OleDbCommand With {.Connection = cn}
        cn.Open()
        Try
            cmd.CommandText = _
            <SQL>
                INSERT INTO Customers 
                    (
                        Identifier,
                        FirstName,
                        LastName
                    ) 
                VALUES (@Identifier,@FirstName, @LastName)
            </SQL>.Value
            
            cmd.Parameters.AddWithValue("@Identifier", CurrentID)
            cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text)
            cmd.Parameters.AddWithValue("@LastName", txtLastName.Text)
    
            cmd.ExecuteNonQuery()
    
        Catch OleDbExceptionErr As OleDbException
            MessageBox.Show(OleDbExceptionErr.Message)
        End Try
    
    End Using
    I dont Understand how the cn Stuff works mate..

    Currently i Have:

    Code:
    Dim oConn As OleDbConnection        'To reference a Connection obj.
            Dim oCmd_Select As OleDbCommand     'To Instantiate a Command obj used to execute the Select SQL.
            Dim oDataReader As OleDbDataReader  'To instantiate a DataReader obj.
            Dim oCmd_Update As OleDbCommand     'To Instantiate a Command obj used to execute the Insert/Update SQL.
    
            'sErrorMessage = ""
            oConn = Nothing
            oCmd_Select = Nothing
            oDataReader = Nothing
            oCmd_Update = Nothing
    
            ' --- create a database command object and set the SQL statement 
            ' --- it will execute, and the database connection object associated to it
            sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
            sConnection = sConnection & "User ID=Admin;"
            sConnection = sConnection & "Data Source="
            sConnection = sConnection & Application.StartupPath & "\" & sDBNameA
            oConn = New OleDbConnection(sConnection)
            oConn.Open()
    
            oCmd_Select = oConn.CreateCommand()
            sSQL = "select ID from CUSTOMER where ID= " & sCusID & ""
            oCmd_Select.CommandText = sSQL
    
            ' --- execute SQL command and place results into a datareader object
            oDataReader = oCmd_Select.ExecuteReader()
    
            bStudentNumberExists = (oDataReader.Read() = True)
    
            'MessageBox.Show("bStudentNumberExists=" & bStudentNumberExists)
            ' --- Build Insert or Update Sql depending if record retrieved with select query is found
            If bStudentNumberExists Then
    
    
                    sSaveSql = "update CUSTOMER set " _
                                 & "Surname = '" & oThisFormA.txtCusSurname.Text.Trim & "'," _
                                 & "Given = '" & oThisFormA.txtCustGiven.Text.Trim & "'," _
                                 & "DateOfBirth = '" & oThisFormA.txtCustDOB.Text.Trim & "', " _
                                 & "Sex = '" & oThisFormA.txtCustSex.Text.Trim & "'," _
                                 & "Phone = '" & oThisFormA.txtCustPhone.Text.Trim & "'," _
                                 & "Address = '" & oThisFormA.txtCustAddress.Text.Trim & "'," _
                                 & "Suburb = '" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
                                 & "State = '" & oThisFormA.txtCustState.Text.Trim & "'," _
                                 & "PostCode = '" & oThisFormA.txtCustPostCode.Text.Trim & "'," _
                                 & "where ID ='" & oThisFormA.txtCustID.Text.Trim & "';"
    
    End If

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

    Re: Help Resolving Syntax Error

    cn is your oConn

    This is known as cn is being set as the connection to the command
    Code:
    Using cmd As OleDbCommand = New OleDbCommand With {.Connection = cn}

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

    Re: Help Resolving Syntax Error

    only because I'm feeling generous today....

    Code:
                    'sSaveSql = "update CUSTOMER set " _
                    '             & "Surname = '" & oThisFormA.txtCusSurname.Text.Trim & "'," _
                    '             & "Given = '" & oThisFormA.txtCustGiven.Text.Trim & "'," _
                    '             & "DateOfBirth = '" & oThisFormA.txtCustDOB.Text.Trim & "', " _
                    '             & "Sex = '" & oThisFormA.txtCustSex.Text.Trim & "'," _
                    '             & "Phone = '" & oThisFormA.txtCustPhone.Text.Trim & "'," _
                    '             & "Address = '" & oThisFormA.txtCustAddress.Text.Trim & "'," _
                    '             & "Suburb = '" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
                    '             & "State = '" & oThisFormA.txtCustState.Text.Trim & "'," _
                    '             & "PostCode = '" & oThisFormA.txtCustPostCode.Text.Trim & "'," _
                    '             & "where ID ='" & oThisFormA.txtCustID.Text.Trim & "';"
    
    
                    sSaveSql = "update CUSTOMER set " _
                                 & "Surname = ?," _
                                 & "Given = ?," _
                                 & "DateOfBirth = ?, " _
                                 & "Sex = ?," _
                                 & "Phone = ?," _
                                 & "Address = ?," _
                                 & "Suburb = ?," _
                                 & "State = ?," _
                                 & "PostCode = ?," _
                                 & "where ID = ?;"
    
    cmd.Parameters.AddWithValue("@Surname", oThisFormA.txtCusSurname.Text.Trim)
    cmd.Parameters.AddWithValue("@Given", oThisFormA.txtCustGiven.Text.Trim)
    cmd.Parameters.AddWithValue("@DateOfBirth", CDate(oThisFormA.txtCustDOB.Text.Trim))
    cmd.Parameters.AddWithValue("@Sex", oThisFormA.txtCustSex.Text.Trim)
    cmd.Parameters.AddWithValue("@Phone",oThisFormA.txtCustPhone.Text.Trim)
    cmd.Parameters.AddWithValue("@Address", oThisFormA.txtCustAddress.Text.Trim)
    cmd.Parameters.AddWithValue("@Suburb", oThisFormA.txtCustSuburb.Text.Trim)
    cmd.Parameters.AddWithValue("@State", oThisFormA.txtCustState.Text.Trim)
    cmd.Parameters.AddWithValue("@PostCode", oThisFormA.txtCustPostCode.Text.Trim)
    cmd.Parameters.AddWithValue("@ID", CInt(oThisFormA.txtCustID.Text.Trim))
    I made some assumptions: 1) Your DOB field is a date type. 2) Your ID field is an integer type.

    -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
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Help Resolving Syntax Error

    You can clean up your connection as follows so not to worry about string concatenation. Note the use of IO.Path.Combine which ensures the blackslash will be added in this case.

    Code:
    Dim builder As New OleDbConnectionStringBuilder With _
        { _
            .DataSource = IO.Path.Combine(Application.StartupPath, sDBNameA), _
            .Provider = "Microsoft.Jet.OLEDB.4.0" _
        }
    builder.Add("User ID", "Admin")
    
    Dim oConn As New OleDbConnection With {.ConnectionString = builder.ConnectionString}

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

    Re: Help Resolving Syntax Error

    I'd also like to point out that I wouldn't use the Reader if all you are doing is checking to see if a record exists... use the ExecuteScalar method instead. Using the reader will lock the connection to the reader, preventing you from using it anywhere else until the reader is closed. By using ExecuteScalar you free up your connection, allowing you to use it for the update or what ever else command you want.

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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    vb Code:
    1. If bStudentNumberExists Then
    2.             Using cmd As OleDbCommand = New OleDbCommand
    3.                 sSaveSql = "update CUSTOMER set " _
    4.                                & "Surname = ?," _
    5.                                & "Given = ?," _
    6.                                & "DateOfBirth = ?, " _
    7.                                & "Sex = ?," _
    8.                                & "Phone = ?," _
    9.                                & "Address = ?," _
    10.                                & "Suburb = ?," _
    11.                                & "State = ?," _
    12.                                & "PostCode = ?," _
    13.                                & "where ID = ?;"
    14.  
    15.                 cmd.Parameters.AddWithValue("@Surname", oThisFormA.txtCusSurname.Text.Trim)
    16.                 cmd.Parameters.AddWithValue("@Given", oThisFormA.txtCustGiven.Text.Trim)
    17.                 cmd.Parameters.AddWithValue("@DateOfBirth", CDate(oThisFormA.txtCustDOB.Text.Trim))
    18.                 cmd.Parameters.AddWithValue("@Sex", oThisFormA.txtCustSex.Text.Trim)
    19.                 cmd.Parameters.AddWithValue("@Phone", oThisFormA.txtCustPhone.Text.Trim)
    20.                 cmd.Parameters.AddWithValue("@Address", oThisFormA.txtCustAddress.Text.Trim)
    21.                 cmd.Parameters.AddWithValue("@Suburb", oThisFormA.txtCustSuburb.Text.Trim)
    22.                 cmd.Parameters.AddWithValue("@State", oThisFormA.txtCustState.Text.Trim)
    23.                 cmd.Parameters.AddWithValue("@PostCode", oThisFormA.txtCustPostCode.Text.Trim)
    24.                 cmd.Parameters.AddWithValue("@ID", CInt(oThisFormA.txtCustID.Text.Trim))
    25.  
    26.             End Using
    27.             'sSaveSql = "update CUSTOMER set " _
    28.             '             & "Surname = '" & oThisFormA.txtCusSurname.Text.Trim & "'," _
    29.             '             & "Given = '" & oThisFormA.txtCustGiven.Text.Trim & "'," _
    30.             '             & "DateOfBirth = '" & oThisFormA.txtCustDOB.Text.Trim & "', " _
    31.             '             & "Sex = '" & oThisFormA.txtCustSex.Text.Trim & "'," _
    32.             '             & "Phone = '" & oThisFormA.txtCustPhone.Text.Trim & "'," _
    33.             '             & "Address = '" & oThisFormA.txtCustAddress.Text.Trim & "'," _
    34.             '             & "Suburb = '" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
    35.             '             & "State = '" & oThisFormA.txtCustState.Text.Trim & "'," _
    36.             '             & "PostCode = '" & oThisFormA.txtCustPostCode.Text.Trim & "'," _
    37.             '             & "where ID ='" & oThisFormA.txtCustID.Text.Trim & "';"
    38.  
    39.         Else
    40.             sSaveSql = "insert into CUSTOMER(ID, Surname, Given, DateOfBirth,Sex,Phone,Address,Suburb,State,PostCode) " _
    41.                      & " values('" & oThisFormA.txtCustID.Text & "'," _
    42.                      & "'" & oThisFormA.txtCusSurname.Text.Trim & "'," _
    43.                      & "'" & oThisFormA.txtCustGiven.Text.Trim & "'," _
    44.                      & "'" & oThisFormA.txtCustDOB.Text.Trim & "'," _
    45.                      & "'" & oThisFormA.txtCustSex.Text.Trim & "'," _
    46.                      & "'" & oThisFormA.txtCustPhone.Text.Trim & "'," _
    47.                      & "'" & oThisFormA.txtCustAddress.Text.Trim & "'," _
    48.                      & "'" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
    49.                      & "'" & oThisFormA.txtCustState.Text.Trim & "'," _
    50.                      & "'" & oThisFormA.txtCustPostCode.Text.Trim & "' )"
    51.             MsgBox("ID:" & sCusID & " has been Added to the table")
    52.  
    53.  
    54.             ' Be careful however with the commas and the closing bracket
    55.         End If

    With that as my Update code is still get SYNTAX ERROR in UPDATE clause

    Thanks for your help so far, but still not working.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by techgnome View Post
    I'd also like to point out that I wouldn't use the Reader if all you are doing is checking to see if a record exists... use the ExecuteScalar method instead. Using the reader will lock the connection to the reader, preventing you from using it anywhere else until the reader is closed. By using ExecuteScalar you free up your connection, allowing you to use it for the update or what ever else command you want.

    -tg
    Ok thanks but it's the way i have been asked to do it

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

    Re: Help Resolving Syntax Error

    Change

    Code:
    & "PostCode = ?," _
    To

    Code:
    & "PostCode = ? " _

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    Change

    Code:
    & "PostCode = ?," _
    To

    Code:
    & "PostCode = ? " _
    That fixed that error, thanks a lot! But now with this full code:
    vb Code:
    1. Public Sub g_sSaveCustomerRecord(ByVal oThisFormA As frmCaptureCustomer, _
    2.                                    ByVal sCusID As String, _
    3.                                    ByVal sDBNameA As String)
    4.         ' ---------------------------------------------------------------------
    5.         ' Incoming Parameters:
    6.         '       oThisFormA  - a reference to the current form
    7.         '                     of type frmDataCapture
    8.         '       sStudentNumberA - The student number
    9.         '       sDBNameA    - The Database File Name
    10.         ' Return value :
    11.         '       if save successfull (no validation issues) return empty string, ""
    12.         '       otherwise return error message
    13.         '------------------------------------------------------------
    14.         Dim sSaveSql As String
    15.         Dim sConnection As String
    16.         Dim sErrorMessage As String
    17.         Dim sSQL As String
    18.         Dim bStudentNumberExists As Boolean
    19.  
    20.         Dim oConn As OleDbConnection        'To reference a Connection obj.
    21.         Dim oCmd_Select As OleDbCommand     'To Instantiate a Command obj used to execute the Select SQL.
    22.         Dim oDataReader As OleDbDataReader  'To instantiate a DataReader obj.
    23.         Dim oCmd_Update As OleDbCommand     'To Instantiate a Command obj used to execute the Insert/Update SQL.
    24.  
    25.         'sErrorMessage = ""
    26.         oConn = Nothing
    27.         oCmd_Select = Nothing
    28.         oDataReader = Nothing
    29.         oCmd_Update = Nothing
    30.  
    31.         ' --- create a database command object and set the SQL statement
    32.         ' --- it will execute, and the database connection object associated to it
    33.         sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
    34.         sConnection = sConnection & "User ID=Admin;"
    35.         sConnection = sConnection & "Data Source="
    36.         sConnection = sConnection & Application.StartupPath & "\" & sDBNameA
    37.         oConn = New OleDbConnection(sConnection)
    38.         oConn.Open()
    39.  
    40.         oCmd_Select = oConn.CreateCommand()
    41.         sSQL = "select ID from CUSTOMER where ID= " & sCusID & ""
    42.         oCmd_Select.CommandText = sSQL
    43.  
    44.         ' --- execute SQL command and place results into a datareader object
    45.         oDataReader = oCmd_Select.ExecuteReader()
    46.  
    47.         bStudentNumberExists = (oDataReader.Read() = True)
    48.  
    49.         'MessageBox.Show("bStudentNumberExists=" & bStudentNumberExists)
    50.         ' --- Build Insert or Update Sql depending if record retrieved with select query is found
    51.         If bStudentNumberExists Then
    52.             Using cmd As OleDbCommand = New OleDbCommand
    53.                 sSaveSql = "update CUSTOMER set " _
    54.                                & "Surname = ?," _
    55.                                & "Given = ?," _
    56.                                & "DateOfBirth = ?, " _
    57.                                & "Sex = ?," _
    58.                                & "Phone = ?," _
    59.                                & "Address = ?," _
    60.                                & "Suburb = ?," _
    61.                                & "State = ?," _
    62.                                & "PostCode = ? " _
    63.                                & "where ID = ?;"
    64.  
    65.                 cmd.Parameters.AddWithValue("@Surname", oThisFormA.txtCusSurname.Text.Trim)
    66.                 cmd.Parameters.AddWithValue("@Given", oThisFormA.txtCustGiven.Text.Trim)
    67.                 cmd.Parameters.AddWithValue("@DateOfBirth", CDate(oThisFormA.txtCustDOB.Text.Trim))
    68.                 cmd.Parameters.AddWithValue("@Sex", oThisFormA.txtCustSex.Text.Trim)
    69.                 cmd.Parameters.AddWithValue("@Phone", oThisFormA.txtCustPhone.Text.Trim)
    70.                 cmd.Parameters.AddWithValue("@Address", oThisFormA.txtCustAddress.Text.Trim)
    71.                 cmd.Parameters.AddWithValue("@Suburb", oThisFormA.txtCustSuburb.Text.Trim)
    72.                 cmd.Parameters.AddWithValue("@State", oThisFormA.txtCustState.Text.Trim)
    73.                 cmd.Parameters.AddWithValue("@PostCode", oThisFormA.txtCustPostCode.Text.Trim)
    74.                 cmd.Parameters.AddWithValue("@ID", CInt(oThisFormA.txtCustID.Text.Trim))
    75.  
    76.             End Using
    77.             'sSaveSql = "update CUSTOMER set " _
    78.             '             & "Surname = '" & oThisFormA.txtCusSurname.Text.Trim & "'," _
    79.             '             & "Given = '" & oThisFormA.txtCustGiven.Text.Trim & "'," _
    80.             '             & "DateOfBirth = '" & oThisFormA.txtCustDOB.Text.Trim & "', " _
    81.             '             & "Sex = '" & oThisFormA.txtCustSex.Text.Trim & "'," _
    82.             '             & "Phone = '" & oThisFormA.txtCustPhone.Text.Trim & "'," _
    83.             '             & "Address = '" & oThisFormA.txtCustAddress.Text.Trim & "'," _
    84.             '             & "Suburb = '" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
    85.             '             & "State = '" & oThisFormA.txtCustState.Text.Trim & "'," _
    86.             '             & "PostCode = '" & oThisFormA.txtCustPostCode.Text.Trim & "'," _
    87.             '             & "where ID ='" & oThisFormA.txtCustID.Text.Trim & "';"
    88.  
    89.             'Else
    90.             '    sSaveSql = "insert into CUSTOMER(ID, Surname, Given, DateOfBirth,Sex,Phone,Address,Suburb,State,PostCode) " _
    91.             '             & " values('" & oThisFormA.txtCustID.Text & "'," _
    92.             '             & "'" & oThisFormA.txtCusSurname.Text.Trim & "'," _
    93.             '             & "'" & oThisFormA.txtCustGiven.Text.Trim & "'," _
    94.             '             & "'" & oThisFormA.txtCustDOB.Text.Trim & "'," _
    95.             '             & "'" & oThisFormA.txtCustSex.Text.Trim & "'," _
    96.             '             & "'" & oThisFormA.txtCustPhone.Text.Trim & "'," _
    97.             '             & "'" & oThisFormA.txtCustAddress.Text.Trim & "'," _
    98.             '             & "'" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
    99.             '             & "'" & oThisFormA.txtCustState.Text.Trim & "'," _
    100.             '             & "'" & oThisFormA.txtCustPostCode.Text.Trim & "' )"
    101.             '    MsgBox("ID:" & sCusID & " has been Added to the table")
    102.  
    103.  
    104.             '    ' Be careful however with the commas and the closing bracket
    105.         End If
    106.  
    107.  
    108.         oDataReader.Close()
    109.         oCmd_Select.Dispose()
    110.  
    111.  
    112.         ' Execute sql update or insert query:
    113.         oCmd_Update = oConn.CreateCommand()
    114.         oCmd_Update.CommandText = sSaveSql
    115.         oCmd_Update.ExecuteNonQuery()
    116.  
    117.  
    118.  
    119.  
    120.  
    121.  
    122.     End Sub

    I get Erro:
    Code:
    No value given for one or more required parameters.
    Any Idea why?

    I have done a debug and every txt box had a vale assigned to it.

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

    Re: Help Resolving Syntax Error

    Good to hear that fixed the first error, using parameters along with you statement w/o string concatenation allows you to examine said statement easier to spot issues.

    For example the following returns a string which is easy to locate issues.
    Code:
    Dim UpdateStatement = _
    <SQL>
        UPDATE CUSTOMER set 
            Surname = ?,
            Given = ?,
            DateOfBirth = ?, 
            Sex = ?,
            Phone = ?,
            Address = ?,
            Suburb = ?,
            State = ?,
            PostCode = ?
        WHERE ID = ?
    </SQL>.Value

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    Good to hear that fixed the first error, using parameters along with you statement w/o string concatenation allows you to examine said statement easier to spot issues.

    For example the following returns a string which is easy to locate issues.
    Code:
    Dim UpdateStatement = _
    <SQL>
        UPDATE CUSTOMER set 
            Surname = ?,
            Given = ?,
            DateOfBirth = ?, 
            Sex = ?,
            Phone = ?,
            Address = ?,
            Suburb = ?,
            State = ?,
            PostCode = ?
        WHERE ID = ?
    </SQL>.Value
    Sorry to bug ya mate, im new to vb haha so not to sure what you want me to do with that piece of code

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

    Re: Help Resolving Syntax Error

    Quote Originally Posted by coccoster View Post
    Sorry to bug ya mate, im new to vb haha so not to sure what you want me to do with that piece of code
    Lets simplify it by taking the variable out, this is your SQL statement assigned directly to the commmand's command text.
    Code:
    Using cmd As OleDbCommand = New OleDbCommand
        cmd.CommandText = _
        <SQL>
        UPDATE CUSTOMER set 
            Surname = ?,
            Given = ?,
            DateOfBirth = ?, 
            Sex = ?,
            Phone = ?,
            Address = ?,
            Suburb = ?,
            State = ?,
            PostCode = ?
        WHERE ID = ?
        </SQL>.Value
    
    End Using

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    Lets simplify it by taking the variable out, this is your SQL statement assigned directly to the commmand's command text.
    Code:
    Using cmd As OleDbCommand = New OleDbCommand
        cmd.CommandText = _
        <SQL>
        UPDATE CUSTOMER set 
            Surname = ?,
            Given = ?,
            DateOfBirth = ?, 
            Sex = ?,
            Phone = ?,
            Address = ?,
            Suburb = ?,
            State = ?,
            PostCode = ?
        WHERE ID = ?
        </SQL>.Value
    
    End Using
    I commented out the other code you gave me and pasted that code instead.. The only error i got was
    Code:
    "Command text was not set for the command object."
    Not sure im doing the right thing though.

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

    Re: Help Resolving Syntax Error

    Here is another example

    Original
    Code:
    sSQL = "select ID from CUSTOMER where ID= " & sCusID & ""
    Alternate
    Code:
    Dim sSQL = <SQL>select ID from CUSTOMER where ID= <%= sCusID %></SQL>.Value
    Both do the same thing but the second one w/o concatenation

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    Here is another example

    Original
    Code:
    sSQL = "select ID from CUSTOMER where ID= " & sCusID & ""
    Alternate
    Code:
    Dim sSQL = <SQL>select ID from CUSTOMER where ID= <%= sCusID %></SQL>.Value
    Both do the same thing but the second one w/o concatenation
    Ohh ok..

    I un commented out everything, and did what you just posted..

    Still get the same error as above:
    Code:
    "Command text was not set for the command object."
    Sp full code looks like:

    vb Code:
    1. Public Sub g_sSaveCustomerRecord(ByVal oThisFormA As frmCaptureCustomer, _
    2.                                    ByVal sCusID As String, _
    3.                                    ByVal sDBNameA As String)
    4.         ' ---------------------------------------------------------------------
    5.         ' Incoming Parameters:
    6.         '       oThisFormA  - a reference to the current form
    7.         '                     of type frmDataCapture
    8.         '       sStudentNumberA - The student number
    9.         '       sDBNameA    - The Database File Name
    10.         ' Return value :
    11.         '       if save successfull (no validation issues) return empty string, ""
    12.         '       otherwise return error message
    13.         '------------------------------------------------------------
    14.         Dim sSaveSql As String
    15.         Dim sConnection As String
    16.         Dim sErrorMessage As String
    17.         Dim sSQL As String
    18.         Dim bStudentNumberExists As Boolean
    19.  
    20.         Dim oConn As OleDbConnection        'To reference a Connection obj.
    21.         Dim oCmd_Select As OleDbCommand     'To Instantiate a Command obj used to execute the Select SQL.
    22.         Dim oDataReader As OleDbDataReader  'To instantiate a DataReader obj.
    23.         Dim oCmd_Update As OleDbCommand     'To Instantiate a Command obj used to execute the Insert/Update SQL.
    24.  
    25.         'sErrorMessage = ""
    26.         oConn = Nothing
    27.         oCmd_Select = Nothing
    28.         oDataReader = Nothing
    29.         oCmd_Update = Nothing
    30.  
    31.         ' --- create a database command object and set the SQL statement
    32.         ' --- it will execute, and the database connection object associated to it
    33.         sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
    34.         sConnection = sConnection & "User ID=Admin;"
    35.         sConnection = sConnection & "Data Source="
    36.         sConnection = sConnection & Application.StartupPath & "\" & sDBNameA
    37.         oConn = New OleDbConnection(sConnection)
    38.         oConn.Open()
    39.  
    40.         oCmd_Select = oConn.CreateCommand()
    41.         sSQL = <SQL>select ID from CUSTOMER where ID= <%= sCusID %></SQL>.Value
    42.         'sSQL = "select ID from CUSTOMER where ID= " & sCusID & ""
    43.         oCmd_Select.CommandText = sSQL
    44.  
    45.         ' --- execute SQL command and place results into a datareader object
    46.         oDataReader = oCmd_Select.ExecuteReader()
    47.  
    48.         bStudentNumberExists = (oDataReader.Read() = True)
    49.  
    50.         'MessageBox.Show("bStudentNumberExists=" & bStudentNumberExists)
    51.         ' --- Build Insert or Update Sql depending if record retrieved with select query is found
    52.         If bStudentNumberExists Then
    53.  
    54.             Using cmd As OleDbCommand = New OleDbCommand
    55.                 sSaveSql = "update CUSTOMER set " _
    56.                                & "Surname = ?," _
    57.                                & "Given = ?," _
    58.                                & "DateOfBirth = ?, " _
    59.                                & "Sex = ?," _
    60.                                & "Phone = ?," _
    61.                                & "Address = ?," _
    62.                                & "Suburb = ?," _
    63.                                & "State = ?," _
    64.                                & "PostCode = ? " _
    65.                                & "where ID = ?;"
    66.  
    67.                 cmd.Parameters.AddWithValue("@Surname", oThisFormA.txtCusSurname.Text.Trim)
    68.                 cmd.Parameters.AddWithValue("@Given", oThisFormA.txtCustGiven.Text.Trim)
    69.                 cmd.Parameters.AddWithValue("@DateOfBirth", CDate(oThisFormA.txtCustDOB.Text.Trim))
    70.                 cmd.Parameters.AddWithValue("@Sex", oThisFormA.txtCustSex.Text.Trim)
    71.                 cmd.Parameters.AddWithValue("@Phone", oThisFormA.txtCustPhone.Text.Trim)
    72.                 cmd.Parameters.AddWithValue("@Address", oThisFormA.txtCustAddress.Text.Trim)
    73.                 cmd.Parameters.AddWithValue("@Suburb", oThisFormA.txtCustSuburb.Text.Trim)
    74.                 cmd.Parameters.AddWithValue("@State", oThisFormA.txtCustState.Text.Trim)
    75.                 cmd.Parameters.AddWithValue("@PostCode", oThisFormA.txtCustPostCode.Text.Trim)
    76.                 cmd.Parameters.AddWithValue("@ID", CInt(oThisFormA.txtCustID.Text.Trim))
    77.  
    78.  
    79.             End Using
    80.             'sSaveSql = "update CUSTOMER set " _
    81.             '             & "Surname = '" & oThisFormA.txtCusSurname.Text.Trim & "'," _
    82.             '             & "Given = '" & oThisFormA.txtCustGiven.Text.Trim & "'," _
    83.             '             & "DateOfBirth = '" & oThisFormA.txtCustDOB.Text.Trim & "', " _
    84.             '             & "Sex = '" & oThisFormA.txtCustSex.Text.Trim & "'," _
    85.             '             & "Phone = '" & oThisFormA.txtCustPhone.Text.Trim & "'," _
    86.             '             & "Address = '" & oThisFormA.txtCustAddress.Text.Trim & "'," _
    87.             '             & "Suburb = '" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
    88.             '             & "State = '" & oThisFormA.txtCustState.Text.Trim & "'," _
    89.             '             & "PostCode = '" & oThisFormA.txtCustPostCode.Text.Trim & "'," _
    90.             '             & "where ID ='" & oThisFormA.txtCustID.Text.Trim & "';"
    91.  
    92.             'Else
    93.             '    sSaveSql = "insert into CUSTOMER(ID, Surname, Given, DateOfBirth,Sex,Phone,Address,Suburb,State,PostCode) " _
    94.             '             & " values('" & oThisFormA.txtCustID.Text & "'," _
    95.             '             & "'" & oThisFormA.txtCusSurname.Text.Trim & "'," _
    96.             '             & "'" & oThisFormA.txtCustGiven.Text.Trim & "'," _
    97.             '             & "'" & oThisFormA.txtCustDOB.Text.Trim & "'," _
    98.             '             & "'" & oThisFormA.txtCustSex.Text.Trim & "'," _
    99.             '             & "'" & oThisFormA.txtCustPhone.Text.Trim & "'," _
    100.             '             & "'" & oThisFormA.txtCustAddress.Text.Trim & "'," _
    101.             '             & "'" & oThisFormA.txtCustSuburb.Text.Trim & "'," _
    102.             '             & "'" & oThisFormA.txtCustState.Text.Trim & "'," _
    103.             '             & "'" & oThisFormA.txtCustPostCode.Text.Trim & "' )"
    104.             '    MsgBox("ID:" & sCusID & " has been Added to the table")
    105.  
    106.  
    107.             '    ' Be careful however with the commas and the closing bracket
    108.         End If
    109.  
    110.  
    111.         oDataReader.Close()
    112.         oCmd_Select.Dispose()
    113.  
    114.  
    115.         ' Execute sql update or insert query:
    116.         oCmd_Update = oConn.CreateCommand()
    117.         oCmd_Update.CommandText = sSaveSql
    118.         oCmd_Update.ExecuteNonQuery()
    119.  
    120.  
    121.  
    122.  
    123.  
    124.  
    125.     End Sub

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

    Re: Help Resolving Syntax Error

    Quote Originally Posted by coccoster View Post
    I commented out the other code you gave me and pasted that code instead.. The only error i got was
    Code:
    "Command text was not set for the command object."
    Not sure im doing the right thing though.
    Not sure either as the code is clearly setting the CommandText ie
    Code:
            Using cmd As OleDbCommand = New OleDbCommand
                cmd.CommandText = _
                <SQL>
                UPDATE CUSTOMER set 
                    Surname = ?,
                    Given = ?,
                    DateOfBirth = ?, 
                    Sex = ?,
                    Phone = ?,
                    Address = ?,
                    Suburb = ?,
                    State = ?,
                    PostCode = ?
                WHERE ID = ?
                </SQL>.Value
    
                MsgBox(cmd.CommandText)
            End Using
    Or
    Code:
            Using cmd As OleDbCommand = New OleDbCommand
                cmd.CommandText = _
                <SQL>
                UPDATE CUSTOMER set 
                    Surname = ?,
                    Given = ?,
                    DateOfBirth = ?, 
                    Sex = ?,
                    Phone = ?,
                    Address = ?,
                    Suburb = ?,
                    State = ?,
                    PostCode = ?
                WHERE ID = ?
                </SQL>.Value
    
                Console.WriteLine(cmd.CommandText)
            End Using
    Result from console write line
    Code:
                UPDATE CUSTOMER set 
                    Surname = ?,
                    Given = ?,
                    DateOfBirth = ?, 
                    Sex = ?,
                    Phone = ?,
                    Address = ?,
                    Suburb = ?,
                    State = ?,
                    PostCode = ?
                WHERE ID = ?

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    Not sure either as the code is clearly setting the CommandText ie
    Code:
            Using cmd As OleDbCommand = New OleDbCommand
                cmd.CommandText = _
                <SQL>
                UPDATE CUSTOMER set 
                    Surname = ?,
                    Given = ?,
                    DateOfBirth = ?, 
                    Sex = ?,
                    Phone = ?,
                    Address = ?,
                    Suburb = ?,
                    State = ?,
                    PostCode = ?
                WHERE ID = ?
                </SQL>.Value
    
                MsgBox(cmd.CommandText)
            End Using
    Or
    Code:
            Using cmd As OleDbCommand = New OleDbCommand
                cmd.CommandText = _
                <SQL>
                UPDATE CUSTOMER set 
                    Surname = ?,
                    Given = ?,
                    DateOfBirth = ?, 
                    Sex = ?,
                    Phone = ?,
                    Address = ?,
                    Suburb = ?,
                    State = ?,
                    PostCode = ?
                WHERE ID = ?
                </SQL>.Value
    
                Console.WriteLine(cmd.CommandText)
            End Using
    Result from console write line
    Code:
                UPDATE CUSTOMER set 
                    Surname = ?,
                    Given = ?,
                    DateOfBirth = ?, 
                    Sex = ?,
                    Phone = ?,
                    Address = ?,
                    Suburb = ?,
                    State = ?,
                    PostCode = ?
                WHERE ID = ?
    Does the code i posted before look right to you?

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

    Re: Help Resolving Syntax Error

    The following will work if you have Option Infer On and using Framework 3.5 or higher (VS2008 or VS2010)

    I highly suggest turning Option Strict On

    Code:
            If bStudentNumberExists Then
    
                Using cmd As OleDbCommand = New OleDbCommand
    				cmd.CommandText = _
    				<SQL>
    				UPDATE CUSTOMER set 
    					Surname = ?,
    					Given = ?,
    					DateOfBirth = ?, 
    					Sex = ?,
    					Phone = ?,
    					Address = ?,
    					Suburb = ?,
    					State = ?,
    					PostCode = ?
    				WHERE ID = ?
    				</SQL>.Value
    
    
                    cmd.Parameters.AddWithValue("@Surname", oThisFormA.txtCusSurname.Text.Trim)
                    cmd.Parameters.AddWithValue("@Given", oThisFormA.txtCustGiven.Text.Trim)
                    cmd.Parameters.AddWithValue("@DateOfBirth", CDate(oThisFormA.txtCustDOB.Text.Trim))
                    cmd.Parameters.AddWithValue("@Sex", oThisFormA.txtCustSex.Text.Trim)
                    cmd.Parameters.AddWithValue("@Phone", oThisFormA.txtCustPhone.Text.Trim)
                    cmd.Parameters.AddWithValue("@Address", oThisFormA.txtCustAddress.Text.Trim)
                    cmd.Parameters.AddWithValue("@Suburb", oThisFormA.txtCustSuburb.Text.Trim)
                    cmd.Parameters.AddWithValue("@State", oThisFormA.txtCustState.Text.Trim)
                    cmd.Parameters.AddWithValue("@PostCode", oThisFormA.txtCustPostCode.Text.Trim)
                    cmd.Parameters.AddWithValue("@ID", CInt(oThisFormA.txtCustID.Text.Trim))
    
    
                End Using
    
            End If

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Yep ive had Option Strict on the whole time.
    I'm currently using vs2010.

    Well with my code looking like:
    vb Code:
    1. Public Sub g_sSaveCustomerRecord(ByVal oThisFormA As frmCaptureCustomer, _
    2.                                    ByVal sCusID As String, _
    3.                                    ByVal sDBNameA As String)
    4.         ' ---------------------------------------------------------------------
    5.         ' Incoming Parameters:
    6.         '       oThisFormA  - a reference to the current form
    7.         '                     of type frmDataCapture
    8.         '       sStudentNumberA - The student number
    9.         '       sDBNameA    - The Database File Name
    10.         ' Return value :
    11.         '       if save successfull (no validation issues) return empty string, ""
    12.         '       otherwise return error message
    13.         '------------------------------------------------------------
    14.         Dim sSaveSql As String
    15.         Dim sConnection As String
    16.         Dim sErrorMessage As String
    17.         Dim sSQL As String
    18.         Dim bStudentNumberExists As Boolean
    19.  
    20.         Dim oConn As OleDbConnection        'To reference a Connection obj.
    21.         Dim oCmd_Select As OleDbCommand     'To Instantiate a Command obj used to execute the Select SQL.
    22.         Dim oDataReader As OleDbDataReader  'To instantiate a DataReader obj.
    23.         Dim oCmd_Update As OleDbCommand     'To Instantiate a Command obj used to execute the Insert/Update SQL.
    24.  
    25.         'sErrorMessage = ""
    26.         oConn = Nothing
    27.         oCmd_Select = Nothing
    28.         oDataReader = Nothing
    29.         oCmd_Update = Nothing
    30.  
    31.         ' --- create a database command object and set the SQL statement
    32.         ' --- it will execute, and the database connection object associated to it
    33.         sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
    34.         sConnection = sConnection & "User ID=Admin;"
    35.         sConnection = sConnection & "Data Source="
    36.         sConnection = sConnection & Application.StartupPath & "\" & sDBNameA
    37.         oConn = New OleDbConnection(sConnection)
    38.         oConn.Open()
    39.  
    40.         oCmd_Select = oConn.CreateCommand()
    41.         sSQL = <SQL>select ID from CUSTOMER where ID= <%= sCusID %></SQL>.Value
    42.         'sSQL = "select ID from CUSTOMER where ID= " & sCusID & ""
    43.         oCmd_Select.CommandText = sSQL
    44.  
    45.         ' --- execute SQL command and place results into a datareader object
    46.         oDataReader = oCmd_Select.ExecuteReader()
    47.  
    48.         bStudentNumberExists = (oDataReader.Read() = True)
    49.  
    50.  
    51.         ' --- Build Insert or Update Sql depending if record retrieved with select query is found
    52.         If bStudentNumberExists Then
    53.  
    54.             If bStudentNumberExists Then
    55.  
    56.                 Using cmd As OleDbCommand = New OleDbCommand
    57.                     cmd.CommandText = _
    58.                     <SQL>
    59.                 UPDATE CUSTOMER set
    60.                     Surname = ?,
    61.                     Given = ?,
    62.                     DateOfBirth = ?,
    63.                     Sex = ?,
    64.                     Phone = ?,
    65.                     Address = ?,
    66.                     Suburb = ?,
    67.                     State = ?,
    68.                     PostCode = ?
    69.                 WHERE ID = ?
    70.                 </SQL>.Value
    71.  
    72.  
    73.                     cmd.Parameters.AddWithValue("@Surname", oThisFormA.txtCusSurname.Text.Trim)
    74.                     cmd.Parameters.AddWithValue("@Given", oThisFormA.txtCustGiven.Text.Trim)
    75.                     cmd.Parameters.AddWithValue("@DateOfBirth", CDate(oThisFormA.txtCustDOB.Text.Trim))
    76.                     cmd.Parameters.AddWithValue("@Sex", oThisFormA.txtCustSex.Text.Trim)
    77.                     cmd.Parameters.AddWithValue("@Phone", oThisFormA.txtCustPhone.Text.Trim)
    78.                     cmd.Parameters.AddWithValue("@Address", oThisFormA.txtCustAddress.Text.Trim)
    79.                     cmd.Parameters.AddWithValue("@Suburb", oThisFormA.txtCustSuburb.Text.Trim)
    80.                     cmd.Parameters.AddWithValue("@State", oThisFormA.txtCustState.Text.Trim)
    81.                     cmd.Parameters.AddWithValue("@PostCode", oThisFormA.txtCustPostCode.Text.Trim)
    82.                     cmd.Parameters.AddWithValue("@ID", CInt(oThisFormA.txtCustID.Text.Trim))
    83.  
    84.  
    85.                 End Using
    86.  
    87.             End If
    88.  
    89.  
    90.             '    ' Be careful however with the commas and the closing bracket
    91.         End If
    92.  
    93.  
    94.         oDataReader.Close()
    95.         oCmd_Select.Dispose()
    96.  
    97.  
    98.         ' Execute sql update or insert query:
    99.         oCmd_Update = oConn.CreateCommand()
    100.         oCmd_Update.CommandText = sSaveSql
    101.         oCmd_Update.ExecuteNonQuery()
    102.  
    103.     End Sub

    I am getting the same
    Code:
    "Command text was not set for the command object."
    error

    I put Option Infer On right at the top aswell.

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

    Re: Help Resolving Syntax Error

    Quote Originally Posted by coccoster View Post
    Yep ive had Option Strict on the whole time.
    I'm currently using vs2010.

    Well with my code looking like:
    vb Code:
    1. Public Sub g_sSaveCustomerRecord(ByVal oThisFormA As frmCaptureCustomer, _
    2.                                    ByVal sCusID As String, _
    3.                                    ByVal sDBNameA As String)
    4.         ' ---------------------------------------------------------------------
    5.         ' Incoming Parameters:
    6.         '       oThisFormA  - a reference to the current form
    7.         '                     of type frmDataCapture
    8.         '       sStudentNumberA - The student number
    9.         '       sDBNameA    - The Database File Name
    10.         ' Return value :
    11.         '       if save successfull (no validation issues) return empty string, ""
    12.         '       otherwise return error message
    13.         '------------------------------------------------------------
    14.         Dim sSaveSql As String
    15.         Dim sConnection As String
    16.         Dim sErrorMessage As String
    17.         Dim sSQL As String
    18.         Dim bStudentNumberExists As Boolean
    19.  
    20.         Dim oConn As OleDbConnection        'To reference a Connection obj.
    21.         Dim oCmd_Select As OleDbCommand     'To Instantiate a Command obj used to execute the Select SQL.
    22.         Dim oDataReader As OleDbDataReader  'To instantiate a DataReader obj.
    23.         Dim oCmd_Update As OleDbCommand     'To Instantiate a Command obj used to execute the Insert/Update SQL.
    24.  
    25.         'sErrorMessage = ""
    26.         oConn = Nothing
    27.         oCmd_Select = Nothing
    28.         oDataReader = Nothing
    29.         oCmd_Update = Nothing
    30.  
    31.         ' --- create a database command object and set the SQL statement
    32.         ' --- it will execute, and the database connection object associated to it
    33.         sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
    34.         sConnection = sConnection & "User ID=Admin;"
    35.         sConnection = sConnection & "Data Source="
    36.         sConnection = sConnection & Application.StartupPath & "\" & sDBNameA
    37.         oConn = New OleDbConnection(sConnection)
    38.         oConn.Open()
    39.  
    40.         oCmd_Select = oConn.CreateCommand()
    41.         sSQL = <SQL>select ID from CUSTOMER where ID= <%= sCusID %></SQL>.Value
    42.         'sSQL = "select ID from CUSTOMER where ID= " & sCusID & ""
    43.         oCmd_Select.CommandText = sSQL
    44.  
    45.         ' --- execute SQL command and place results into a datareader object
    46.         oDataReader = oCmd_Select.ExecuteReader()
    47.  
    48.         bStudentNumberExists = (oDataReader.Read() = True)
    49.  
    50.  
    51.         ' --- Build Insert or Update Sql depending if record retrieved with select query is found
    52.         If bStudentNumberExists Then
    53.  
    54.             If bStudentNumberExists Then
    55.  
    56.                 Using cmd As OleDbCommand = New OleDbCommand
    57.                     cmd.CommandText = _
    58.                     <SQL>
    59.                 UPDATE CUSTOMER set
    60.                     Surname = ?,
    61.                     Given = ?,
    62.                     DateOfBirth = ?,
    63.                     Sex = ?,
    64.                     Phone = ?,
    65.                     Address = ?,
    66.                     Suburb = ?,
    67.                     State = ?,
    68.                     PostCode = ?
    69.                 WHERE ID = ?
    70.                 </SQL>.Value
    71.  
    72.  
    73.                     cmd.Parameters.AddWithValue("@Surname", oThisFormA.txtCusSurname.Text.Trim)
    74.                     cmd.Parameters.AddWithValue("@Given", oThisFormA.txtCustGiven.Text.Trim)
    75.                     cmd.Parameters.AddWithValue("@DateOfBirth", CDate(oThisFormA.txtCustDOB.Text.Trim))
    76.                     cmd.Parameters.AddWithValue("@Sex", oThisFormA.txtCustSex.Text.Trim)
    77.                     cmd.Parameters.AddWithValue("@Phone", oThisFormA.txtCustPhone.Text.Trim)
    78.                     cmd.Parameters.AddWithValue("@Address", oThisFormA.txtCustAddress.Text.Trim)
    79.                     cmd.Parameters.AddWithValue("@Suburb", oThisFormA.txtCustSuburb.Text.Trim)
    80.                     cmd.Parameters.AddWithValue("@State", oThisFormA.txtCustState.Text.Trim)
    81.                     cmd.Parameters.AddWithValue("@PostCode", oThisFormA.txtCustPostCode.Text.Trim)
    82.                     cmd.Parameters.AddWithValue("@ID", CInt(oThisFormA.txtCustID.Text.Trim))
    83.  
    84.  
    85.                 End Using
    86.  
    87.             End If
    88.  
    89.  
    90.             '    ' Be careful however with the commas and the closing bracket
    91.         End If
    92.  
    93.  
    94.         oDataReader.Close()
    95.         oCmd_Select.Dispose()
    96.  
    97.  
    98.         ' Execute sql update or insert query:
    99.         oCmd_Update = oConn.CreateCommand()
    100.         oCmd_Update.CommandText = sSaveSql
    101.         oCmd_Update.ExecuteNonQuery()
    102.  
    103.     End Sub

    I am getting the same
    Code:
    "Command text was not set for the command object."
    error

    I put Option Infer On right at the top aswell.
    Which line number in regards to the code above is giving you this error message.

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    Which line number in regards to the code above is giving you this error message.
    101. oCmd_Update.ExecuteNonQuery()

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

    Re: Help Resolving Syntax Error

    Quote Originally Posted by coccoster View Post
    101. oCmd_Update.ExecuteNonQuery()
    That is because you have not set the CommandText for oCmd_Update

    You have set the following only
    Code:
    oCmd_Select.CommandText = sSQL
    and
    Code:
    Using cmd As OleDbCommand = New OleDbCommand
        cmd.CommandText = _
        <SQL>
        UPDATE CUSTOMER set 
            Surname = ?,
            Given = ?,
            DateOfBirth = ?, 
            Sex = ?,
            Phone = ?,
            Address = ?,
            Suburb = ?,
            State = ?,
            PostCode = ?
        WHERE ID = ?
        </SQL>.Value

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    That is because you have not set the CommandText for oCmd_Update

    You have set the following only
    Code:
    oCmd_Select.CommandText = sSQL
    and
    Code:
    Using cmd As OleDbCommand = New OleDbCommand
        cmd.CommandText = _
        <SQL>
        UPDATE CUSTOMER set 
            Surname = ?,
            Given = ?,
            DateOfBirth = ?, 
            Sex = ?,
            Phone = ?,
            Address = ?,
            Suburb = ?,
            State = ?,
            PostCode = ?
        WHERE ID = ?
        </SQL>.Value

    I'm not sure what i have to do now, sorry mate.

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

    Re: Help Resolving Syntax Error

    Quote Originally Posted by coccoster View Post

    I'm not sure what i have to do now, sorry mate.
    You need to set the CommandText, simple as that.

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Quote Originally Posted by kevininstructor View Post
    You need to set the CommandText, simple as that.
    Im sorry but how do i do that??

    Im not quite sure i understand entirely what i need to edit/add

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

    Re: Help Resolving Syntax Error

    Quote Originally Posted by coccoster View Post
    Im sorry but how do i do that??

    Im not quite sure i understand entirely what i need to edit/add
    You set the CommandText
    i.e.

    MyCommand.CommandText = "Your command text"

    or as I have shown using
    Code:
    <SQL>
    UPDATE CUSTOMER set 
        Surname = ?,
        Given = ?,
        DateOfBirth = ?, 
        Sex = ?,
        Phone = ?,
        Address = ?,
        Suburb = ?,
        State = ?,
        PostCode = ?
    WHERE ID = ?
    </SQL>.Value
    It is that simple.

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    137

    Re: Help Resolving Syntax Error

    Code:
    <SQL>
    UPDATE CUSTOMER set 
        Surname = ?,
        Given = ?,
        DateOfBirth = ?, 
        Sex = ?,
        Phone = ?,
        Address = ?,
        Suburb = ?,
        State = ?,
        PostCode = ?
    WHERE ID = ?
    </SQL>.Value
    So i have to remove the "?" and add txtHardGiven.Text etc??

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

    Re: Help Resolving Syntax Error

    no... you assign theabove as t he command text... then you need to add the parameters in the manner in which I showed using command.parameters.addwithvalue ...

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

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