
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.