Solved

Modify SQL in VBA

Posted on 2011-02-28
4
425 Views
Last Modified: 2012-05-11
I have a make table query that adds a table to a back end database. I need to modify the location of this table from VBA. Here is my query in design view:

SELECT tblJobNumbers.ID, tblJobNumbers.[Job Number] INTO tblJobNumbersLoc IN 'C:\Program Files\Timesheet Database\TSDB JobsLoc and SubmitLoc.mdb'
FROM tblJobNumbers
WHERE (((tblJobNumbers.ID) Like "1249"));

How can I modify this query in VBA?
0
Comment
Question by:threeieng
4 Comments
 
LVL 25

Expert Comment

by:reb73
ID: 34998636
Not 100% sure what you are alluding to, perhaps the link below puts you on the right path -

http://www.tek-tips.com/viewthread.cfm?qid=1118666&page=2
0
 
LVL 28

Expert Comment

by:omgang
ID: 34998779
Assuming the query name is MyQuery; change accordingly
OM Gang


Public Function TestQuerySQL()

On Error GoTo Err_TestQuerySQL

    Dim qdf As DAO.QueryDef
    Dim strSQL As String, strOldPath As String, strNewPath As String
    Dim intStart As Integer, intEnd As Integer
   
    strNewPath = "c:\temp\NewDB.mdb"     '<------- set path to new db location here

    Set qdf = CurrentDb.QueryDefs("MyQuery")
    strSQL = qdf.SQL  
        'get the start location of the current db path in the SQL statement
    intStart = InStr(strSQL, "IN '") + 4
        'get the end location
    intEnd = InStr(intStart, strSQL, "' From")
        'grab the full path from the current SQL statement
    strOldPath = Mid(strSQL, intStart, (intEnd - intStart))
        'replace the current path with the new path
    strSQL = Replace(strSQL, strOldPath, strNewPath)
    'Debug.Print strSQL
        'update the SQL statement in the QueryDef object
    qdf.SQL = strSQL

Exit_TestQuerySQL:
    Set qdf = Nothing
    Exit Function

Err_TestQuerySQL:
    MsgBox Err.Number & ", " & Err.Description, , "Error in Function TestQuerySQL of Module Module8"
    Resume Exit_TestQuerySQL

End Function
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 34998796
Build your SQL as a string:

strSQL = <create string similar to your present SQL.>
CurrentDb.Execute(strSQL)

/gustav
0
 
LVL 9

Accepted Solution

by:
Armen Stein - Microsoft Access MVP since 2006 earned 500 total points
ID: 34999943
First, MakeTable is not a great approach.  It adds more to database bloat, and you lose some control over the structure of the resulting table.

A better approach is to use a persistent table with exactly the structure you want.  Then you can run a query to Delete all records from it, and another one to Append the ones you want.  Much cleaner.

Also, with this approach you now have the ability to use the table as a normal linked table - no need to hardcode the file location as you show in your example.

Cheers,
Armen
0

Featured Post

Industry Leaders: 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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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…

733 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