|
-
Feb 13th, 2010, 05:03 AM
#1
Thread Starter
Fanatic Member
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
-
Feb 13th, 2010, 05:16 AM
#2
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
-
Feb 13th, 2010, 06:02 AM
#3
Re: Using Parameterized Query
 Originally Posted by bharanidharanit
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,...
-
Feb 13th, 2010, 06:28 AM
#4
Thread Starter
Fanatic Member
Re: Using Parameterized Query
ya thankyou that works, i missed that...
-
Feb 13th, 2010, 06:34 AM
#5
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,...
-
Feb 13th, 2010, 07:46 AM
#6
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).
-
Feb 13th, 2010, 10:45 AM
#7
Thread Starter
Fanatic Member
Re: Using Parameterized Query
-
Feb 13th, 2010, 11:20 PM
#8
Thread Starter
Fanatic Member
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
-
Feb 13th, 2010, 11:52 PM
#9
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|