Re: "If ......Exists'.... Then Run SQL code"

From: Allen Browne (AllenBrowne_at_SeeSig.Invalid)
Date: 11/29/04


Date: Mon, 29 Nov 2004 12:04:29 +0800

Roger, here is the code to delete all records from a table, and reset the
Seed of the AutoNumber field to 1.

To use it:

1. Click on the Modules tab of the Database window.

2. Click New. Access opens a code window.

3. From the Tools menu, choose References.

4. Check the box beside:
        Microsoft ADO Ext. 2.x for DLL and Security

5. Paste in the code below.

6. Check that it compiles: Compile on Debug menu.

7. Save the module. A name like Module1 is okay.

To use the code to delete all records from a table named "MyTempTable", use
a macro with the RunCode action, and in the lower pane of macro design,
enter:
    DeleteAllAndResetAutoNum("MyTempTable")

-------------code starts----------------
Function DeleteAllAndResetAutoNum(strTable As String) As Boolean
    'Return: True if sucessful.
    Dim cat As New ADOX.Catalog
    Dim tbl As ADOX.Table
    Dim col As ADOX.Column
    Dim strSql As String

    'Delete all records.
    strSql = "DELETE FROM [" & strTable & "];"
    CurrentProject.Connection.Execute strSql

    'Find and reset the AutoNum field.
    cat.ActiveConnection = CurrentProject.Connection
    Set tbl = cat.Tables(strTable)
    For Each col In tbl.Columns
        If col.Properties("Autoincrement") Then
            col.Properties("Seed") = 1
            DeleteAllAndResetAutoNum = True
        End If
    Next
End Function
-------code ends-------------

-- 
Allen Browne - Microsoft MVP.  Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Gettingthere" <Gettingthere@discussions.microsoft.com> wrote in message
news:EC4D2E1C-5B54-4746-A761-5EBD3E5BE400@microsoft.com...
> No doubt you are 100% correct.  Do to my level of knowledge (nil) in
> programming (writing scripts),  I have ended up in this format.  The 
> biggest
> hurdle was trying to group records  in batches of 25 grouped by dates AND
> list in a reporr same order as entered.  Very dificult without knowing any
> programming.
> Always want to improve anything I do , any other suggestions would be
> appreciated.
>
> Thanks
>
> Roger 


Relevant Pages

  • Re: saved bitmap size
    ... Peter Proost wrote: ... Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. ... Dim i As Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: saved bitmap size
    ... i did test my owned bmp and the file size is 3.6 mb and downsizing to 67kb and i saved as supra.bmp. ... Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. ... Dim i As Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Import data from two workbooks with similar names
    ... I don't understand the programming behind this, ... > The following finds and opens two files. ... > Dim objFSO As Scripting.FileSystemObject ... > Dim objFile As Scripting.File ...
    (microsoft.public.excel.programming)
  • Re: 2 different numbers.....
    ... Peter Proost wrote: ... Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. ... Private Sub Form1_Load(ByVal sender As Object, ... Dim xPos, yPos As Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Degradation of performance : upgrading from Excel 2000 to Excel 20
    ... well we are nuclear powered here Roger with Winfrith just down the road ... I tried switching off multithreaded calculation mode in ... Dim i As Long ... this code works both on Excel 2000 and on Excel 2003 but on the same ...
    (microsoft.public.excel.programming)