MS Access Query

If I have a query within my database with a parameter who do I call that query using a data command?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Compose the query, replacing the values you want to be parameters with
question marks. Use the query to build a command object.

Use the command's Parameters.Add method to add parameter objects to the
command. The database will replace the question marks with the
parameters in the order in which they are added. In other words, the
first question mark is replaced with the first parameter, and so forth.

Execute the command and process the results as usual.

Imports System.Data.OleDb

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Get the database file name.
        Dim db_name As String = Application.ExecutablePath
        db_name = db_name.Substring(0, db_name.LastIndexOf("\") + 1)
        If db_name.EndsWith("\bin\") Then
            db_name = db_name.Substring(0, db_name.Length - 4)
        End If
        db_name &= "Users.mdb"

        ' Build the database connect string.
        Me.connUsers.ConnectionString = _
            "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;" & _
            "Data Source=""" & db_name & """;Mode=Share Deny None;Jet OLEDB:Engine Type=5;" & _
            "Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;" & _
            "persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;" & _
            "Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;" & _
            "Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"

        ' Load the list of names.
        LoadListBoxFromQuery(lstNames, _
            "SELECT FirstName, LastName FROM People ORDER BY FirstName, LastName")
    End Sub

    Private Sub LoadListBoxFromQuery(ByVal lst As ListBox, ByVal query As String)

        ' Open the connection.

        ' Make a SELECT Command.
        Dim cmd As New OleDb.OleDbCommand( _
            query, connUsers)

        ' Execute the query.
        Dim db_reader As OleDbDataReader = _

        ' Display the results.
        Dim txt As String
        Dim i As Integer
        Do While db_reader.Read
            txt = db_reader.Item(0).ToString
            For i = 1 To db_reader.FieldCount - 1
                txt &= vbTab & db_reader.Item(i).ToString
            Next i

        ' Close the connection.
    End Sub

    Private Sub lstNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstNames.SelectedIndexChanged
        ' Get the selected name.
        Dim names() As String = lstNames.Text.Split(CChar(vbTab))

        ' Display the person's data.
        DisplayPersonData(names(0), names(1))
    End Sub

    Private Sub DisplayPersonData(ByVal first_name As String, ByVal last_name As String)
        ' Open the connection.

        ' Make a Command for this connection
        ' and this transaction.
        Dim cmd As New OleDb.OleDbCommand( _
            "SELECT * FROM People WHERE FirstName=? AND LastName=?", _

        ' Create parameters for the query.
        cmd.Parameters.Add(New OleDb.OleDbParameter("FirstName", first_name))
        cmd.Parameters.Add(New OleDb.OleDbParameter("LastName", last_name))

        ' Execute the query.
        Dim db_reader As OleDbDataReader = _

        ' Display the results.
        If db_reader.HasRows Then
            txtFirstName.Text = db_reader.Item("FirstName").ToString
            txtLastName.Text = db_reader.Item("LastName").ToString
            txtStreet.Text = db_reader.Item("Street").ToString
            txtCity.Text = db_reader.Item("City").ToString
            txtState.Text = db_reader.Item("State").ToString
            txtZip.Text = db_reader.Item("Zip").ToString
            For Each ctl As Control In Me.Controls
                If TypeOf ctl Is TextBox Then ctl.Text = ""
            Next ctl
        End If

        ' Close the connection.
    End Sub

End Class
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.