Using DAO to insert records 
Author Message
 Using DAO to insert records

Unfortunately, I do not have much experience with Acess VBA and the
syntax.  My task is to insert records/data extracted from another DB
into an Access table.  So, far I am able to open the DB and the Table,
but I do not know the correct syntax to create a record, insert data
into the fields, and then post the record.

Any help would be greatly appreciated.

Todd

The following code is Object Pascal (Borland Delphi).  Though the syntax
is different in VBA, there are enough similarities, that I can translate it.
{========================================================================
 Open an Access DB and a table
 ========================================================================}
procedure TExportToAccess.OpenAccessTable;
var
  AccessApp: Variant;
begin
  AccessApp := CreateOleObject('Access.Application');
  Try
    AccessApp.OpenCurrentDatabase(FDatabaseName);
    Try
      AccessApp.DoCmd.OpenTable(FTableName, acNormal, acEdit);
    Except
      AccessApp := UnAssigned;
    End;
  Except
    //
  End;
  AccessApp := UnAssigned;
end;

--
Todd Cary
Ariste Software
2200 D Street Extension
Petaluma, CA 94952
707-773-4523

[It is simplicity that makes the uneducated more effective than
 the educated when addressing popular audiences. - Aristotle]



Sat, 26 Feb 2005 03:55:47 GMT  
 Using DAO to insert records
Well, I got this far, but I am not sure if what I am doing is the best
way to do it.  For instance, I loop through all of the tables to get the
one I want.  Is there a better way to do this?

I would like to empty the table.  Is there an Empty method?

procedure TExportToAccess.OpenAccessTable;
var
  vAccessApp: Variant;
  vDB: Variant;
  vTable: Variant;
  vRecord: Variant;
  vFields: Variant;
  sEngine: String;
  sValue: String;
  iCount: Integer;
  I: Integer;
  bFoundIt: Boolean;
begin
  bFoundIt := False;
  If IniRec.DAOVer = DAO35 Then
    sEngine := 'DAO.DBEngine.35'
  Else
    sEngine := 'DAO.DBEngine.36';

  Try
    vAccessApp := GetActiveOleObject(sEngine);
  Except
    vAccessApp := CreateOleObject(sEngine);
  End;

  vDB := vAccessApp.OpenDatabase(FDatabaseName);
  iCount := vDB.TableDefs.Count;
  For I := 0 To iCount - 1 Do
  Begin
    If UpperCase(vDB.TableDefs[I].Name) = UpperCase(FTableName) Then
    Begin
      vTable := vDB.TableDefs[I];
      bFoundIt := True;
      Break;
    End;
  End;
  If bFoundIt Then
  Begin
    vRecord := vTable.OpenRecordSet;
    vRecord.AddNew;
    vRecord.AcctNo := '123456';
    vRecord.Update;
  End;
  vDB.Close;
  vAccessApp := UnAssigned;
end;

Todd

--
Todd Cary
Ariste Software
2200 D Street Extension
Petaluma, CA 94952
707-773-4523

[It is simplicity that makes the uneducated more effective than
 the educated when addressing popular audiences. - Aristotle]



Sat, 26 Feb 2005 05:06:02 GMT  
 Using DAO to insert records
Hi Todd

As well as referring to collection members by index number, you can refer to
them by name. So your entire loop:

Quote:
>   iCount := vDB.TableDefs.Count;
>   For I := 0 To iCount - 1 Do
>   Begin
>     If UpperCase(vDB.TableDefs[I].Name) = UpperCase(FTableName) Then
>     Begin
>       vTable := vDB.TableDefs[I];
>       bFoundIt := True;
>       Break;
>     End;
>   End;

can be replaced by:
    vTable := vDB.TableDefs(FTableName)

The best way to empty a table is by executing a SQL statement.  I don't know
what your string concatenation operator is, but assuming it's "&" as in VB:
    vDB.Execute 'Delete * from ' & FTableName

--
Good Luck!
Graham Mandeno [Access MVP]
Auckland, New Zealand

Return mail address is invalid in a vain attempt to reduce spam.

Please post new questions or followups to newsgroup.


Quote:
> Well, I got this far, but I am not sure if what I am doing is the best
> way to do it.  For instance, I loop through all of the tables to get the
> one I want.  Is there a better way to do this?

> I would like to empty the table.  Is there an Empty method?

> procedure TExportToAccess.OpenAccessTable;
> var
>   vAccessApp: Variant;
>   vDB: Variant;
>   vTable: Variant;
>   vRecord: Variant;
>   vFields: Variant;
>   sEngine: String;
>   sValue: String;
>   iCount: Integer;
>   I: Integer;
>   bFoundIt: Boolean;
> begin
>   bFoundIt := False;
>   If IniRec.DAOVer = DAO35 Then
>     sEngine := 'DAO.DBEngine.35'
>   Else
>     sEngine := 'DAO.DBEngine.36';

>   Try
>     vAccessApp := GetActiveOleObject(sEngine);
>   Except
>     vAccessApp := CreateOleObject(sEngine);
>   End;

>   vDB := vAccessApp.OpenDatabase(FDatabaseName);
>   iCount := vDB.TableDefs.Count;
>   For I := 0 To iCount - 1 Do
>   Begin
>     If UpperCase(vDB.TableDefs[I].Name) = UpperCase(FTableName) Then
>     Begin
>       vTable := vDB.TableDefs[I];
>       bFoundIt := True;
>       Break;
>     End;
>   End;
>   If bFoundIt Then
>   Begin
>     vRecord := vTable.OpenRecordSet;
>     vRecord.AddNew;
>     vRecord.AcctNo := '123456';
>     vRecord.Update;
>   End;
>   vDB.Close;
>   vAccessApp := UnAssigned;
> end;

> Todd

> --
> Todd Cary
> Ariste Software
> 2200 D Street Extension
> Petaluma, CA 94952
> 707-773-4523

> [It is simplicity that makes the uneducated more effective than
>  the educated when addressing popular audiences. - Aristotle]



Sat, 26 Feb 2005 06:29:48 GMT  
 Using DAO to insert records
Graham -

Many thanks for the suggestions on the Loop!!

I got the Delete Query working!!!

Todd

--
Todd Cary
Ariste Software
2200 D Street Extension
Petaluma, CA 94952
707-773-4523

[It is simplicity that makes the uneducated more effective than
 the educated when addressing popular audiences. - Aristotle]



Sat, 26 Feb 2005 12:01:07 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Inserting large number of records into a large table with SQL/DAO

2. Insert Into using several textboxes and DAO

3. tranferring records using DAO

4. help with using dao to update a record when printed

5. ADP: Insert New Record using ADO

6. How: Save a Form's current record using VBA/DAO

7. how do I copy a record with a different fields using DAO

8. Inserting records using sqldataadapter

9. Inserting new records using ADO - problem??

10. Insert records using DBGrid??

11. Question about updating record in text file using DAO

12. Deleting records using DAO

 

 
Powered by phpBB® Forum Software