Results 1 to 9 of 9

Thread: Using Parameterized Query

  1. #1

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Using Parameterized Query

    I using this code to get value from database using ADODB connection. But i am getting runtime error in the highlighted field. How to add parameterized query.
    Code:
    Dim db As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim tmp As String
    Private Sub Combo1_click()
    rs.Open "SELECT journeytime from ticket where cityname=' & combo1.text & '", db
    rs.MoveFirst
    While (rs.EOF = False)
    tmp = rs.Fields(0).Value
    Combo2.AddItem (tmp)
    rs.MoveNext
    Wend
    rs.Close
    End Sub
    
    Private Sub Form_Load()
    db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;Persist Security Info=False"
    rs.Open "SELECT cityname from ticket", db
    rs.MoveFirst
    While (rs.EOF = False)
    tmp = rs.Fields(0).Value
    Combo1.AddItem (tmp)
    rs.MoveNext
    Wend
    rs.Close
    End Sub

  2. #2
    gibra
    Guest

    Re: Using Parameterized Query

    See this topic
    Database - Why should I use Parameters instead of putting values into my SQL string?
    http://www.vbforums.com/showthread.php?t=548787

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Using Parameterized Query

    Quote Originally Posted by bharanidharanit View Post
    I using this code to get value from database using ADODB connection. But i am getting runtime error in the highlighted field. How to add parameterized query.
    Code:
    Dim db As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim tmp As String
    Private Sub Combo1_click()
    rs.Open "SELECT journeytime from ticket where cityname=' & combo1.text & '", db
    rs.MoveFirst
    While (rs.EOF = False)
    tmp = rs.Fields(0).Value
    Combo2.AddItem (tmp)
    rs.MoveNext
    Wend
    rs.Close
    End Sub
    
    Private Sub Form_Load()
    db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;Persist Security Info=False"
    rs.Open "SELECT cityname from ticket", db
    rs.MoveFirst
    While (rs.EOF = False)
    tmp = rs.Fields(0).Value
    Combo1.AddItem (tmp)
    rs.MoveNext
    Wend
    rs.Close
    End Sub
    Change that line to something like this:
    Code:
    rs.Open "SELECT journeytime from ticket where cityname='" & combo1.text & "'", db
    ....
    Last edited by akhileshbc; Feb 13th, 2010 at 06:14 AM. Reason: spelling mistake

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Using Parameterized Query

    ya thankyou that works, i missed that...

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Using Parameterized Query

    If that solved your problem, please mark the Thread as RESOLVED....

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Using Parameterized Query

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

    While akhileshbc's method has worked so far, I strongly recommend that you use parameters as explained in the link gibra posted - because it solves lots of problems that you haven't discovered yet (like having the ' character in the data, which will make your code fail and/or have errors).

  7. #7

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Using Parameterized Query

    thankyou all....

  8. #8

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Using Parameterized Query

    I dont know whats the error here, It says "cannot change the ActiveConnection property of a Recordset object which has a Command object as its source"
    Code:
    strSQL = "select journeytime from schedule where cityname=param1"
    Dim adoCommand As adodb.Command
    Set adoCommand = New adodb.Command
    With adoCommand
        .ActiveConnection = adoConnection
        .CommandType = adCmdText
        .CommandText = strSQL
        .Prepared = True
        .Parameters.Append .CreateParameter("param1", adVarChar, adParamInput, Len(Combo1.Text), Combo1.Text)
        .Execute
    End With
    Set adoRecordset = New adodb.Recordset
        adoRecordset.Open adoCommand, adoConnection, 3, 2
    
    Combo2.Text = "Select Time"
    While (adoRecordset.EOF = False)
    tmp = adoRecordset.Fields(0).Value
    Combo2.AddItem (tmp)
    adoRecordset.MoveNext
    Wend
    Set adoCommand = Nothing
    Set adoRecordset = Nothing
    Combo2.Enabled = True
    Combo2.SetFocus

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

    Re: Using Parameterized Query

    Code:
    strSQL = "select journeytime from schedule where cityname=?" 'If using Access, use ? as the parameter placeholder; for SQL Server use @paramname
    Dim adoCommand As adodb.Command
    Set adoCommand = New adodb.Command
    With adoCommand
        .ActiveConnection = adoConnection
        .CommandType = adCmdText
        .CommandText = strSQL
        .Prepared = True
        .Parameters.Append .CreateParameter("param1", adVarChar, adParamInput, Len(Combo1.Text), Combo1.Text)
    Set adoRecordset =   .Execute
    End With
    'Set adoRecordset = New adodb.Recordset
    '    adoRecordset.Open adoCommand, adoConnection, 3, 2
    
    Combo2.Text = "Select Time"
    While (adoRecordset.EOF = False)
    tmp = adoRecordset.Fields(0).Value
    Combo2.AddItem (tmp)
    adoRecordset.MoveNext
    -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