Results 1 to 30 of 30

Thread: Help Resolving Syntax Error

Hybrid View

  1. #1
    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.

  2. #2

    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()

  3. #3
    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

  4. #4

    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.

  5. #5
    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.

  6. #6

    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

  7. #7
    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.

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