Results 1 to 8 of 8
  1. #1
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    Better formatting for SQL Insert Into Query

    The following query works fine. Now I am trying to clean up the formatting because its difficult to read or find anything. This is far better than what I had. But I know there is more.

    Code:
    stSQL = "INSERT INTO dbo_Property" & _
         "( County, kPIN, TownshipNO, TownshipNM, CycleID, CycleName, CountyClass, PropertyDesc, LevelofAssess, Type, OpenFile, KattyAssigned, Paddress1, Paddress2, Pcity, PState, Pzip5, TaxCode)" _
        & "VALUES ('" & stCnty & "','" _
        & stKPIN & "','" _
        & stTwnNo & "','" _
        & stSelTwn & "','" _
        & stCycleID & "','" _
        & stCycleNM & "','" _
        & stSelClas & "','" _
        & stDesc & "','" _
        & stLoa & "','" _
        & stTypeNM & "','" _
        & stOpenFile & "','" _
        & stKattAs & "','" _
        & stPaddress1 & "','" _
        & stPaddress2 & "','" _
        & stPCity & "','" _
        & stPState & "','" _
        & PZip5 & "','" _
        & stTaxCode & "');"
    Somewhere I saw an example where you were able to place the "Field" and then the "Value" (Variable) on one line. So each line contained the field name and then the corresponding variable and it was so easy to read.

    Field "County" Value "stCounty"

    I cant remember where to find it. I have several queries like this. Can anyone help. Readability is the goal. If there are other suggestions on making it readable I would be interested in hearing them. But my main goal now is to get each field and variable on one line.



    Thanks

    Phred

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I can't think of a way to get an append query that way. An update query is structured that way:

    UPDATE TableName
    SET Field1 = 123, Field2 = 'ABC'
    WHERE...

    Appending records with a recordset would also be structured that way:

    rs.AddNew
    rs!Field1 = 123
    rs!Fiedl2 = "ABC"
    rs.Update
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    That's it! So it would look like this:

    stSQL = "INSERT INTO dbo_Property" & _
    Set County = '" & stCnty & "' _
    kPIN = '" & stKPIN & "' _
    TownshipNo = '" & stTwnNo & "' _

    Continues through all fields. There would be no WHERE statement.

    Does this look correct?

    Thanks, Phred

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No. Like I said, I don't think you can do that with an append query. An update query edits an existing record(s), an append query creates new records.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    Rats. I thought it would work for all action queries. My bad luck, but thanks.
    I reverted to the query at the top as it works fine. Can the field names County, kPIN, TownshipNM be broken down vertically?

    County
    kPIN
    TownshipNM

    It would be easier to read if both were in vertical stacks. I am not sure how the syntax would look.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sure, you just can't get them side-by-side with the value. You could switch to a recordset and get that look.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    Double rats. I tried to split the fields each to one line. VBA spits out a warning that there are too many One-Line-Splits so I guess there is a limit to it.

    But thanks for your help on the above. I will mark this solved.

    Phred

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem. It's less efficient by a micron, but you can do it this way instead of with line continuation and not run into the limit:

    ...
    strSQL = strSQL & stCycleNM & "','"
    strSQL = strSQL & stSelClas & "','"
    ...
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Need some help debugging an INSERT INTO query
    By kagoodwin13 in forum Queries
    Replies: 2
    Last Post: 05-08-2015, 10:12 AM
  2. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  3. Need to add a space in an insert query
    By jax1000rr in forum Queries
    Replies: 2
    Last Post: 03-06-2013, 03:49 PM
  4. insert query copy gone bad
    By bbxrider in forum Access
    Replies: 0
    Last Post: 04-26-2011, 01:35 AM
  5. INSERT INTO query in VBA
    By jgelpi16 in forum Programming
    Replies: 5
    Last Post: 09-17-2010, 02:19 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