SQL

S

subs

i have a SQL query - i want the query to be executed but i donot want
the users to see the SQLstatement- is there anyway to do this
 
K

KenSheridan via AccessMonster.com

One option would be to execute the SQL statement in code, e.g. in a command
button's Click event procedure:

Using DAO:

Dim dbs As DAO.Database
Dim strSQL As String

Set dbs = CurrentDb

strSQL = "UPDATE Products " & _
"SET UnitPrice = UnitPrice * 1.1"

dbs.Execute strSQL

or using ADO:

Dim cmd As ADODB.Command
Dim strSQL As String

Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText

strSQL = "UPDATE Products " & _
"SET UnitPrice = UnitPrice * 1.1"

cmd.CommandText = strSQL
cmd.Execute

Ken Sheridan
Stafford, England
 
S

subs

One option would be to execute the SQL statement in code, e.g. in a command
button's Click event procedure:

Using DAO:

    Dim dbs As DAO.Database
    Dim strSQL As String

    Set dbs = CurrentDb

    strSQL = "UPDATE Products  " & _
        "SET UnitPrice = UnitPrice * 1.1"

    dbs.Execute strSQL

or using ADO:

    Dim cmd As ADODB.Command
    Dim strSQL As String

    Set cmd = New ADODB.Command
    cmd.ActiveConnection = CurrentProject.Connection
    cmd.CommandType = adCmdText

    strSQL = "UPDATE Products  " & _
        "SET UnitPrice = UnitPrice * 1.1"

    cmd.CommandText = strSQL
    cmd.Execute

Ken Sheridan
Stafford, England

i am quite new to this- where would i find event procedure--- where
would i put my sql statement-- how should i go about doing this in
access thanks much
 
K

KenSheridan via AccessMonster.com

Firstly you'd need to create a form then add a command button to it. Then
select the button in form design view and open its properties sheet if its
not already open. Then select the On Click event property in the properties
sheet. Click on the 'build' button; that's the one on the right with 3 dots.
Select 'Code Builder' in the dialogue, and click OK. The VBA window will
open at the event procedure with the first and last lines already in place.
Enter the lines of code between these two existing lines.

The SQL statement is the string assigned to the strSQL variable in the code.
In my example its:

strSQL = "UPDATE Products " & _
"SET UnitPrice = UnitPrice * 1.1"

which increases the price of all products by 10 percent. You'd put your owm
SQL in here. Note how the statement is split over two lines for readability
in my example by using the _ (underscore) continuation character.

Code like this executes an SQL statement to carry out an action which changes
the data. I'm assuming this is what you want as you used the word 'execute'
in your post. If, however, you are referring not to undertaking an action
which changes the data, but retrieving data via a query then the best and
simplest way to do that would be to base a form or report on the query and
simply open the form or report via a button. The button wizard will set up
the code to open a form or report for you. You don't even need to save the
query for this; you can simply make the SQL statement the form or report's
RecordSource property.

Ken Sheridan
Stafford, England
One option would be to execute the SQL statement in code, e.g. in a command
button's Click event procedure:
[quoted text clipped - 34 lines]
i am quite new to this- where would i find event procedure--- where
would i put my sql statement-- how should i go about doing this in
access thanks much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top