473,480 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Truncate in VBA

48 New Member
Hi,

Is it possible to fire a "Truncate <table>" from VBA (access2003) into SQL server ?
Special considerations ?

Thanks
Jan 22 '08 #1
6 29210
ADezii
8,834 Recognized Expert Expert
Hi,

Is it possible to fire a "Truncate <table>" from VBA (access2003) into SQL server ?
Special considerations ?

Thanks
I don't think you can do it directly through VBA, but you can indirectly through ADO by executing a Stored Procedure (let's call it StoredProcedure1) via a Command Object:
  1. Create a Stored Procedure that will execute a TRUNCATE TABLE against an Authors Table on a Database residing on an SQL Server.
    Expand|Select|Wrap|Line Numbers
    1. ALTER PROCEDURE dbo.StoredProcedure1
    2. AS TRUNCATE TABLE  dbo.Authors
  2. Execute the Stored Procedure via an ADO Command Object.
    Expand|Select|Wrap|Line Numbers
    1. Dim cnn As ADODB.Connection, cmd As ADODB.Command
    2.  
    3. Set cnn = CurrentProject.Connection
    4. Set cmd = New ADODB.Command
    5.  
    6. cmd.ActiveConnection = cnn
    7. cmd.CommandText = "StoredProcedure1"
    8. cmd.CommandType = adCmdStoredProc
    9.  
    10. cmd.Execute
    11.  
    12. Set cmd = Nothing
    13. Set cnn = Nothing
    14.  
  3. Hope this helps, I'm sort of out-of-my-field on this one!
Jan 23 '08 #2
Jim Doherty
897 Recognized Expert Contributor
Hi,

Is it possible to fire a "Truncate <table>" from VBA (access2003) into SQL server ?
Special considerations ?

Thanks
ADeziis example is solid and spot on I throw this into the pot for no other than your interest level of potential difference in method of execution from the 'Access' side creating a temporary querydef setting an ODBC connection string to the server and then executing the .SQL property of simply.

TRUNCATE TABLE dbo.tblBlah

has the same effect but 'does' rely on ODBC connectivity so for me has more overhead but does NOT need a specific stored procedure. Depends on your design flavour.

Personally I keep work server side so I'd go with ADezii on the actual way to go on this (as your stored procedure could then go on to account for elements such as why the table was truncated and maybe fire the reasons off to another audit system all from within the same procedure etc etc

Jim :)
Jan 23 '08 #3
wquatan
48 New Member
Thanks ADeziis and Jim !

I'm completely new into SQL server but not into Access. Till now lack of time made me put code (query's) on the Access-side (despite preferring it server-side)

In this particular case (because of the provided example, thanks for that) I'll go for the ADeziis example.

But just out of curiosity, how would I do the "TRUNCATE TABLE dbo.tblBlah" from VBA ?

Thanks
Jan 23 '08 #4
ADezii
8,834 Recognized Expert Expert
Thanks ADeziis and Jim !

I'm completely new into SQL server but not into Access. Till now lack of time made me put code (query's) on the Access-side (despite preferring it server-side)

In this particular case (because of the provided example, thanks for that) I'll go for the ADeziis example.

But just out of curiosity, how would I do the "TRUNCATE TABLE dbo.tblBlah" from VBA ?

Thanks
To the best of my knowledge you can't do it directly from VBA. Mr. Doherty would have a better idea on this than I would, he seems to be better qualified.
Jan 23 '08 #5
Jim Doherty
897 Recognized Expert Contributor
Thanks ADeziis and Jim !

I'm completely new into SQL server but not into Access. Till now lack of time made me put code (query's) on the Access-side (despite preferring it server-side)

In this particular case (because of the provided example, thanks for that) I'll go for the ADeziis example.

But just out of curiosity, how would I do the "TRUNCATE TABLE dbo.tblBlah" from VBA ?

Thanks

Using ODBC connectivity to SQL server the logic would be to create a temporary passthrough query on the fly set its connect property in code to the relevant connection string (the example below uses windows integrated security). We set the necessary SQL property of the temporary query to the TRANSACT SQL command that gets fired server side and also set the 'returns records' property of the passthrough query to false (we arent returning a recordset to the client here obviously we are merely executing a process on the server)

The below is the code that will perform that action and relies on a reference to the DAO 3.6 object library (I leave error handlng to you nothing destructive this will either work or it will not)

