Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Query SQL Server with concatenated text from Excel

Hi,

I've found a module that will let me query SQL Server databases via
ODBC. Below I've created a module which does exactly what I want it to
do using concatenated cells in a worksheet. My issue however, is that
now that I've gotten the code correct, excel tells me it doesn't have
the resources necessary to execute the command. My example below
searches for 2 UPCs, in reality I'll have several hundred, possibly
thousands. If I type these values in manually they work just fine, and
I've even tried executing the code on the SQL server itself with no
luck. Is there a more efficient way to do this? I can't simply query
everything then do a lookup table because the results exceed excels
max dimensions. I'm essentially trying to see if these UPCs are in our
system.

Thanks!

<----begin code------

Sub what()

Dim qt As QueryTable

sqlstring = "select vm.vendor, im.upc_ean, im.description,
vi.vendor_item, vi.case_pack from vendor_item vi, vendor_master vm,
item_master im where vi.record_status < 3 and vm.record_status<3 and
im.record_status<3 and vi.item_id = im.item_id and vi.v_id =vm.v_id
and vi.vi_block_from_pos=0 and vendor='" & Sheet3.Range("A1") & "' and
upc_ean like '" & Sheet3.Range("A3") & ""

connstring = _
"ODBC;DSN=tciinstore;UID=administrator;PWD=!
password;Database=tciinstore"

With Sheet1.QueryTables.Add(Connection:=connstring,
Destination:=Range("A1"), Sql:=sqlstring)
.Refresh
End With

Sheet1.Range("A1").Select
End Sub


<----end code------
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 253
Default Query SQL Server with concatenated text from Excel

Hi Ryan,

To start with , there is a string lenght limitation using this type of query
To overcome this limitation, the string has to be passed as an arraysee
below for microsft function
regards
JY

varSql = StringToArray(strSql)
Range("A2").Select
With Range("A2").QueryTable
.Connection = _
"OLEDB;Provider=MSDAORA.1;Password=XXX;User ID=pdmspub;Data
Source=XXXX"
.CommandType = xlCmdSql
.CommandText = varSql
.Refresh BackgroundQuery:=False
End With

End Sub


Function StringToArray(Query As String) As Variant

Const StrLen = 127 ' Set the maximum string length for
' each element in the array to return
' to 127 characters.
Dim NumElems As Integer
Dim Temp() As String
Dim i
' Divide the length of the string Query by StrLen and
' add 1 to determine how many elements the String array
' Temp should contain, and redimension the Temp array to
' contain this number of elements.

NumElems = (Len(Query) / StrLen) + 1
ReDim Temp(1 To NumElems) As String

' Build the Temp array by sequentially extracting 127
' segments of the Query string into each element of the
' Temp array.

For i = 1 To NumElems
Temp(i) = Mid(Query, ((i - 1) * StrLen) + 1, StrLen)
Next i

' Set the function StringToArray to the Temp array so it
' can be returned to the calling procedure.

StringToArray = Temp

End Function


wrote in message
...
Hi,

I've found a module that will let me query SQL Server databases via
ODBC. Below I've created a module which does exactly what I want it to
do using concatenated cells in a worksheet. My issue however, is that
now that I've gotten the code correct, excel tells me it doesn't have
the resources necessary to execute the command. My example below
searches for 2 UPCs, in reality I'll have several hundred, possibly
thousands. If I type these values in manually they work just fine, and
I've even tried executing the code on the SQL server itself with no
luck. Is there a more efficient way to do this? I can't simply query
everything then do a lookup table because the results exceed excels
max dimensions. I'm essentially trying to see if these UPCs are in our
system.

Thanks!

<----begin code------

Sub what()

Dim qt As QueryTable

sqlstring = "select vm.vendor, im.upc_ean, im.description,
vi.vendor_item, vi.case_pack from vendor_item vi, vendor_master vm,
item_master im where vi.record_status < 3 and vm.record_status<3 and
im.record_status<3 and vi.item_id = im.item_id and vi.v_id =vm.v_id
and vi.vi_block_from_pos=0 and vendor='" & Sheet3.Range("A1") & "' and
upc_ean like '" & Sheet3.Range("A3") & ""

connstring = _
"ODBC;DSN=tciinstore;UID=administrator;PWD=!
password;Database=tciinstore"

With Sheet1.QueryTables.Add(Connection:=connstring,
Destination:=Range("A1"), Sql:=sqlstring)
.Refresh
End With

Sheet1.Range("A1").Select
End Sub


<----end code------



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Query SQL Server with concatenated text from Excel

On Jan 22, 4:48 am, "Jean-Yves" wrote:
Hi Ryan,

To start with , there is a string lenght limitation using this type of query
To overcome this limitation, the string has to be passed as an arraysee
below for microsft function
regards
JY

varSql = StringToArray(strSql)
Range("A2").Select
With Range("A2").QueryTable
.Connection = _
"OLEDB;Provider=MSDAORA.1;Password=XXX;User ID=pdmspub;Data
Source=XXXX"
.CommandType = xlCmdSql
.CommandText = varSql
.Refresh BackgroundQuery:=False
End With

