Results 1 to 13 of 13

Thread: VB2010 SQL Error in Select statement.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    VB2010 SQL Error in Select statement.

    Hey,

    From what I can see this is all correct.
    Code:
            Dim cn As New ADODB.Connection
            cn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";user id=admin;Jet OLEDB:Database Password=police10;"
            cn.Open()
    
            Dim cmd = New ADODB.Command
            Dim rs = New ADODB.Recordset
    
            With cmd
                .ActiveConnection = cn
                .CommandType = CommandType.Text
                .CommandText = "SELECT colTitle FROM tblSongs WHERE colArtist=?"
                .CreateParameter(, ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 40, "Beach boys")
                .Prepared = True
                rs = .Execute 
            End With
    Error location is bold. It says "No value given for one or more required parameters."

    Thanks,
    Jessee

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB2010 SQL Error in Select statement.

    You need to append parameter to parameters collection http://www.vbforums.com/showthread.php?t=629066

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: VB2010 SQL Error in Select statement.

    Hey,

    I adapted it to this?
    Code:
    .Parameters.Append(.CreateParameter(, ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 100, frmSelection.lstArtist.Text))
    And it didnt work.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB2010 SQL Error in Select statement.

    did you try giving the parameter a name?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: VB2010 SQL Error in Select statement.

    Hey,

    Code:
    With cmd
                .ActiveConnection = cn
                .CommandType = CommandType.Text
                .CommandText = "SELECT colTitle FROM tblSongs WHERE colArtist= Artist"
                .Parameters.Append(.CreateParameter("Artist", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 100, frmSelection.lstArtist.Text))
                .Prepared = True
                rs = .Execute
            End With
    And I get this error:
    Unable to cast COM object of type 'System.__ComObject' to class type 'ADODB.InternalParameter'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: VB2010 SQL Error in Select statement.

    Anything?

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB2010 SQL Error in Select statement.

    Try to do it similar to sample. And make sure you are not nesting With blocks.

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

    Re: VB2010 SQL Error in Select statement.

    Question: If this is in VS2010... then you're using .NET, right? Why? Why? WHYYY are you using ADO then? Should be using ADO.NET... would make things soooo much easier.

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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: VB2010 SQL Error in Select statement.

    Quote Originally Posted by techgnome View Post
    Question: If this is in VS2010... then you're using .NET, right? Why? Why? WHYYY are you using ADO then? Should be using ADO.NET... would make things soooo much easier.
    -tg
    Hey,

    Im just so accustomed to the way I was using it in VB6, and cant grasp how to do it in vs 2010.

    Tried doing it using datareader I think it was, but gave up on that as I had no idea what it was and how to use it.

  10. #10
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: VB2010 SQL Error in Select statement.

    The problem is that this:-
    "SELECT colTitle FROM tblSongs WHERE colArtist= Artist"
    Should still be this:-
    "SELECT colTitle FROM tblSongs WHERE colArtist=?"
    ADO doesn't actually recognise a parm name in the sql string itself. Instead it assigns parameter value in sequence, so the first question mark will get assigned the first parameter in the collection. Perverse, I know, but there you have it. I think Leinad was telling you to name the parm in the collection, not the sql string.

    TG is right, though, ADO.Net is MUCH better and well worth investing a bit of time in learning. Check out the tutorials on this forum. They were enough for me to get my head round the switch and I came from ADO too. It's really not that hard.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  11. #11
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: VB2010 SQL Error in Select statement.

    Quote Originally Posted by Letsgetcoding View Post
    Hey,

    Im just so accustomed to the way I was using it in VB6, and cant grasp how to do it in vs 2010.

    Tried doing it using datareader I think it was, but gave up on that as I had no idea what it was and how to use it.
    You just highlighted what's ailing some of the IT departments across the world.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: VB2010 SQL Error in Select statement.

    Quote Originally Posted by abhijit View Post
    You just highlighted what's ailing some of the IT departments across the world.

    What do you mean?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: VB2010 SQL Error in Select statement.

    Quote Originally Posted by FunkyDexter View Post
    The problem is that this:-
    "SELECT colTitle FROM tblSongs WHERE colArtist= Artist"
    Should still be this:-
    "SELECT colTitle FROM tblSongs WHERE colArtist=?"
    ADO doesn't actually recognise a parm name in the sql string itself. Instead it assigns parameter value in sequence, so the first question mark will get assigned the first parameter in the collection. Perverse, I know, but there you have it. I think Leinad was telling you to name the parm in the collection, not the sql string.

    TG is right, though, ADO.Net is MUCH better and well worth investing a bit of time in learning. Check out the tutorials on this forum. They were enough for me to get my head round the switch and I came from ADO too. It's really not that hard.
    Hey,

    Can you please link me to the appropriate tutorial?

    Thanks!
    Jessee

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