Results 1 to 7 of 7
  1. #1
    trpatrick is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    3

    Converting SQL statement to VBA

    In a form I have created a button that will perform an update query and I want to have the query in VBA code instead of creating a special query for a simple function. How do I structure the following SQL statement in VBA code in the On Click event?



    UPDATE Badges SET Badges.Status = "a"
    WHERE (((Badges.Status)="c"));

  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    number of ways - easiest is

    currentdb.execute("UPDATE Badges SET Status = 'a' WHERE Status='c'",dbfailonerror)

    note the conversion of double quotes to single quotes around a and c

  3. #3
    gpeirce is offline Novice
    Windows 8 Access 2007
    Join Date
    Oct 2015
    Location
    Oregon, USA
    Posts
    13
    As someone learning, is this the same as:

    strSQL = "UPDATE Badges SET Badges.Status = 'a' WHERE Badges.Status = 'c' "
    DoCmd.RunSQL strSQL
    Last edited by gpeirce; 10-25-2015 at 06:12 PM. Reason: correct puncuation of sql

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    If you use a string variable
    and assign it

    strSQL = "UPDATE Badges SET Badges.Status = 'a' WHERE Badges.Status = 'c' "


    You can then use

    currentdb.execute strSQL, dbFailonError

    This will give an error message if an error occurs.

  5. #5
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    that is another way - but it will generate prompts - just try it and decide for yourself

    see this link for a comparison of the two methods

    http://www.utteraccess.com/wiki/inde...SQL_vs_Execute

  6. #6
    trpatrick is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    3
    Thank you for your help.

  7. #7
    mathhero is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    6
    to convert SQL to VBA, you can use this program.
    convert QuerySql To VBA_Sql.zip

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  2. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  3. Converting SQL to VBA code
    By Davidyam in forum Access
    Replies: 3
    Last Post: 04-18-2012, 10:29 AM
  4. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  5. Converting mde to mdb
    By Jdejesus in forum Access
    Replies: 2
    Last Post: 04-08-2011, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums