Solved

SQL VBA query out put to a table which already exists

Posted on 2011-09-14
5
284 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

760 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

20 Experts available now in Live!

Get 1:1 Help Now