?
Solved

Modify SQL in VBA

Posted on 2011-02-28
4
Medium Priority
?
433 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
[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
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 51

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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