Question Pass Parameters to query

kbsudhir

Member
Joined
Jun 13, 2008
Messages
21
Programming Experience
Beginner


Hi All,

I am trying to pass execute a Access Query with parameters from VB.Net. It is not extracting any data from the tables nor throwing any errors. I am not able to understand where am I going wrong. Below is my code snippet.

VB.NET:
Private Sub ClientNamecbox_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ClientNamecbox.SelectedIndexChanged



        Dim ParamValue As String = ""
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Docs\Tracker.accdb;")
        Dim cmd As New OleDbCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "GetPursuit"





        cmd.Parameters.Add("Name", OleDbType.VarWChar).Value = Namecbox.SelectedValue ' Add Parameter
        cmd.Connection = con




        Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        con.Open()


        Dim ds As DataSet = New DataSet()
        da.Fill(ds, "Tracker")



        PNamecbox.ValueMember = ""
        PNamecbox.DataSource = ds.Tables("Tracker")
        PNamecbox.DisplayMember = "PName"
        PNamecbox.ValueMember = "PName"


        con.Close()


  End Sub

Please guide.

Thanks in advance.

Regards
Sudhir

 
Hi,

What you have shown is basically correct, assuming that you have populated your ComboBox in the correct way to use the SelectedValue property of the ComboBox. So, knowing that, there is no way that we can really answer your question since you have not shown what the SQL is that you have used in your Access Query.

Try posting the SQL of your query to see if we can help you further.

Cheers,

Ian
 

Hi All,

As instructed below is my query which I have created at 2010.

SELECT Tracker.PName
FROM Tracker
WHERE (((Tracker.Name)=""))
GROUP BY Tracker.Name;

Regards
Sudhir

 
Hi,

You have not written your SQL to accept a Parameter which is why it is not working. In your code you declare a Parameter called "Name" so you need to add that to your query which you do with Square Brackets in Access. Here is an example of how the SQL should look like:-

VB.NET:
SELECT Employees.*
FROM Employees
WHERE (((Employees.FirstName)=[Name]));

Hope that helps.

Cheers,

Ian
 
Back
Top