End Sub

Function StringToArray(Query As String) As Variant

Const StrLen = 127 ' Set the maximum string length for
' each element in the array to return
' to 127 characters.
Dim NumElems As Integer
Dim Temp() As String
Dim i
' Divide the length of the string Query by StrLen and
' add 1 to determine how many elements the String array
' Temp should contain, and redimension the Temp array to
' contain this number of elements.

NumElems = (Len(Query) / StrLen) + 1
ReDim Temp(1 To NumElems) As String

' Build the Temp array by sequentially extracting 127
' segments of the Query string into each element of the
' Temp array.

For i = 1 To NumElems
Temp(i) = Mid(Query, ((i - 1) * StrLen) + 1, StrLen)
Next i

' Set the function StringToArray to the Temp array so it
' can be returned to the calling procedure.

StringToArray = Temp

End Function

wrote in message

...

Hi,


I've found a module that will let me query SQL Server databases via
ODBC. Below I've created a module which does exactly what I want it to
do using concatenated cells in a worksheet. My issue however, is that
now that I've gotten the code correct, excel tells me it doesn't have
the resources necessary to execute the command. My example below
searches for 2 UPCs, in reality I'll have several hundred, possibly
thousands. If I type these values in manually they work just fine, and
I've even tried executing the code on the SQL server itself with no
luck. Is there a more efficient way to do this? I can't simply query
everything then do a lookup table because the results exceed excels
max dimensions. I'm essentially trying to see if these UPCs are in our
system.


Thanks!


<----begin code------


Sub what()


Dim qt As QueryTable


sqlstring = "select vm.vendor, im.upc_ean, im.description,
vi.vendor_item, vi.case_pack from vendor_item vi, vendor_master vm,
item_master im where vi.record_status < 3 and vm.record_status<3 and
im.record_status<3 and vi.item_id = im.item_id and vi.v_id =vm.v_id
and vi.vi_block_from_pos=0 and vendor='" & Sheet3.Range("A1") & "' and
upc_ean like '" & Sheet3.Range("A3") & ""


connstring = _
"ODBC;DSN=tciinstore;UID=administrator;PWD=!
password;Database=tciinstore"


With Sheet1.QueryTables.Add(Connection:=connstring,
Destination:=Range("A1"), Sql:=sqlstring)
.Refresh
End With


Sheet1.Range("A1").Select
End Sub


<----end code------


Thanks Jean...i'm having trouble figuring out where to put everything
though. Do I combine this with my code? Thanks!
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default Query SQL Server with concatenated text from Excel

Try changing your code so that the string sqlstring contains teh following
expression: "LIKE %xxx%" rather than "LIKE 'XXX' "

Sam

" wrote:

Hi,

I've found a module that will let me query SQL Server databases via
ODBC. Below I've created a module which does exactly what I want it to
do using concatenated cells in a worksheet. My issue however, is that
now that I've gotten the code correct, excel tells me it doesn't have
the resources necessary to execute the command. My example below
searches for 2 UPCs, in reality I'll have several hundred, possibly
thousands. If I type these values in manually they work just fine, and
I've even tried executing the code on the SQL server itself with no
luck. Is there a more efficient way to do this? I can't simply query
everything then do a lookup table because the results exceed excels
max dimensions. I'm essentially trying to see if these UPCs are in our
system.

Thanks!

<----begin code------

Sub what()

Dim qt As QueryTable

sqlstring = "select vm.vendor, im.upc_ean, im.description,
vi.vendor_item, vi.case_pack from vendor_item vi, vendor_master vm,
item_master im where vi.record_status < 3 and vm.record_status<3 and
im.record_status<3 and vi.item_id = im.item_id and vi.v_id =vm.v_id
and vi.vi_block_from_pos=0 and vendor='" & Sheet3.Range("A1") & "' and
upc_ean like '" & Sheet3.Range("A3") & ""

connstring = _
"ODBC;DSN=tciinstore;UID=administrator;PWD=!
password;Database=tciinstore"

With Sheet1.QueryTables.Add(Connection:=connstring,
Destination:=Range("A1"), Sql:=sqlstring)
.Refresh
End With

Sheet1.Range("A1").Select
End Sub


<----end code------

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel with SQL Query (*.sql) and SQL Server Kode Excel Discussion (Misc queries) 2 May 8th 07 01:10 PM
Excel:Get concatenated text to be recognised as formula not text? yvette Excel Discussion (Misc queries) 5 January 15th 07 07:32 PM
Query SQL Server from Excel Doctorjones_md Excel Worksheet Functions 1 January 6th 07 08:35 AM
Query SQL Server from Excel Doctorjones_md Excel Programming 1 January 6th 07 08:35 AM
How do I convert a text array to a concatenated text cell? Excel. Vargasjc Excel Worksheet Functions 5 December 20th 06 06:35 PM


All times are GMT +1. The time now is 01:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"