Expand|Select|Wrap|Line Numbers
  1.  On Error Resume Next 
  2. Dim mydb As DAO.Database
  3. Dim qdf As DAO.querydef
  4. Set mydb = CurrentDb
  5. Set qdf = mydb.CreateQueryDef("")
  6. With qdf
  7. .Connect = "ODBC;DSN=yourservername;UID=username;DATABASE=yourdbname;Trusted_Connection=Yes"
  8. .SQL = "TRUNCATE TABLE dbo.tblBlah"
  9. .ReturnsRecords = False
  10. .Execute
  11. .Close
  12. End With
  13. 'If you wish the query to persist in the database
  14. 'as a permanent query then you MUST supply a name for the query
  15. 'shown between speechmarks in the createquerydef("") statement
  16. 'ie replace createquerydef("") with createquerydef("qryTruncate") for instance.
  17. 'if you do save it to the database as a permanent
  18. 'query then uncommenting the below line will delete it once this routine ends
  19. 'CurrentDb.QueryDefs.Delete (qdf.Name)
  20.  
  21.  
And thankyou ADezii for your gracious response but in your presence I can with confidence say "No way Jose" :)

Hope this helps you a little wquatan

Jim :)
Jan 24 '08 #6
ADezii
8,834 Recognized Expert Expert
Using ODBC connectivity to SQL server the logic would be to create a temporary passthrough query on the fly set its connect property in code to the relevant connection string (the example below uses windows integrated security). We set the necessary SQL property of the temporary query to the TRANSACT SQL command that gets fired server side and also set the 'returns records' property of the passthrough query to false (we arent returning a recordset to the client here obviously we are merely executing a process on the server)

The below is the code that will perform that action and relies on a reference to the DAO 3.6 object library (I leave error handlng to you nothing destructive this will either work or it will not)

Expand|Select|Wrap|Line Numbers
  1.  On Error Resume Next 
  2. Dim mydb As DAO.Database
  3. Dim qdf As DAO.querydef
  4. Set mydb = CurrentDb
  5. Set qdf = mydb.CreateQueryDef("")
  6. With qdf
  7. .Connect = "ODBC;DSN=yourservername;UID=username;DATABASE=yourdbname;Trusted_Connection=Yes"
  8. .SQL = "TRUNCATE TABLE dbo.tblBlah"
  9. .ReturnsRecords = False
  10. .Execute
  11. .Close
  12. End With
  13. 'If you wish the query to persist in the database
  14. 'as a permanent query then you MUST supply a name for the query
  15. 'shown between speechmarks in the createquerydef("") statement
  16. 'ie replace createquerydef("") with createquerydef("qryTruncate") for instance.
  17. 'if you do save it to the database as a permanent
  18. 'query then uncommenting the below line will delete it once this routine ends
  19. 'CurrentDb.QueryDefs.Delete (qdf.Name)
  20.  
  21.  
And thankyou ADezii for your gracious response but in your presence I can with confidence say "No way Jose" :)

Hope this helps you a little wquatan

Jim :)
You are quite welcome, Jim. Thanks for the code snippet, I was wandering how to implement it myself.
Jan 24 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4515
by: martin | last post by:
Hi, We have a heavily used production server and a table which logs every hit on a web site. This table has grown large over time and we want to clear it down as efficiently as possible. We would...
3
8639
by: LineVoltageHalogen | last post by:
Greeting All, I have a stored proc that dynamically truncates all the tables in my databases. I use a cursor and some dynamic sql for this: ...... create cursor Loop through sysobjects and...
1
3017
by: New MSSQL DBA | last post by:
I have recently been assigned to take over several MSSQL environments and found some of the existing practice confusing. As most of my previous experiences are on Oracle and Unix platform so would...
5
3090
by: ronin 47th | last post by:
Hi group, In one of the books 'Gurus Guide to Transact SQL' i found this info: ------------------------------------------------------------ TRUNCATE TABLE empties a table without logging row...
2
47497
by: rdraider | last post by:
Hi, I am trying to create a script that deletes transaction tables and leaves master data like customer, vendors, inventory items, etc. How can I use TRUNCATE TABLE with an Exists? My problem is...
9
14673
by: Sumanth | last post by:
Are there any implementations of truncate in db2. Is it going to be implemented in the future? Is there an alternate way of doing a truncate of a table that has a high record count without using...
14
12253
by: Sala | last post by:
Hi I want to truncate all data in database ... pls help me how i ll truncate?
10
13864
by: Troels Arvin | last post by:
Hello, Until this date, I believed that DB2 has no TRUNCATE TABLE command. But then I came across...
5
1091
by: Timothy Madden | last post by:
Hello I see there is now why to truncate a file (in C or C++) and that I have to use platform-specific functions for truncating files. Anyone knows why ? I mean C/C++ evolved over many years...
8
7672
by: ananthaisin | last post by:
How to reduce the table size for any table while using truncate or delete statements. In oracle 8i it was truncating the storage space but in 10g it is not .... I have given truncate statement in...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3015
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
574
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.