Results 1 to 6 of 6
  1. #1
    sanal is offline Advanced Beginner
    Windows 10 Access 2007
    Join Date
    Feb 2018
    Posts
    43

    Convertion of sql view of append query to vba points to avoid syntax errors


    How to avoid syntax errors in the convertion of sql view of an append query to vba for using in a module and example vba code .

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    This is a very broad question so difficult to give a focused answer.
    See if my sql to vba converter utility can help with the task
    https://www.access-programmers.co.uk...d.php?t=293372
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    Peter M is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2017
    Posts
    67
    Agree the question is very broad. But since I do this daily, here is my standard approach. I break it up into chunks and make sure everything is explicitly declared.
    (And first write the query using the Wizard, then copy from the SQL view. Depending on the size and complexity, I will use Excel to parse the strings.)
    Option Explicit
    Dim sqlInsert, sqlSelect, sqlFrom, sqlGroupBy, sqlWhere, sqlHaving, sqlEnd, sqlStr As String

    public function sql_Append_CSV(varSourceFile) as string

    sqlInsert = sqlInsert & "INSERT INTO " & destTable & " ("
    sqlInsert = sqlInsert & " RECORD_TYPE,"
    sqlInsert = sqlInsert & " Paid_DT,"
    sqlInsert = sqlInsert & " Transaction_NUM,"
    etc.
    sqlInsert = sqlInsert & ")"

    sqlSelect = "Select "
    sqlSelect = sqlSelect & " [CSV_LINK].RECORD_TYPE,"
    sqlSelect = sqlSelect & " [CSV_LINK].PaiD_DT,"
    sqlSelect = sqlSelect & " [CSV_LINK].Transaction_NUM,"
    etc.
    sqlFrom = " FROM " & varSourceFile & " AS CSV_LINK "

    sqlStr = sqlInsert & sqlSelect & sqlFrom ";"
    sql_Append_CSV = sqlStr
    end function

    Watch for SPACES between all your string pieces and NO TRAILING COMMAS (e.g. final select statement).
    Last edited by Peter M; 04-10-2018 at 10:35 AM. Reason: typo

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by Peter M View Post
    Option Explicit
    Dim sqlInsert, sqlSelect, sqlFrom, sqlGroupBy, sqlWhere, sqlHaving, sqlEnd, sqlStr As String
    Hmmmmmm...

    Something to be aware of:
    In VBA, unlike some other programming languages, variables should/must be EXPLICITLY declared.
    In the above Dim statement, the variables sqlInsert, sqlSelect, sqlFrom, sqlGroupBy, sqlWhere, sqlHaving and sqlEnd are declared as type Variant......
    Only variable sqlStr is declared as type String.

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    removed duplicate info
    ssanfu beat me to it again...
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Peter M is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2017
    Posts
    67
    Quote Originally Posted by ssanfu View Post
    Hmmmmmm...

    Something to be aware of:
    In VBA, unlike some other programming languages, variables should/must be EXPLICITLY declared.
    In the above Dim statement, the variables sqlInsert, sqlSelect, sqlFrom, sqlGroupBy, sqlWhere, sqlHaving and sqlEnd are declared as type Variant......
    Only variable sqlStr is declared as type String.
    Ouch! Thank you; this might explain some weird problems I've had where I was sure I had already stated the variable is a string so stop treating it like a number (and vice versa).

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

Similar Threads

  1. VBA Syntax For Append Query
    By jo15765 in forum Programming
    Replies: 12
    Last Post: 04-10-2017, 10:39 AM
  2. Replies: 3
    Last Post: 02-07-2016, 10:48 AM
  3. Replies: 4
    Last Post: 11-15-2015, 10:15 AM
  4. Replies: 2
    Last Post: 10-20-2012, 12:36 PM
  5. Maximun View; How to Avoid
    By cassidym in forum Database Design
    Replies: 1
    Last Post: 08-12-2010, 06:59 PM

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