Solved

SQL VBA query out put to a table which already exists

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

691 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