ADO Insert Query

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I've managed to query my MS Access database through Excel using the ADO method.

I'd like to learn how the syntax differs for an INSERT query; specifically when inserting values from a form.

Here is the code I use for a SELECT query:

Code:
Set myConnection = New ADODB.Connection
Set myResults = New ADODB.Recordset
FilePath = "L:\Production - Historical\db\prod_history.accdb"

With myConnection
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Open "Data Source = """ & FilePath & """"
End With

Dim sSQL As String
   sSQL = "SELECT [a_Field] FROM [a_Table] GROUP BY [a_Field];"

With myResults
    .Source = sSQL
    .ActiveConnection = myConnection
    .CursorLocation = adUseClient
    .CursorType = adOpenForwardOnly
    .LockType = adLockReadOnly
    .Open
End With

varData = myResults.GetRows
Sheets("SHEET_1").myComboBox.List = Application.Transpose(varData)

myResults.Close

And here is the insert query i'd like to execute:

Code:
Dim sSQL As String
   sSQL = "INSERT INTO [tblComments] ([WH_IDX], [CommentDate], [Comment], [User]) " _
 & "VALUES (" & WH_KEY & "," & frmAddComment.txtDate.Value & "," & frmAddComment.txtComment.Value & "," & frmAddComment.txtUser.Value & ");"
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Here's a shot:

Code:
Set myConnection = New ADODB.Connection
FilePath = "L:\Production - Historical\db\prod_history.accdb"

With myConnection
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Open "Data Source = """ & FilePath & """"
    .CommandText = "INSERT INTO [tblComments] ([WH_IDX], [CommentDate], [Comment], [User]) VALUES (" & Well_KEY & "," & frmAddComment.txtDate.Value & "," & frmAddComment.txtComment.Value & "," & frmAddComment.txtUser.Value & ");"
    .Execute
End With

If CBool(myConnection.State And adStateOpen) = True Then
    myConnection.Close
    Set myConnection = Nothing
End If

I get an "Argument not optional" error on the .Execute line... What am I missing?
 
Upvote 0
One more try:
Code:
Set myConnection = New ADODB.Connection
FilePath = "L:\Production - Historical\db\prod_history.accdb"

Dim sSQL As String
sSQL = "INSERT INTO [tblEngineerComments] ([WH_IDX], [CommentDate], [EngComment], [Engineer]) VALUES (" & Well_KEY & "," & frmAddComment.txtDate.Value & "," & frmAddComment.txtComment.Value & "," & frmAddComment.txtEngineer.Value & ");"

With myConnection
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Open "Data Source = """ & FilePath & """"
    .Execute sSQL, adExecuteNoRecords
End With

If CBool(myConnection.State And adStateOpen) = True Then
    myConnection.Close
    Set myConnection = Nothing
End If

I get a "no value given for one or more parameters" error, which still points to the .Execute line. -- Thoughts?
 
Upvote 0
Sorry for the multiple posts, but editing is not allowed after 10 minutes.

I resolved the issue, which (with the new code) was actually an error in the SQL statement. I needed quote around the string values, so the insert query now looks like this:

Code:
sSQL = "INSERT INTO tblComments (WH_IDX, CommentDate, [Comment], [User]) VALUES (" & W_KEY & "," & frmAddComment.txtDate.Value & "," & Chr(34) & frmAddComment.txtComment.Value & Chr(34) & "," & Chr(34) & frmAddComment.txtUser.Value & Chr(34) & ");"
 
Upvote 0
Sorry I went offline. So I take it all is well and it works? Great! And you did a great job pulling it together. Well done!
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top