Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Array data in SQL Query?

Hi,

I'm trying to read data from a worksheet, inputting it into an array,
and make SQL queries based on the array value, repeat for each value.
I'm having problems getting the array value into the query though.

Heres example code:


Sub Pass_array()
Dim myarray As Variant
myarray = Range("C15:C18").Value
receive_array myarray

End Sub

---

Sub receive_array(thisarray)
Dim dbConn2 As New ADODB.Connection
Dim rs2 As New ADODB.Recordset 'the recordset

dbConn2.ConnectionString = (connection string here, works fine)

dbConn2.Open



For i = 1 To UBound(thisarray)

MsgBox thisarray(i, 1) *** <-- this is my check to see what value is
getting put in

With rs2

..Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START=thisarray(i,1)", dbConn2,
adOpenStatic

noRecords = .RecordCount

Range("F15").CopyFromRecordset rs

.Close

End With

Next

dbConn.Close


End Sub


If I do this, it says thisarray is not a function. If I just input i,
it says invalid column name.

Any help is appreciated thanks.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Array data in SQL Query?

An array element is just like a variable, so you will have to do this:

..Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START = " & thisarray(i,1), dbConn2,
adOpenStatic

Otherwise thisArray(i,1) will taken as that literal string, which obviously
means nothing.

RBS


"mazzarin" wrote in message
ups.com...
Hi,

I'm trying to read data from a worksheet, inputting it into an array,
and make SQL queries based on the array value, repeat for each value.
I'm having problems getting the array value into the query though.

Heres example code:


Sub Pass_array()
Dim myarray As Variant
myarray = Range("C15:C18").Value
receive_array myarray

End Sub

---

Sub receive_array(thisarray)
Dim dbConn2 As New ADODB.Connection
Dim rs2 As New ADODB.Recordset 'the recordset

dbConn2.ConnectionString = (connection string here, works fine)

dbConn2.Open



For i = 1 To UBound(thisarray)

MsgBox thisarray(i, 1) *** <-- this is my check to see what value is
getting put in

With rs2

.Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START=thisarray(i,1)", dbConn2,
adOpenStatic

noRecords = .RecordCount

Range("F15").CopyFromRecordset rs

.Close

End With

Next

dbConn.Close


End Sub


If I do this, it says thisarray is not a function. If I just input i,
it says invalid column name.

Any help is appreciated thanks.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Array data in SQL Query?

I see what you mean!

but you have the value placed outside of the query itself, and I cannot
get it to become what its supposed to be (in the first case, 500)
inside the quotes...

(PRICING.START= &thisarray(i,1))", -- Incorrect syntax near '&'

(PRICING.START= '&thisarray(i,1)')", -- Syntax error converting the
varchar value '&thisarray(i,1)' to a column of data type int

