Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    brigitteAT is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    61

    transform sql to vba ... issue with new line

    My probles is solved in the meantime! Thank you all so much!!!

    I want several add on queries and procedures (private subs) to run automatically with one double-click

    thus I tried to copy the sql into the procedure, but I have no idea how to create the (necessary ?) new lines in vba


    it is probably quite simple but I failed:

    the original sql looks like this (new lines start at "INSERT", "SELECT" and "FROM"

    INSERT INTO FL ( FLDatum, FLJAHR, FLBUART, FLBELEGNR, FLText, FLKONR, FLKTO, FLKUC, FLGKb, FLGKn, FLKb,
    FLKn, FLGKKTO, FLGKONR, FLPeriode )
    SELECT [Abfrage MI-VS-Summe].FJDatum, [Abfrage MI-VS-Summe].FJJahr, [Abfrage MI-VS-Summe].FJBUART,
    [Abfrage MI-VS-Summe].FJBelegNr, [Abfrage MI-VS-Summe].FJText, [Abfrage MI-VS-Summe].FJONR, [Abfrage
    MI-VS-Summe].FJKTO, [Abfrage MI-VS-Summe].FJUC, [Abfrage MI-VS-Summe].SummevonFJHB, [Abfrage MI-VS-
    Summe].SummevonFJHB, [Abfrage MI-VS-Summe].SummevonFJsb, [Abfrage MI-VS-Summe].SummevonFJsb,
    [Abfrage MI-VS-Summe].FJGKKTO, [Abfrage MI-VS-Summe].FJGKONR, [Abfrage MI-VS-Summe].fjperiode
    FROM [Abfrage MI-VS-Summe];


    I found out, that with "_" I can cut the line and start a new one in vba, but this does not work for where a new line seems to be necessary....
    thus I tried with vbfl an char(10), chr(13) ... but always get a message that something is missing...

    DoCmd.RunSQL(INSERT INTO FL ( FLDatum, FLJAHR, FLBUART, FLBELEGNR, FLText, FLKONR, FLKTO, FLKUC, FLGKb, _
    FLGKn, FLKb, FLKn, FLGKKTO, FLGKONR, FLPeriode ) & char(10) & _
    SELECT [Abfrage MI-VS-Summe].FJDatum, [Abfrage MI-VS-Summe].FJJahr, [Abfrage MI-VS-Summe].FJBUART, _
    [Abfrage MI-VS-Summe].FJBelegNr, [Abfrage MI-VS-Summe].FJText,[Abfrage MI-VS-Summe].FJONR, _
    [Abfrage MI-VS-Summe].FJKTO, [Abfrage MI-VS-Summe].FJUC, [Abfrage MI-VS-Summe].SummevonFJHB, _
    [Abfrage MI-VS-Summe].SummevonFJHB,[Abfrage MI-VS-Summe].SummevonFJsb, _
    [Abfrage MI-VS-Summe].SummevonFJsb, [Abfrage MI-VS-Summe].FJGKKTO, [Abfrage MI-VS-Summe].FJGKONR, _
    [Abfrage MI-VS-Summe].fjperiode & & char(10) & _
    FROM [Abfrage MI-VS-Summe]

    could somebody please give me a hint?
    thanks in advance!!!
    Last edited by brigitteAT; 01-24-2021 at 01:01 PM.

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    I have copied code from M$ pages where the line continuation character ( _ ) was used, only to have it fail and I could not figure out the problem - so I never use it. I use this appraoch
    Code:
    Dim strSql As String
    
    strSql = "INSERT INTO FL (FLDatum, FLJAHR, FLBUART, FLBELEGNR, FLText, FLKONR, FLKTO, "
    strSql = strSql & "FLKUC, FLGKb, FLGKn, FLKb, FLKn, FLGKKTO, FLGKONR, FLPeriode) "
    strSql = strSql & "SELECT [Abfrage MI-VS-Summe].FJDatum, [Abfrage MI-VS-Summe].FJJahr, "
    strSql = strSql & "[Abfrage MI-VS-Summe].FJBUART, [Abfrage MI-VS-Summe].FJBelegNr, "
    strSql = strSql & "[Abfrage MI-VS-Summe].FJText, [Abfrage MI-VS-Summe].FJONR, [Abfrage MI-VS-Summe].FJKTO, "
    strSql = strSql & "[Abfrage MI-VS-Summe].FJUC, [Abfrage MI-VS-Summe].SummevonFJHB, "
    strSql = strSql & "[Abfrage MI-VS-Summe].SummevonFJHB, [Abfrage MI-VS-Summe].SummevonFJsb, "
    strSql = strSql & "[Abfrage MI-VS-Summe].SummevonFJsb, [Abfrage MI-VS-Summe].FJGKKTO, "
    strSql = strSql & "[Abfrage MI-VS-Summe].FJGKONR, [Abfrage MI-VS-Summe].fjperiode "
    strSql = strSql & "FROM [Abfrage MI-VS-Summe];"
    I put my spaces at the end of the line, some prefer the start. Key is to adopt one approach and stick with it. As you can see, I also create the variable(s) that will contain the string. I usually use the Execute method of the CurrentDb object to execute the sql. You didn't ask how to run the sql so I don't know if you got that figured out.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    You can't just add & to the sql?
    You would need to make it a string as Micron has done. Then you can append a line feed or whatever.
    I put my space at the start as easier to see for me?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,205
    You may find my vba/sql conversion utility useful http://www.mendipdatasystems.co.uk/s...ain/4594398120
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  5. #5
    brigitteAT is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    61
    GREAT!!!! thank you so much! it does what I want it to do!
    hopefully that will also work with my next steps ... if not, be sure I'll be back ... :-):-):-)

    I'working completely working by Trial and Error, thus I'm very grateful to get so much help here in this community!!!

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    I didn't see the DoCmd.RunSql in there - I took it as the sql itself. So it would never compile because there are no quotes. Concatenating vb line characters only made it worse. There is also a missing closing parenthesis. What might have worked
    Code:
    DoCmd.RunSQL("INSERT INTO FL (FLDatum, FLJAHR, FLBUART, FLBELEGNR, FLText, FLKONR, FLKTO, FLKUC, FLGKb, " _
    & "FLGKn, FLKb, FLKn, FLGKKTO, FLGKONR, FLPeriode) SELECT [Abfrage MI-VS-Summe].FJDatum, " _
    & "[Abfrage MI-VS-Summe].FJJahr, [Abfrage MI-VS-Summe].FJBUART, [Abfrage MI-VS-Summe].FJBelegNr, " _
    & "[Abfrage MI-VS-Summe].FJText,[Abfrage MI-VS-Summe].FJONR, [Abfrage MI-VS-Summe].FJKTO, " _
    & "[Abfrage MI-VS-Summe].FJUC, [Abfrage MI-VS-Summe].SummevonFJHB, [Abfrage MI-VS-Summe].SummevonFJHB, " _
    & "[Abfrage MI-VS-Summe].SummevonFJsb, [Abfrage MI-VS-Summe].SummevonFJsb, [Abfrage MI-VS-Summe].FJGKKTO, " _
    & "[Abfrage MI-VS-Summe].FJGKONR, [Abfrage MI-VS-Summe].fjperiode FROM [Abfrage MI-VS-Summe])"
    Last edited by Micron; 01-24-2021 at 10:56 AM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    brigitteAT is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    61
    my first query now works with the method of "Micron" :-)
    I'm going to try your method with my next query

  8. #8
    brigitteAT is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    61
    my first query now works with the method of "Micron" :-)
    I'm now trying the method of Isladogs with my next query, but can't yet test it, as it doesn't accept characters which I defined in the Query

    SELECT ..... , Abfragekriterien.Monat AS FJBelegNr, "VS " & [Monat] & "/21 " & [poskurz] AS FJText, .......

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    Your issue is likely incorrect concatenation and/or lack of proper quoting. The complete attempt would be more revealing/useful than what you posted just now, but if you do, please use code tags (# on forum toolbar) to encapsulate your code as I did. Makes it much easier to read. Copy, click # and paste.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,205
    Quote Originally Posted by brigitteAT View Post
    my first query now works with the method of "Micron" :-)
    I'm now trying the method of Isladogs with my next query, but can't yet test it, as it doesn't accept characters which I defined in the Query
    SELECT ..... , Abfragekriterien.Monat AS FJBelegNr, "VS " & [Monat] & "/21 " & [poskurz] AS FJText, .......
    Replace the double quotes with single quotes.
    Code:
    SELECT ..... , Abfragekriterien.Monat AS FJBelegNr, 'VS ' & [Monat] & '/21 ' & [poskurz] AS FJText, .......
    Your query should still work fine and the conversion should also work without issue
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  11. #11
    brigitteAT is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    61
    thank you so much Isladogs! your Method looks easier in the end, but I found out, that for my purpose it is better to use the Method of Micron, as for my trial and error method, having the docmd. in one line only, I can test the whole procedure easier by just excluding one line of comand which do already work !

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Again, you are just concatenating to non strings?

    Code:
    "SELECT ..... , Abfragekriterien.Monat AS FJBelegNr, " &  "VS " & [Monat] & "/21 " & [poskurz]  & " AS FJText,"
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,205
    Quote Originally Posted by brigitteAT View Post
    thank you so much Isladogs! your Method looks easier in the end, but I found out, that for my purpose it is better to use the Method of Micron, as for my trial and error method, having the docmd. in one line only, I can test the whole procedure easier by just excluding one line of comand which do already work !
    Sorry but you've lost me....
    Converting queries to SQL takes practice to get right but, once you have a method that works for you, it becomes straightforward to do.
    My utility is mainly useful whilst in the learning stage...
    However you approach the conversion, you cannot use double quotes in the middle of a SQL statement as Access will treat that as an endpoint.
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  14. #14
    brigitteAT is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jan 2021
    Posts
    61
    Thank you Welshgasman!!! Isladogs has already given me the solution and I marked the thread as solved... doesn't that work!?!?
    sorry but I don't understand what you mean by
    "conctenating to non strings" ?!?!

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Quote Originally Posted by brigitteAT View Post
    Thank you Welshgasman!!! Isladogs has already given me the solution and I marked the thread as solved... doesn't that work!?!?
    sorry but I don't understand what you mean by
    "conctenating to non strings" ?!?!
    By that I mean you have not surrounded a portion of the sql statement in quotes

    Select whatever from whatever table & " this is my extra text that I want added"

    The Select portion is not surrounded by quotes.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Transform Data
    By arvive in forum Access
    Replies: 4
    Last Post: 08-14-2019, 07:23 AM
  2. Best way to transform a table
    By siema24 in forum Access
    Replies: 5
    Last Post: 01-27-2017, 03:01 AM
  3. Line spacing issue in Access 2010 reports
    By rebfein in forum Reports
    Replies: 2
    Last Post: 07-05-2016, 12:07 PM
  4. Timer event vs. longrunning line: concurrency issue
    By GlistEngineering in forum Programming
    Replies: 1
    Last Post: 07-25-2012, 05:40 PM
  5. Line-Bar Chart Issue
    By santoshpk in forum Reports
    Replies: 0
    Last Post: 03-17-2009, 02:47 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