[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

MS Access Query

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

0
Gunit2507
Asked:
Gunit2507
2 Solutions
 
iboutchkineCommented:
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)
        lst.Items.Clear()

        ' Open the connection.
        connUsers.Open()

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

        ' Execute the query.
        Dim db_reader As OleDbDataReader = _
            cmd.ExecuteReader(CommandBehavior.Default)

        ' 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
            lst.Items.Add(txt)
        Loop

        ' Close the connection.
        connUsers.Close()
    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.
        connUsers.Open()

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

        ' 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 = _
            cmd.ExecuteReader(CommandBehavior.SingleRow)

        ' Display the results.
        If db_reader.HasRows Then
            db_reader.Read()
            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
        Else
            For Each ctl As Control In Me.Controls
                If TypeOf ctl Is TextBox Then ctl.Text = ""
            Next ctl
        End If

        ' Close the connection.
        connUsers.Close()
    End Sub

End Class
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now