Solved

SQL-Execute out of Access

Posted on 2011-09-23
12
282 Views
Last Modified: 2012-05-12
I do have following code that doesn't work

Private Sub Form_Close()
Dim c As ADODB.Connection
Dim cmd As ADODB.Command
Dim Srv As Variant
Dim db As Variant
Dim rs As New Recordset

'Server_Info-------------------------------------------------
                    Srv = "7920"
                    db = "K"
'------------------------------------------------------------
Set c = New ADODB.Connection
c.ConnectionString = "Driver={SQL Server};Server=" & Srv & ";Database=" & db & ""
c.Open

c.Execute "DELETE FROM tblStatistikTmp_sb_Land"  
c.Execute "DELETE FROM tblStatistikTmp_sb_Index"
c.Execute "DELETE FROM tblStatistikTmp_sb_Sektor"
c.Execute "DELETE FROM tblStatistikChartTmp"        
c.Execute "DELETE FROM tblStatistikChartTmp_Spot"
c.Close

Set c = Nothing
End Sub

Connecting to the Server works on other issues when I execute a stored procedure and if I run the delete code on the SQL-Server directly, it works as well, no idea why it doesnt work out of the Access?
thx for any help
0
Comment
Question by:Kongta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 36585891
What is the exact problem you are having with the code -- error messages, etc?

Do you have your references set for ADO?

From the toolbar in the VBA Editor:

Tools --> References -->  {ensure that the reference for "Microsoft ActiveX Data Objects 2.? Library" is checked)
0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 36585912
Why not do something with the Command object?

That's what you should be executing, not the connection.

Set cmd = New ADODB.Command

cmd.Text = "DELETE FROM tblStatistikTmp_sb_Sektor"

cmd.ActiveConnection = c

cmd.Execute
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36585975
<That's what you should be executing, not the connection.>

That's typically what I use as well, but connection.execute is a valid approach (it should work unless there is something else going wrong).
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 34

Expert Comment

by:Norie
ID: 36586060
Agreed, I posted that incorrectly using Execute with a connection should work.

I just thought using the Command object might help in finding the problem.

Also it was there, so why not use it.:)

0
 

Author Comment

by:Kongta
ID: 36586148
I do have Microsoft ActiveX Data Objects 6. activated already.

Using mbizup's code, I get an error on cmd.Text  ?!
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36586170
Kongta,

You're getting us confused.  I haven't suggested any code yet :)

Again - what was the exact issue (or error message) you were getting with the code you originally posted?
0
 

Author Comment

by:Kongta
ID: 36586200
Sorry, I just implemented my code in a blank form an ran it and it worked. Seems not to work in my other form which has a few graphs in it and three subforms, no idea why.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36586227
The code *should* fly syntactically.

Do any of the delete queries work (how about a delete query on a table that is unrelated to the form or subforms)?  Try them one at a time.

What are the recordsources of the form and subforms?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36586235
And what is the exact error message, if any?
0
 

Accepted Solution

by:
Kongta earned 0 total points
ID: 36586262
oh man, how stup... am I? Got it, haven't choosen/activated the procedure in the on the properties, damn

Shall I split the points, is that ok for you?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36586304
You're not stupid - that happens to everyone.  :)

A point split (or a deletion) is fine with me, but if you want to award points, you should click the "Accept and Award Points" button under your own last comment.  That marks your answer as 'accepted' so that people reading this know what the resolution was, and it lets you award some or all of the points at your own discretion to different comments as 'assisted'
0
 

Author Closing Comment

by:Kongta
ID: 36715177
thx, yeah, full in the code and didn't think about activation. rgds and nice weekend.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question