Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to this and I need to insert data from a word form into my access table. I am using word and access 2016
VB
"INSERT INTO Shippers" _
& "(Company, BusinessPhone) " _
& "VALUES (" _
& strCompany & ", " _
& strBusinessPhone & ")"
Debug.Print strSQL
'Substitute path and connection string with DSN if available.
strPath = "C:\Users\Admin\Documents\northwind.accdb"
strConnection = "Provider=Microsoft.ace.OLEDB.16.0;" _
& "Data Source = " & strPath
Debug.Print strConnection
Set cnn = New ADODB.Connection
cnn.Open strConnection
cnn.Execute strSQL, lngSuccess 'this is the problem
cnn.Close


What I have tried:

I comment the line and there is no error but it does not insert the data
Posted
Updated 10-Oct-17 10:32am
v2
Comments
cvogt61457 27-Sep-17 22:03pm    
What do you see with the debug statements?
What value is returned in lngSuccess?
Can you retrieve data correctly? (to test that you're actually reaching the database)

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fixing that will probably also cure the problem you have noticed ... which is caused by the company name being assumed by SQL to be a variable name rather than a string.
 
Share this answer
 
In addition to solution 1 by OriginalGriff...

You should use Execute method[^] for ADODB.Command instead of Execute method[^] for ADODB.Connection. A main difference is that in 1. case you're able to pass parameters.

Please, read this: Parameters Collection (ADO) | Microsoft Docs[^] to find out how to pass parameters into ADODB.Command.

A CommandText should looks like:
VB
Dim oCommand As ADODB.Command, oRst As ADODB.Recordset
Dim sCommand As String

sCommand = "INSERT INTO Shippers(Company, BusinessPhone) VALUES(@pCompany, @pBPhone);"
Set oCommand = New ADODB.Command
With o Command
    .CommandType = 1 'adCmdText
    .CommandText = sCommand
    .Parameters.Append ccmd.CreateParameter("@pCompany", adVarChar, adParamInput, 150, "CompanyNameHere")   
    .Parameters.Append ccmd.CreateParameter("@pBPhone", adVarChar, adParamInput, 150, "PhoneNumberHere") 
    Set oRst = .Execute
End With


Note: Above code comes directly from my head. So, it can contains bugs (but i believe it doesn't).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900