Results 1 to 9 of 9

Thread: [RESOLVED] Problem in executing query

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    33

    Resolved [RESOLVED] Problem in executing query

    hi i have the following query which inserts selected rows from one table into another based on a condition.I get the following error when i use the ExecuteReader() method to fire the query
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
             Dim con As OleDb.OleDbConnection
             Dim strinsert As String
             Dim cmdinsert As OleDb.OleDbCommand
            con = New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=D:\master.mdb")
    
            con.Open()
             
    strinsert = "insert into qtest(Qid,Question,Option A,Option B,Option C,Option D,Solution,Date Created) select * from Questionbank where Qid>3"
            cmdinsert = New OleDb.OleDbCommand(strinsert, con)
            cmdinsert.ExecuteReader()
            con.Close()
    I get an error saying "Syntax Error in Insert Statement" & the following line gets highlighted

    cmdinsert.ExecuteReader()

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Problem in executing query

    You are not reading anything from the database so you should be using ExecuteNonQuery method. And your Insert statement should look like this
    PHP Code:
    "Insert Into qtest(Qid,Question,Option A,Option B,Option C,Option D,Solution," 
    "[Date Created]) Select Qid,Question,Option A,Option B,Option C,Option D,Solution,[Date Created] From Questionbank where Qid>3" 
    Notice the square brackets in for the Date Created field and the list of columns in the Select query.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    33

    Re: Problem in executing query

    It still does not work I made the changes as u told,n how do u say ders no query to the DB the select statement inside the insert does query the db doesnt it? So how can i use the ExecuteNonquery method??/

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Problem in executing query

    Well there are other fields too that have a space in their field names. All of them should be put in square brackets.
    PHP Code:
    "Insert Into qtest(Qid,Question,[Option A],[Option B],[Option C]," 
    " [Option D],Solution,[Date Created]) Select Qid,Question,[Option A],[Option B],[Option C],[Option D],Solution,[Date Created] From Questionbank where Qid>3" 
    Select is a subquery in this and Insert is the actual query, so you are not reading anything from the database that needs to be stored in the client side. You should execute it as NonQuery.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5
    Member juanmf's Avatar
    Join Date
    Dec 2005
    Location
    Mordor
    Posts
    46

    Re: Problem in executing query

    do u really need to specify the fields? if the * in the SELECT statement has the same order and column count. shouldn´t it be:
    strinsert = "insert into qtest select * from Questionbank where Qid>3"
    mysql> insert into prestamos select * FROM PRESTAMOS WHERE NOMBRE LIKE "%JI%";
    Query OK, 2 rows affected (0.00 sec)
    it works too and have less probability of error.
    If an answer to your question has been helpful, then please, Rate it! where are u?
    On Staying Informed and Intellectual Self-Defense

    There's no way to be informed without devoting effort to the task, whether we have in mind what's happening in the world, or anything else. Understanding doesn't come free. But it's feasible for anyone who is part of a cooperative community -- Same holds for "intellectual self-defense."

    By Noam Chomsky

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

    Re: Problem in executing query

    juanmf - intentions are in the right place, but not entirely correct. It is actualy SAFER to specify the field names. Two reasons... if a new field is added... or you don't want to copy over any ID fields....

    The query as Shuja Ali has provided should work....

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

  7. #7
    Member juanmf's Avatar
    Join Date
    Dec 2005
    Location
    Mordor
    Posts
    46

    Re: Problem in executing query

    thanks for the tips techgnome!
    If an answer to your question has been helpful, then please, Rate it! where are u?
    On Staying Informed and Intellectual Self-Defense

    There's no way to be informed without devoting effort to the task, whether we have in mind what's happening in the world, or anything else. Understanding doesn't come free. But it's feasible for anyone who is part of a cooperative community -- Same holds for "intellectual self-defense."

    By Noam Chomsky

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    33

    Re: Problem in executing query

    Quote Originally Posted by Shuja Ali
    Well there are other fields too that have a space in their field names. All of them should be put in square brackets.
    PHP Code:
    "Insert Into qtest(Qid,Question,[Option A],[Option B],[Option C]," 
    " [Option D],Solution,[Date Created]) Select Qid,Question,[Option A],[Option B],[Option C],[Option D],Solution,[Date Created] From Questionbank where Qid>3" 
    Select is a subquery in this and Insert is the actual query, so you are not reading anything from the database that needs to be stored in the client side. You should execute it as NonQuery.

    Thanx Shuja Ali the code now worked like a charm & I was mistaken about NonQuery I made sum research on the net n found tht u were right indeed thanx again this forum rox

  9. #9
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Problem in executing query

    You are welcome.

    If your problem is resolved, could you please Mark This thread resolved by opening Thread Tools and select Mark Thread as Resolved.

    I spammed again
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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