Solved

SQL VBA query out put to a table which already exists

Posted on 2011-09-14
5
286 Views
Last Modified: 2012-05-12
This question is steming from this thread: http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_27308439.html

1. I have a VBA based SQL query output to a table. Everytime I run this, it works, but the 2nd time it gets called, I get a msg "The table ### already exists".  
I want the query to overwrite the existing table, everytime the query is run. If it is already being overwritten, I want no warning msgs though. Upon research I came up with this but its not helping as the "disabling" error msg only works if you work with "Query Design"

My coding is like this

 DoCmd.SetWarnings False
CurrentDb.Execute "SELECT ZONE.CITY, .....
DoCmd.SetWarnings True 

Open in new window


Should I be putting something so it overwrites the existing query output table? (the full query is in above comments)

2. The query itself is taking my WHOLE screen in VBA module going to the left and I have to scroll a mile (as the original I have is huge) Is there a way to break it down within VBA console?

Thanks!
0
Comment
Question by:Shanan212
  • 3
  • 2
5 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 36539291
You need code to delete the table before running a maketable query, and error handling in case you try to delete a non - existent table:


On error goto eh

    CurrentDb.TableDefs.Delete ("yourTableName")
    DoCmd.SetWarnings False
    CurrentDb.Execute "SELECT ZONE.CITY, .....
    DoCmd.SetWarnings True  

    exit sub

EH:
  if err.number = 3265 then resume next
  msgbox "error " & err.number & ": " & err.description

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36539329
Just explaining context a bit... that "Exit Sub" line and the error handler need to go together as shown, immeditately before the End Sub line, AFTER any other code you might have.

If you are using a function, of course, use "Exit Function" instead of "Exit Sub"
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36539365
Thanks for attempting to solve.

1. That exit sub, exits the whole thing or just exit this part "On error goto EH"

2. I also have this function to see if any tables are open and closes all tables. This was previously incorporated into a query which was initiated from 'query design' rather than inside VBA.

Would this work again?

    
    Dim td As DAO.TableDef

For Each td In CurrentDb.TableDefs
       If IsOpen(td.Name, acTable) Then
           DoCmd.Close acTable, td.Name
       End If
    Next

Open in new window


Thanks!

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36539688
This should work with any existing code you have.  Again, all of your code should come BEFORE the Exit Sub statement.

The role of this statement is to make the code exit the sub gracefully (completely) if no errors are encountered.  Without it, the code would progress through the error handler even if there were no errors.

So the *very* last lines in your sub should be these:

    exit sub

EH:
  if err.number = 3265 then resume next
  msgbox "error " & err.number & ": " & err.description 
End Sub

Open in new window

0
 
LVL 13

Author Comment

by:Shanan212
ID: 36542739
Well the query itself is located in a function

Private Sub exgetBEYOND(termcombo As String)
    
    On Error GoTo EH

    CurrentDb.TableDefs.Delete ("getB")
    DoCmd.SetWarnings False
    CurrentDb.Execute "SELECT ZONE.CPCOMBO, ZONE.[ROUTING TERMINAL], ZONE.TCOMBI, ZONE.ZONE, ZONE.MIN, ZONE.LTL, ZONE.[500], ZONE.[1M], ZONE.[2M], ZONE.[5M], ZONE.[10M], ZONE.[20M] INTO getB FROM [ZONE]WHERE ZONE.CPCOMBO=" & Chr(34) & termcombo & Chr(34) & " AND ZONE.ZONE=" & Chr(34) & "BEYOND" & Chr(34), dbFailOnError
    DoCmd.SetWarnings True

    Exit Sub

EH:
  If Err.Number = 3265 Then Resume Next
  MsgBox "error " & Err.Number & ": " & Err.Description
    
End Sub

Open in new window


So I guess its ok!

Thanks!
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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now