Re: Parameterised query with an ms access table by Paul
Paul
Mon Feb 04 10:21:11 CST 2008
On Fri, 1 Feb 2008 21:52:44 -0000, "John" <John@nospam.infovis.co.uk> wrote:
¤ Hi
¤
¤ When using sql server, I can use a sql like below in my data adapter to send
¤ the sql a parameter value;
¤
¤ SELECT <field list>
¤ FROM <table>
¤ WHERE Fieldx = @Para1
¤
¤ My question is, can I do something similar with an access db ie can I send a
¤ parameter to a data adaptor sql which is connected to an ms access table? If
¤ not is there another way to do this?
Yes, you just need to create the Command object for the query and assign it to the corresponding
Command of the DataAdapter. Below is an example:
Dim AccessConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Test Files\db1 XP.mdb")
Dim AccessCommand As New OleDbCommand("SELECT * FROM Table1 WHERE [Date Field] BETWEEN ? AND ?",
AccessConnection)
AccessConnection.Open()
Dim StartDate As New System.DateTime(2005, 1, 25)
Dim EndDate As New System.DateTime(2006, 5, 5)
AccessCommand.Parameters.Add("@DateValStart", System.Data.OleDb.OleDbType.DBDate).Value = StartDate
AccessCommand.Parameters.Add("@DateValEnd", System.Data.OleDb.OleDbType.DBDate).Value = EndDate
Dim da As New System.Data.OleDb.OleDbDataAdapter
da.SelectCommand = AccessCommand
Dim ds As New DataSet
da.Fill(ds, "Table1")
Paul
~~~~
Microsoft MVP (Visual Basic)