Results 1 to 4 of 4

Thread: access queries running through vba

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    5

    access queries running through vba

    hi

    i have 3 basic queries in a database and i want to be able to run those when the database is opened. i have tried using VBA but get an error, this is what i have so far -
    VB Code:
    1. Sub run()
    2.  
    3. DoCmd.RunSQL "INSERT INTO [currency] ( currencyName, euroRate )" & _
    4. "SELECT Template.Code, Template.[Rate vs EUR]" & _
    5. "FROM Template " & _
    6. "WHERE (((Template.Code) In ('GBP','USD','HKD')));"
    7.  
    8. DoCmd.RunSQL "INSERT INTO [currency] ([currencyName], [euroRate])" & _
    9. " VALUES ('EUR', '1.0000');"
    10.  
    11. DoCmd.RunSQL "SELECT currency.currencyName, currency.euroRate, (currency!euroRate/currency_1 !euroRate) AS dollarRate " & _
    12. "FROM [currency], [currency] AS currency_1 " & _
    13. "WHERE (((currency_1.currencyName)='USD'));"
    14.  
    15. End Sub

    the first 2 queries run fine, but then i get an error stating that 'A RunSQL action require an argument consisting of an SQL statement'

    any help much appreciated

    jimmyp

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: access queries running through vba

    It's this bit:
    (currency!euroRate/currency_1 !euroRate)

    Which should be:
    (currency.euroRate/currency_1.euroRate)



    As you are using more than one table (even tho they are the same table), you should specify how they are linked - otherwise you will get a cross-join (all rows in one table will show next to all rows of the other). In this case tho, I guess it is ok as it is.

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: access queries running through vba

    .RunSql isn't used with SELECT statements; you'd need a recordset to do that in code. Alternatively, you can save the query in the Access db and then use .OpenQuery "qryFoo" instead. (I made this same mistake last week in answering another poster).
    Tengo mas preguntas que contestas

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    5

    Re: access queries running through vba

    ah ok thanks for the info

Posting Permissions

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



Click Here to Expand Forum to Full Width