Can I combine Access and SQL?

just2cool

Registered User.
Local time
Today, 05:36
Joined
Jun 20, 2007
Messages
16
I was wondering how to go about combining both Access and SQL together.
Am I going to write the SQL codes in VB and then Access will know how to combine the tables I've created with the SQL codes?

Thanks in advance!!
Jason
 
This question is a lillte vague - Are you talking SQL server as the BE database?
 
This is what Im not sure about either.

I know how to use SQL statements, and since Im not sure whether Access can do all the things I can do with SQL statements, it seemed to be a good idea to combine both. Im reading a VBA book on Access but I lost track of all the relationships, Access, SQL, VBA...what do they all do together and how do I make use of them all together? Thanks again!
 
You can use SQL in an Access application. If you open a query in Access, there is an SQL view. You can eneter your SQL there and save it as a query. Remeber this is Access dialect SQL so a few little differences, but you'll still get there.
 
My answer will include terms you might wish to look up in Access Help if you are not sure about them.

Part I: Using SQL from VBA

You can do several things from VBA that are directly SQL-related.

First, open a recordset based on SQL using the .OpenRecordset method

Second, execute an SQL action query using the .RunSQL method. (Or is that the .ExecuteSQL method ... I sometimes get confused.)

Third, use functions such as DLookup, DMax, DAvg - which IMPLY an SQL SELECT statement for which you supply the (single) field name to be selected, the table name, and the sometimes optional WHERE clause.

Part II - using VBA in SQL

Any SQL query (well... almost any query) can include VBA references in the SELECT and WHERE clauses. For VBA that implies underlying SQL, using that code runs the risk of self-blocking and of otherwise depressing performance, since VBA code is pseudo-compiled in Access. (Differs from VB compiler, which actually DOES produce machine code...)

To make this work, create public functions in general modules. The code you write has to be PUBLIC - so that SQL can see it. Function - because SQL requires a value. In a general module - because at the time SQL is running, it is not directly in the same context as class modules (associated with forms and reports). But a general module is in context for as long as Access is open.

When writing VBA functions for use in SQL, remember that the returned value must be consistent. I.e. if it is an INTEGER function, it DAMN well must not ever return a DOUBLE value. And it should always return something no matter what kind of error it encounters. I.e. good error trapping.
 
Second, execute an SQL action query using the .RunSQL method. (Or is that the .ExecuteSQL method ... I sometimes get confused.)

To execute action queries (updates, deletes, etc.), there are two methods:

DoCmd.RunSQL "SQL HERE"

-- or --

CurrentDb.Execute "SQL HERE"

The second method is preferable because it does not require you to turn warnings off and on. For example, these two are exactly the same:

DoCmd.SetWarnings Off
DoCmd.RunSQL "SQL HERE"
DoCmd.SetWarnings On


-- and --

CurrentDb.Execute "SQL HERE"

Without turning warnings off and on, you'll get all those, well, warning messages; I.e, "You are about to update X rows in this table. Are you sure?" and so on.

And, note that "SQL HERE" in both examples can be SQL itself of the name of a query.
 
thanks, moniker. my old memory gets fuzzy sometimes and I don't keep the Access help files open for lookups when I answer some of these questions.
 
Thanks guys for the help!!! I'm chewing all the info now.
 

Users who are viewing this thread

Back
Top Bottom