0

I have searched all the boards and can not find were anyone has asked how to do a line break in code for INSERT INTO statement. I have tried many variations, I can seem to get any of them to work. He is an example of my code and what I am trying. I know it is just a misplaced comma, quote or ampersand.

StrSQL = "INSERT INTO Tbl_Data_Shop & _
(ClaimNumber, ExposureNumber, ClaimSuffix, & _
Shop_Name, Shop_StreetAddress, Shop_City, & _
Shop_State, Shop_Zip, Shop_Phone) & _
"Values 
('" & Forms!Frm_Data_Main!TBClaimNumber & "' & _
"'" & Forms!Frm_Data_Main!TBExposureNumber & "' & _
"'" & Forms!Frm_Data_Main!TBClaimSuffix & "'," & _
"'" & TBSShop_Name & "'," & _
"'" & TBSShop_StreetAddress & "'," & _
"'" & TBSShop_City & "'," & _
"'" & TBSShop_State & "'," & _
"'" & TBSShop_Zip & "'," & _
"'" & TBSShop_Phone & "'");"
Erik A
  • 31,639
  • 12
  • 42
  • 67
Tiny1127
  • 37
  • 7

3 Answers3

2

Once again, a classic example to use the industry best practice of parameterization which you can do in MS Access with QueryDefs.Parameters. Beyond protecting against sql injection, you avoid any need to worry about quotes or ampersands with string interpolation and arguably build a more readable and maintainable code block.

Regardless of language (here being VBA), the process involves setting up a prepared SQL statement with placeholders. Then in a different step you bind data values to placeholders for execution.

SQL

Save below as a saved MS Access query (Ribbon > Create > Queries > SQL View). This SQL query uses the PARAMETERS clause (valid in Access SQL dialect) to define placeholders and their types and then uses the placeholders. You can break all the lines you want!

PARAMETERS TBClaimNumberParam TEXT(255), TBExposureNumberParam TEXT(255), 
           TBClaimSuffixParam TEXT(255), TBSShop_NameParam TEXT(255), 
           TBSShop_StreetAddressParam TEXT(255), TBSShop_CityParam TEXT(255),
           TBSShop_StateParam TEXT(255), TBSShop_ZipParam TEXT(255), 
           TBSShop_PhoneParam TEXT(255); 
INSERT INTO Tbl_Data_Shop (ClaimNumber, ExposureNumber, ClaimSuffix,
                           Shop_Name, Shop_StreetAddress, Shop_City, 
                           Shop_State, Shop_Zip, Shop_Phone)
VALUES (TBClaimNumberParam, TBExposureNumberParam, TBClaimSuffixParam,
        TBSShop_NameParam, TBSShop_StreetAddressParam, TBSShop_CityParam,
        TBSShop_StateParam, TBSShop_ZipParam, TBSShop_PhoneParam)

VBA

In this step, you reference the above saved query, mySavedQuery, into a QueryDef object which then has VBA values binded to the query's named parameters (defined in above SQL).

Dim qdef As QueryDef

Set qdef = CurrentDb.QueryDefs("mySavedQuery")

' BIND VALUES TO PARAMETERS
qdef!TBClaimNumberParam = Forms!Frm_Data_Main!TBClaimNumber
qdef!TBExposureNumberParam = Forms!Frm_Data_Main!TBExposureNumber
qdef!TBClaimSuffixParam = Forms!Frm_Data_Main!TBClaimSuffix
qdef!TBSShop_NameParam = TBSShop_Name
qdef!TBSShop_StreetAddressParam = TBSShop_StreetAddress
qdef!TBSShop_CityParam = TBSShop_City
qdef!TBSShop_StateParam = TBSShop_State
qdef!TBSShop_ZipParam = TBSShop_Zip
qdef!TBSShop_PhoneParam = TBSShop_Phone 

' EXECUTE ACTION
qdef.Execute dbFailOnError

Set qdef = Nothing
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • I really like how clean this is. However it is a little over my head. I would love to learn how to use this method. I looked at both of the links you included and did a little searching and still am not sure how to implement this. What is a "Stored Query Object" and how do I create one. Is it just code I add to my main or is it a module? Can you give me a beginners explanation of how to use this. I have several forms that I would like to use this on. – Tiny1127 Sep 28 '18 at 22:39
  • I add some explanation on the two steps. Basically just create a new Access query, here named *mySavedQuery* which is referenced in VBA. – Parfait Sep 29 '18 at 02:34
1

Make each line a string on its own - and correct the commas and parenthesis:

StrSQL = "INSERT INTO Tbl_Data_Shop " & _
"(ClaimNumber, ExposureNumber, ClaimSuffix, " & _
"Shop_Name, Shop_StreetAddress, Shop_City, " & _
"Shop_State, Shop_Zip, Shop_Phone) " & _
"Values (" & _
"'" & Forms!Frm_Data_Main!TBClaimNumber & "'," & _
"'" & Forms!Frm_Data_Main!TBExposureNumber & "'," & _
"'" & Forms!Frm_Data_Main!TBClaimSuffix & "'," & _
"'" & TBSShop_Name & "'," & _
"'" & TBSShop_StreetAddress & "'," & _
"'" & TBSShop_City & "'," & _
"'" & TBSShop_State & "'," & _
"'" & TBSShop_Zip & "'," & _
"'" & TBSShop_Phone & "');"
Gustav
  • 53,498
  • 7
  • 29
  • 55
0

There are missing/misplaced quotation marks and &s . However I would use a prepared statement, for a number of reasons, namely safety and managability .

StrSQL = "INSERT INTO Tbl_Data_Shop & _
(ClaimNumber, ExposureNumber, ClaimSuffix, & _
Shop_Name, Shop_StreetAddress, Shop_City, & _
Shop_State, Shop_Zip, Shop_Phone) & _
 Values ('" & Forms!Frm_Data_Main!TBClaimNumber & "', & _
'" & Forms!Frm_Data_Main!TBExposureNumber & "', & _
'" & Forms!Frm_Data_Main!TBClaimSuffix & "', & _
'" & TBSShop_Name & "', & _
'" & TBSShop_StreetAddress & "', & _
'" & TBSShop_City & "', & _
'" & TBSShop_State & "', & _
'" & TBSShop_Zip & "', & _
'" & TBSShop_Phone & "');"

Try and let us know.

DaniDev
  • 2,471
  • 2
  • 22
  • 29
  • I am still getting a Expected: end of statement error at " Values ('" – Tiny1127 Sep 28 '18 at 21:42
  • After you last update I am getting the end of statement at ( after the first line. – Tiny1127 Sep 28 '18 at 21:46
  • made a few more edits. Your sql statement is rather messy and your vba line breaks are making harder to see what is actually happening. Please try again and let me know. – DaniDev Sep 28 '18 at 21:47
  • I copied and pasted you code and I am still getting an error at the (. If you can suggest an way to clean up the code I am open suggestions. I am self taught so I know my code in not standard practice. I just do my best to make things work. I had it all on one line but it got really hard to read so I inserted the line breaks. This method of inserting date into my table, I am just trying to figure out how to do the line break correctly. – Tiny1127 Sep 28 '18 at 21:55