Solved

Modify SQL in VBA

Posted on 2011-02-28
4
410 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 49

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

831 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