?
Solved

SQL-Execute out of Access

Posted on 2011-09-23
12
Medium Priority
?
287 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 1000 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 1000 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

801 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