ADO Insert Query with Parameter(s)

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I've built an ADO function in VBA to handle queries to an Access database. It's setup to process queries with 0 - 2 parameters.

I'm trying to expand it to handle an Insert as well. When the Insert SQL and parameter are passed to the function, Excel locks up.

Where am I going wrong with handling the INSERT query? Also included is the SQL statement I'm trying to execute. It doesn't have the VALUES part - which isn't necessary in Access? If I have an error in my SQL statement, how might I alter it to work with ADO?

Thanks!

ADO Function:
Code:
Public Function adoQuery(sSQL, Optional ParamValue As Variant, Optional ParamValue2 As Variant, Optional Insert As Boolean)

Set myConnection = New ADODB.Connection
Set myResults = New ADODB.Recordset
Dim FilePath As String
    FilePath = "L:\my_db.accdb"

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


If IsMissing(ParamValue) = False Then

'Query with parameter(s):
    Dim cmd As ADODB.Command
    Set cmd = CreateObject("ADODB.Command")
    Dim param As ADODB.Parameter

    cmd.ActiveConnection = myConnection
    cmd.CommandText = sSQL

    Select Case VarType(ParamValue)
        Case Is <= 6
            paramType = adInteger 'number
            paramSize = adInteger
        Case Is = 7
            paramType = adDate  'date
            paramSize = adDBDate
        Case Else
            paramType = adVarChar  'text
            paramSize = adChar
    End Select
    
    Set param = cmd.CreateParameter("param1", paramType, adParamInput, paramSize, ParamValue)
        
        cmd.Parameters.Append param
            Set param = Nothing
            
    If IsMissing(ParamValue2) = False Then   '(2) parameters passed to function:
        Dim param2 As ADODB.Parameter
    
        Select Case VarType(ParamValue2)
            Case Is <= 6
                paramType = adInteger 'number
                paramSize = adInteger
            Case Is = 7
                paramType = adDate  'date
                paramSize = adDBDate
            Case Else
                paramType = adVarChar  'text
                paramSize = adChar
        End Select
        
        Set param2 = cmd.CreateParameter("param2", paramType, adParamInput, paramSize, ParamValue2)
            
            cmd.Parameters.Append param2
                Set param2 = Nothing
     End If

    'IF INSERT, execute differently:
    If Insert = True Then

        'Proceed with INSERT:
        With cmd
           .Execute sSQL, , adCmdText + adExecuteNoRecords
        End With
    Else
        'Ready to go
        Set myResults = cmd.Execute()
    End If
    
    
Else

'Query without parameter:
    With myResults
        .Source = sSQL: .ActiveConnection = myConnection: .CursorLocation = adUseClient: .CursorType = adOpenForwardOnly: .LockType = adLockReadOnly: .Open
    End With

End If

End Function

Function call:
Code:
adoQuery, sSQL, myParamValue1, , True

SQL to execute (includes variables from program):
Code:
        sSQL = "INSERT INTO tblTempGroupNorm ( Username, Tab_Name, Click_Date, Criteria1, Item_KEY, ReadingDate, Prod1, Prod2, Prod3, TimeDay ) " _
            & "SELECT " & Chr(34) & strname & Chr(34) & " AS Username," _
            & Chr(34) & "Group" & Chr(34) & " AS Tab_Name," _
            & strDate & " AS Click_Date," _
            & Chr(34) & Group & Chr(34) & " AS Criteria1, " _
            & "[Group_Daily].Item_KEY, [Group_Daily].ReadingDate, [Group_Daily].Prod1, [Group_Daily].Prod2, [Group_Daily].Prod3, " _
                & "(SELECT COUNT(Table1A.ReadingDate) " _
                & "FROM [Group_Daily] AS Table1A " _
                & "WHERE [Table1A].[ReadingDate] <= [Group_Daily].[ReadingDate] " _
                & "AND [Table1A].[Item_KEY]=[Group_Daily].[Item_KEY]) AS TimeDay " _
                & "FROM tblProperties INNER JOIN [Group_Daily] ON tblProperties.WH_IDX = [Group_Daily].Item_KEY " _
                & "WHERE ((tblProperties." & db_field & ") = '" & Group & "')" _
                & "ORDER BY [Group_Daily].ReadingDate;"
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Got it to work; it was the query slowing things down and locking up Excel.

Still could use some feedback on any improvements or efficiencies to adoQuery function
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,233
Members
449,092
Latest member
SCleaveland

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