excel - Using Cell reference in SQL query -
i using below vba command extract info using sql query using cell reference filter.
public sub run() dim sql string dim connected boolean dim server_name string dim database_name string dim allocation string sheets("perc").select range("a6").select selection.currentregion.select selection.clearcontents ' our query sql = "select segmentation_id,mpg,sum(segmentation_percent) perc " & _ "from dbo.hfm_allocation_ratio " & _ "where segmentation_id = " & sheets("perc").range("a1").value & " " & _ "group segmentation_id,mpg order mpg" ' connect database server_name = sheets("general").range("d1").value database_name = sheets("general").range("g1").value connected = connect(server_name, database_name) if connected ' if connected run query , disconnect phone call query(sql) phone call disconnect else ' couldn't connect msgbox "could not connect!" end if end sub
but when run macro, it's showing runtime error
invalid column name alo0000274
here alo0000274
filter set in a1
.
kindly help prepare error.
add quotes where
clause (you're passing text value, need include quotes):
sql = "select segmentation_id,mpg,sum(segmentation_percent) perc " & _ "from dbo.hfm_allocation_ratio " & _ "where segmentation_id = '" & sheets("perc").range("a1").value & "' " & _ "group segmentation_id,mpg order mpg"
(note single quotes '
enclose value want filter)
excel vba excel-vba
No comments:
Post a Comment