(PRICING.START= " &thisarray(i, 1), -- Incorrect syntax near '500'

Thoughts?


RB Smissaert wrote:
An array element is just like a variable, so you will have to do this:

.Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START = " & thisarray(i,1), dbConn2,
adOpenStatic

Otherwise thisArray(i,1) will taken as that literal string, which obviously
means nothing.

RBS


"mazzarin" wrote in message
ups.com...
Hi,

I'm trying to read data from a worksheet, inputting it into an array,
and make SQL queries based on the array value, repeat for each value.
I'm having problems getting the array value into the query though.

Heres example code:


Sub Pass_array()
Dim myarray As Variant
myarray = Range("C15:C18").Value
receive_array myarray

End Sub

---

Sub receive_array(thisarray)
Dim dbConn2 As New ADODB.Connection
Dim rs2 As New ADODB.Recordset 'the recordset

dbConn2.ConnectionString = (connection string here, works fine)

dbConn2.Open



For i = 1 To UBound(thisarray)

MsgBox thisarray(i, 1) *** <-- this is my check to see what value is
getting put in

With rs2

.Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START=thisarray(i,1)", dbConn2,
adOpenStatic

noRecords = .RecordCount

Range("F15").CopyFromRecordset rs

.Close

End With

Next

dbConn.Close


End Sub


If I do this, it says thisarray is not a function. If I just input i,
it says invalid column name.

Any help is appreciated thanks.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Array data in SQL Query?

I find it easier to put the query in a variable, so you would get:

Dim strQuery As String

strQuery = "SELECT DISTINCT PRICING.PRICE " & _
"FROM MAIN, PRICING " & _
"WHERE MAIN.SERIES LIKE 'A123' AND " & _
"PRICING.TYPE = 'D' AND " & _
"MAIN.ID = PRICING.PART_ID AND " & _
"PRICING.START = " & thisarray(i, 1)

With rs2
.Open strQuery, dbConn2, adOpenStatic

As your brackets in the query served no purpose I left them out.
Also better to keep your code lines short and divide it in logical parts as
above.


RBS


"mazzarin" wrote in message
oups.com...
I see what you mean!

but you have the value placed outside of the query itself, and I cannot
get it to become what its supposed to be (in the first case, 500)
inside the quotes...

(PRICING.START= &thisarray(i,1))", -- Incorrect syntax near '&'

(PRICING.START= '&thisarray(i,1)')", -- Syntax error converting the
varchar value '&thisarray(i,1)' to a column of data type int

(PRICING.START= " &thisarray(i, 1), -- Incorrect syntax near '500'

Thoughts?


RB Smissaert wrote:
An array element is just like a variable, so you will have to do this:

.Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START = " & thisarray(i,1), dbConn2,
adOpenStatic

Otherwise thisArray(i,1) will taken as that literal string, which
obviously
means nothing.

RBS


"mazzarin" wrote in message
ups.com...
Hi,

I'm trying to read data from a worksheet, inputting it into an array,
and make SQL queries based on the array value, repeat for each value.
I'm having problems getting the array value into the query though.

Heres example code:


Sub Pass_array()
Dim myarray As Variant
myarray = Range("C15:C18").Value
receive_array myarray

End Sub

---

Sub receive_array(thisarray)
Dim dbConn2 As New ADODB.Connection
Dim rs2 As New ADODB.Recordset 'the recordset

dbConn2.ConnectionString = (connection string here, works fine)

dbConn2.Open



For i = 1 To UBound(thisarray)

MsgBox thisarray(i, 1) *** <-- this is my check to see what value is
getting put in

With rs2

.Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START=thisarray(i,1)", dbConn2,
adOpenStatic

noRecords = .RecordCount

Range("F15").CopyFromRecordset rs

.Close

End With

Next

dbConn.Close


End Sub


If I do this, it says thisarray is not a function. If I just input i,
it says invalid column name.

Any help is appreciated thanks.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Array data in SQL Query?

It works! Thank you very much. I will try and organize my code better
in the future :)


RB Smissaert wrote:
I find it easier to put the query in a variable, so you would get:

Dim strQuery As String

strQuery = "SELECT DISTINCT PRICING.PRICE " & _
"FROM MAIN, PRICING " & _
"WHERE MAIN.SERIES LIKE 'A123' AND " & _
"PRICING.TYPE = 'D' AND " & _
"MAIN.ID = PRICING.PART_ID AND " & _
"PRICING.START = " & thisarray(i, 1)

With rs2
.Open strQuery, dbConn2, adOpenStatic

As your brackets in the query served no purpose I left them out.
Also better to keep your code lines short and divide it in logical parts as
above.


RBS


"mazzarin" wrote in message
oups.com...
I see what you mean!

but you have the value placed outside of the query itself, and I cannot
get it to become what its supposed to be (in the first case, 500)
inside the quotes...

(PRICING.START= &thisarray(i,1))", -- Incorrect syntax near '&'

(PRICING.START= '&thisarray(i,1)')", -- Syntax error converting the
varchar value '&thisarray(i,1)' to a column of data type int

(PRICING.START= " &thisarray(i, 1), -- Incorrect syntax near '500'

Thoughts?


RB Smissaert wrote:
An array element is just like a variable, so you will have to do this:

.Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START = " & thisarray(i,1), dbConn2,
adOpenStatic

Otherwise thisArray(i,1) will taken as that literal string, which
obviously
means nothing.

RBS


"mazzarin" wrote in message
ups.com...
Hi,

I'm trying to read data from a worksheet, inputting it into an array,
and make SQL queries based on the array value, repeat for each value.
I'm having problems getting the array value into the query though.

Heres example code:


Sub Pass_array()
Dim myarray As Variant
myarray = Range("C15:C18").Value
receive_array myarray

End Sub

---

Sub receive_array(thisarray)
Dim dbConn2 As New ADODB.Connection
Dim rs2 As New ADODB.Recordset 'the recordset

dbConn2.ConnectionString = (connection string here, works fine)

dbConn2.Open



For i = 1 To UBound(thisarray)

MsgBox thisarray(i, 1) *** <-- this is my check to see what value is
getting put in

With rs2

.Open "SELECT DISTINCT PRICING.PRICE FROM MAIN, PRICING WHERE
(MAIN.SERIES LIKE 'A123') AND (PRICING.TYPE = 'D') AND (MAIN.ID =
PRICING.PART_ID) AND (PRICING.START=thisarray(i,1)", dbConn2,
adOpenStatic

noRecords = .RecordCount

Range("F15").CopyFromRecordset rs

.Close

End With

Next

dbConn.Close


End Sub


If I do this, it says thisarray is not a function. If I just input i,
it says invalid column name.

Any help is appreciated thanks.



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
How to import web query as array? Sam_stock Excel Discussion (Misc queries) 0 April 26th 07 03:58 PM
Array formulas won't recognize external data from Microsoft Query mmrtech Excel Discussion (Misc queries) 7 December 18th 06 11:31 PM
SQL Query Destination as an array varable [email protected] Excel Programming 2 June 19th 06 07:31 PM
Web Query & Array Questions! gr8guy Excel Programming 1 August 25th 04 11:05 PM
Query for data in an array anonymous Excel Programming 2 January 13th 04 12:51 PM


All times are GMT +1. The time now is 05:22 PM.

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

About Us

"It's about Microsoft Excel"