Executing Access Query within VB Code 
Author Message
 Executing Access Query within VB Code

I have a datagrid control connected to a table that is
created by a make table query in Access 2000.  I need to
re-execute the query from the VB app when new records are
added to the source tables in order to update the target
table.  Not sure how to do this.  I have tried connecting
the ado control directly to the query, but I get
a "Syntax error in From clause" message when connecting
the datagrid to the data source.

SQL from the make table query:
SELECT Titles.Title, Titles.ISBN, Authors.Author,
Titles.Year_Published, Publishers.Company_Name INTO
AllTitles
FROM Authors INNER JOIN (Publishers INNER JOIN ([title
author] INNER JOIN Titles ON [title author].ISBN =
Titles.ISBN) ON Publishers.PubID = Titles.PubID) ON
Authors.Au_ID = [title author].Au_ID;

Any suggestions?
Thank you.



Sun, 22 May 2005 12:30:09 GMT  
 Executing Access Query within VB Code
Jim

1) I suppose you are aware that following statement creates a new table that
copies table structure and data. However, this does not copy indexes or
constraints.You have to create theses  with CREATE INDEX, ALTER TABLE
So you could run this one time to create the table.

SELECT whatever INTO TargetTable FROM etc.

Subsequently, you should run

INSERT INTO TargetTable SELECT whatever FROM etc.

otherwise everytime you run the first statement, you should get an error
(TargetTable already exists), unless you delete the TargetTable everytime
you execute the first staement.

Note : You can execute these statements using the connection object.

2) After updating the TargetTable (in your case AllTitles), if you wish to
display it in Datagrid, you should do this

    ObjRecordset.Open "AllTitles",ObjConnection, adOpenStatic,
adLockOptimistic, adCmdTable

Sukesh

Here is a snippet of the code using ADO Library.

    Dim rs As ADODB.Recordset
    Dim cn As ADODB.Connection
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    cn.CursorLocation = adUseClient
    cn.Mode = adModeReadWrite
    cn.Open "ConnectionString"
    strSQL="SELECT whatever INTO TargetTable FROM etc."
    or
    strSQL = "INSERT INTO TargetTable SELECT whatever FROM etc. "
   cn.Execute strSQL
   rs.Open "TargetTable", cn, adOpenStatic, adLockOptimistic, adCmdTable
   Set DataGrid.DataSource = rs


Quote:
> I have a datagrid control connected to a table that is
> created by a make table query in Access 2000.  I need to
> re-execute the query from the VB app when new records are
> added to the source tables in order to update the target
> table.  Not sure how to do this.  I have tried connecting
> the ado control directly to the query, but I get
> a "Syntax error in From clause" message when connecting
> the datagrid to the data source.

> SQL from the make table query:
> SELECT Titles.Title, Titles.ISBN, Authors.Author,
> Titles.Year_Published, Publishers.Company_Name INTO
> AllTitles
> FROM Authors INNER JOIN (Publishers INNER JOIN ([title
> author] INNER JOIN Titles ON [title author].ISBN =
> Titles.ISBN) ON Publishers.PubID = Titles.PubID) ON
> Authors.Au_ID = [title author].Au_ID;

> Any suggestions?
> Thank you.



Sun, 22 May 2005 22:36:36 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Vba code which can execute FTP script from within the Access 97 module

2. Executing Excel-VBA code within a Access-database

3. VB 5.0 and executing queries from within

4. Executing MS Access modules from within VB

5. Newbie: How to execute Query from VB code?

6. Newbie: How to execute Query from VB code?

7. Executing query from VB code

8. executing Access query containing a user-defined function through VB

9. VB using DAO to execute Access query containing a linked table

10. Problem executing a saved query access from VB 6,0

11. Executing Access query containing a user-defined function through VB

12. Calling Access queries from within VB 5.0

 

 
Powered by phpBB® Forum Software