VBA to amend SQL Query

boroyj

New Member
Joined
Jan 15, 2015
Messages
11
Hi all,

I am trying to retrieve data from a SQL connection in an excel workbook. Using MS query and the parameters options I am able to parse a single item number and have the query bring back that relevant information from the database.

I would like it to bring back the information for multiple item numbers in one go, however, I cannot get that to happen.

I've tried pulling all of the numbers into the format that would work in a normal IN statement for example:

Ref
1234
4567
7890

is presented as ('1234',4568','7890') and pointed the parameter cell reference to that, but keep getting '[Microsoft][ODBC SQL Server Driver]String data, right Truncate' error.

Is there a VBA method that can take the multiple rows of item numbers and amend the query to look them up?

Thanks in advance

Rob
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Is this a copy and paste error??

('1234',4568','7890')

You're missing an apostrophe.

Otherwise please share your VBA for us to read.
 
Upvote 0
Is this a copy and paste error??

('1234',4568','7890')

You're missing an apostrophe.

Otherwise please share your VBA for us to read.

yes, it is missing the ' from where I typed it sorry.

I don't have any VBA to share, I was hoping that someone had the same issue sorry.

If it's unclear above - what I am essentially trying to do is take the item numbers which would be listed in one column, then format them in a cell or with VBA code so that:

AB
1Item_numberSQL CRITERIA
21234('1234','4567','7890')
34567
47890

<tbody>
</tbody>


Can be converted to a format that is usable in a SQL statement that is embedded already. for example the above is converted to '1234','4567' to be used in

Select *
from products
where item_number in B2

Thanks

Rob
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,948
Members
449,198
Latest member
MhammadishaqKhan

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