Question with

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ineedahelp
    New Member
    • Sep 2006
    • 98

    Question with

    Can someone please explain to me what the line of code:

    qdf.SQL = strSQL
    is accomplishing?
    1.strSQL = "SELECT DailyPrice.Symb ol, DailyPrice.Loca teDate, DailyPrice.Mark etPrice, DailyPrice.Pric eFlag FROM DailyPrice " & _
    "WHERE DailyPrice.Symb ol IN (" & strCriteria & ") " & _
    "ORDER BY DailyPrice.Symb ol, DailyPrice.Loca teDate;"
    '*****great stuff here type: Set qdf = db.querydefs into thescripts
    Set qdf = CurrentDb.Query Defs("qryDummy" )
    qdf.SQL = strSQL
    DoCmd.OpenQuery ("qryDummy")

    Private Sub cmbSelectionDon e_Click()
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim varItem As Variant
    Dim strCriteria As String
    Dim strSQL As String

    Set db = CurrentDb()
    Set qdf = db.QueryDefs("q ryMultiSelect")
    If Me!cboxRangeNam e = -1 Then
    myPath = txtFilePath & txtImportName
    myExport = Me!txtExportNam e
    myImport = Me!txtImportNam e
    DoCmd.SetWarnin gs (False)
    DoCmd.RunSQL "DELETE * FROM TempSymbol"
    DoCmd.SetWarnin gs (True)
    DoCmd.TransferS preadsheet acImport, , "TempSymbol ", "" & myPath & "", True, "" & txtRangeName & ""
    MsgBox "The symbols have been successfully imported."
    strSQL = "SELECT DailyPrice.Symb ol, DailyPrice.Loca teDate, DailyPrice.Mark etPrice, DailyPrice.Pric eFlag " & _
    "FROM TempSymbol, DailyPrice " & _
    "WHERE (((DailyPrice.S ymbol) = [TempSymbol]![Symbol])) " & _
    "ORDER BY DailyPrice.Symb ol, DailyPrice.Loca teDate;"
    qdf.SQL = strSQL
    'check this out...maybe delete it later
    myPath = txtFilePath & txtExportName
    DoCmd.TransferS preadsheet acExport, , "qryMultiSelect ", "" & myPath & ""
    MsgBox "The table have been successfully exported to: " & myExport & "."
    DoCmd.Close
    qdf.Close
    Set db = Nothing
    Set qdf = Nothing
    Exit Sub............ ......
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32662

    #2
    Code:
    Set qdf = CurrentDb.QueryDefs("qryDummy")
    qdf.SQL = strSQL
    DoCmd.OpenQuery ("qryDummy")
    First line sets qdf to point to CurrentDb.Query Defs("qryDummy" ).
    Second line amends the embedded SQL within qdf ==> CurrentDb.Query Defs("qryDummy" ) to be strSQL.
    Third line executes the amended query.

    This is an extended way of executing SQL code within VB - similar to DoCmd.RunSQL().

    HOWEVER, it is possible that certain QueryDef properties have been set in qryDummy which would effect how the query runs - so may not execute in EXACTLY the same way.

    I use a similar method when I want to execute SQL code which needs a longer TimeOut for instance.

    Comment

    • ineedahelp
      New Member
      • Sep 2006
      • 98

      #3
      thank you for your explanation...s o if I understand correctly, when I create a statement

      strSQL = "SELECT DailyPrice.Symb ol, DailyPrice.Loca teDate, DailyPrice.Mark etPrice, DailyPrice.Pric eFlag " & _
      "FROM TempSymbol, DailyPrice " & _
      "WHERE (((DailyPrice.S ymbol) = [TempSymbol]![Symbol])) " & _
      "ORDER BY DailyPrice.Symb ol, DailyPrice.Loca teDate;"
      qdf.SQL = strSQL
      DoCmd.TransferS preadsheet acExport, , "qryDummy", "" & myPath & ""

      I am creating a query "strSQL" and changing the definition already in place by qryDummy and then executing qryDummy with this NEW definition of strSQL. Am I close?!!! Thanks again for your help!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        Yes close.

        You cannot leave out the line
        Code:
        Set qdf = CurrentDb.QueryDefs("qryDummy")
        though (unless that is entered earlier somewhere).

        You are not 'creating' a query here as such : You are updating a previously existing query in the database, called 'qryDummy'.
        What you're doing is changing the SQL part of the query to what you've just set up in strSQL.
        The TransferSpreads heet line will, however, execute the SQL found in strSQL (and save it to myPath), but only because you transferred it to qdf.SQL or qryDummy.SQL (<== that is not valid code - it is there merely for illustrative purposes).

        Comment

        • ineedahelp
          New Member
          • Sep 2006
          • 98

          #5
          Thank you very much for the explanation...I get it now!

          Comment

          Working...