Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    May 2001
    Location
    Oxfordshire
    Posts
    456
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SQL in code (2003 (SP2))

    I'm trying to put a query result/value into a variable, to be used when a query is created. Putting the SQL into the variable does not produce an error, but neither does the created query work. Ideas please

    Oh, here's some of the code;


    'gets the last record from the target table
    strMax = "SELECT Max(tblEIBEREL0.EISNO) AS MaxOfEISNO FROM tblEIBEREL0;"

    Set qBEDatabase = CurrentDb()
    qBEQueryName = "qryqBE"

    'Test for the existance of pqryqBE in Database. Delete object if exists,
    'and refresh the Querydefs Collection

    If ObjectExists("Queries", qBEQueryName) = True Then
    qBEDatabase.QueryDefs.Delete qBEQueryName
    qBEDatabase.QueryDefs.Refresh
    End If

    'enters the SQL query
    Set qBEQueryDef = qBEDatabase.CreateQueryDef(qBEQueryName)

    qBESQLString = " INSERT INTO tblEIBEREL0 ( EISNO, PRESS, CUSTNO, [SECTION], DIESEQ, ARTSEQ, ALLOY ) "
    qBESQLString = qBESQLString & "SELECT pqryEIBEREL0.EISNO, pqryEIBEREL0.PRESS, pqryEIBEREL0.CUSTNO, "
    qBESQLString = qBESQLString & " pqryEIBEREL0.SECTION, pqryEIBEREL0.DIESEQ, pqryEIBEREL0.ARTSEQ, pqryEIBEREL0.ALLOY "
    qBESQLString = qBESQLString & " FROM pqryEIBEREL0 "
    qBESQLString = qBESQLString & " WHERE pqryEIBEREL0.EISNO>'"
    qBESQLString = qBESQLString & strMax
    qBESQLString = qBESQLString & "';"

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: SQL in code (2003 (SP2))

    Does this do what you want?
    <code>
    ...
    qBESQLString = qBESQLString & " WHERE pqryEIBEREL0.EISNO>("
    qBESQLString = qBESQLString & strMax
    qBESQLString = qBESQLString & ");"
    </code>
    Note that I replaced the single quotes ' with parentheses.

    I assume that the code you posted is followed by a line
    <code>
    qBEQueryDef.SQL = qBESQLString</code>

  3. #3
    4 Star Lounger
    Join Date
    May 2001
    Location
    Oxfordshire
    Posts
    456
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: SQL in code (2003 (SP2))

    qBEQueryDef.SQL = qBESQLString
    is the next line.

    And your solution was, once again, spot on.

    Many thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •