Solved

SQL VBA query out put to a table which already exists

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

809 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