Modifying my parameterized query to select * an entire column

  • This working code lets the user use a click event in Excel/VBA to separately insert values into the same field in SQL. I would like to know how I can rewrite this code in order to have the user insert an entire column into a field when the user does a click event. Please note I am using the (?) as a way to insert a value when the user presses the click button in excel. I know of no other way.

    Sub ClickButton()
    Dim SQL As ADODB.Command
    Set SQL = New ADODB.Command

    SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES (?)"
    SQL.Parameters.Append SQL.CreateParameter("my_id", adVarChar, adParamInput, 50, Value)
    SQL.ActiveConnection = Cn
    SQL.Execute
    End Sub()

  • ???
    Don't understand the question

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster - Friday, November 3, 2017 3:16 PM

    ???
    Don't understand the question

    Sorry about that 🙂  Please have a look at the question now.

  • leavesandmusic - Friday, November 3, 2017 2:58 PM

    ... have the user insert an entire column into a field when the user does a click event. 

    What do you mean by this? Can you give an example?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • leavesandmusic - Friday, November 3, 2017 2:58 PM

    This working code lets the user use a click event in Excel/VBA to separately insert values into the same field in SQL. I would like to know how I can rewrite this code in order to have the user insert an entire column into a field when the user does a click event. Please note I am using the (?) as a way to insert a value when the user presses the click button in excel. I know of no other way.

    Sub ClickButton()
    Dim SQL As ADODB.Command
    Set SQL = New ADODB.Command

    SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES (?)"
    SQL.Parameters.Append SQL.CreateParameter("my_id", adVarChar, adParamInput, 50, Value)
    SQL.ActiveConnection = Cn
    SQL.Execute
    End Sub()

    Obviously, if you continue to use a method that causes Excel to prompt the user for a value, inserting an entire column is no longer practical using the same methodology.   Let's also consider how you determine exactly which column...   Additionally, why are we inserting just one column?   What about duplication of values?  Or does that matter?   I can provide a better methodology, but I need a more complete picture of why you're using a prompted method to begin with, when you can easily determine things like which cell is "currently active" (meaning selected), but as you are then clicking on a button, that might change which cell is active, so I wonder if you might be better off NOT using a click event...   Let's talk about the ENTIRE purpose of the spreadsheet, along with the ultimate objective.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • Thank you for your response. 

    I am constrained by user needs. 

    Therefore I am only inserting one column on a click event due to a user need. 

    The duplication of values does not matter.  However, it must be accomplished using a click event. 

    It would be greatly appreciated if you could modify my code so that I may accomplish this missing link in my code.

  • Do you mean the values in the column for all rows in the spreadsheet?

    Far away is close at hand in the images of elsewhere.
    Anon.

  • I would like only one column: for example,

    Dim arr As Variant
     arr = Sheets("Sheet1").Range("E3:E20").Value

  • leavesandmusic - Friday, November 10, 2017 11:31 AM

    I would like only one column: for example,

    Dim arr As Variant
     arr = Sheets("Sheet1").Range("E3:E20").Value

    Have you tried changing your VBA to produce SQL like this?
    INSERT  MyDatabase.dbo.MyTable ([my_id])
    VALUES (),(),(),(),(), ..., ()

    All you need to do is fill in each bracket with a single value from your spreadsheet. No need for parameters, though you are potentially opening yourself up to SQL Injection by doing this.
    Alternatively (and probably better, because you get more control), create a stored proc in your database which accepts a delimited list of values as a parameter, and let the proc handle it all. Your VBA just has to submit the values to the proc.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Thank you, so I assume the code would look like this?

    Sub ClickButton()
    Dim SQL As ADODB.Command
    Set SQL = New ADODB.Command
    Dim A As Variant
     A = Sheets("Sheet1").Range("E1").Value            
    Dim B As Variant
     B = Sheets("Sheet1").Range("E2").Value          

    SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES (A), (B)"
    SQL.Parameters.Append SQL.CreateParameter("my_id", adVarChar, adParamInput, 50, Value)
    SQL.ActiveConnection = Cn
    SQL.Execute
    End Sub()

    I tested the code.  It does not work. It says "object required" for this line of code: SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES (A), (B)"

  • leavesandmusic - Friday, November 10, 2017 11:51 AM

    Thank you, so I assume the code would look like this?

    Sub ClickButton()
    Dim SQL As ADODB.Command
    Set SQL = New ADODB.Command
    Dim A As Variant
     A = Sheets("Sheet1").Range("E1").Value            
    Dim B As Variant
     B = Sheets("Sheet1").Range("E2").Value          

    SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES (A), (B)"
    SQL.Parameters.Append SQL.CreateParameter("my_id", adVarChar, adParamInput, 50, Value)
    SQL.ActiveConnection = Cn
    SQL.Execute
    End Sub()

    I tested the code.  It does not work. It says "object required" for this line of code: SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES (A), (B)"

    Been a while since I wrote VBA, but I think there are a few problems with your code:

    • You are not putting the values of the variables into the insert statement
    • If the values of the variables are strings, you'll need to put single quotes around them
    • the SqlParameters.Append line can be removed. There are no longer any parameters in this code.

    So instead of 
    SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES (A), (B)"
    I think you'll need something more like (untested)
    SQL.CommandText = "INSERT INTO [MyDatabase].[dbo].[MyTable] ([my_id]) VALUES ('" + A + "'), ('" + B+ "')"

    Obviously, this hard-coded approach works only when you know how many items are being inserted. I'm assuming that you don't & therefore that you'll have to use a loop to build up your VALUES clause.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Thank you for your contribution, but no matter what variation of your code I try, it does not work. 

  • leavesandmusic - Friday, November 10, 2017 1:37 PM

    Thank you for your contribution, but no matter what variation of your code I try, it does not work. 

    OK, but if you expect any further help, telling us that something 'does not work' does not provide enough information to go on.
    I really suggest that you go down the stored procedure route which I suggested for this.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Because of user constraints, I cannot follow through with your stored procedures suggestion.

    I tried to debug your previous code, but no error messages execute when the code is called. 

    It would be greatly appreciated if you can first test the code, make sure that it works, and then offer it as
    a suggestion.  Thank you.

  • leavesandmusic - Friday, November 10, 2017 1:49 PM

    Because of user constraints, I cannot follow through with your stored procedures suggestion.

    I tried to debug your previous code, but no error messages execute when the code is called. 

    It would be greatly appreciated if you can first test the code, make sure that it works, and then offer it as
    a suggestion.  Thank you.

    Sorry, but there are limits to the time which I am prepared to invest setting up test harnesses free of charge.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic. Login to reply