Solved

SQL VBA query out put to a table which already exists

Posted on 2011-09-14
5
288 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
[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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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, when working with VBA, learn some techniques for writing readable and easily maintained code.

730 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