Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Trouble with Array

Reply
 
Old 04-30-2009   #1 (permalink)
bcap


 
 

Trouble with Array

Hi,

I am trying to learn how to use an array for an update statement and
seem to be getting spaces in my update.

Here is what the update looks like:

UPDATE Location SET LocDesc='Test1', Active='1' WHERE LocationID='13'
UPDATE Location SET LocDesc=' Test2', Active=' 1' WHERE LocationID='
12'
UPDATE Location SET LocDesc=' Test3', Active=' 1' WHERE LocationID='
5'
UPDATE Location SET LocDesc=' Test4', Active=' 1' WHERE LocationID='
9'
UPDATE Location SET LocDesc=' Test5', Active=' 1' WHERE LocationID='
3'

(Notice, the first one worked as expected, but the following ones have
spaces before the parameter starts)

Here is my query:

'LocDesc
Dim MyLocDesc,LocDescArray
MyLocDesc = request.form("LocDesc")
LocDescArray=Split(MyLocDesc,",")

'Active
Dim MyActive,ActiveArray
MyActive = request.form("Active")
ActiveArray=Split(MyActive,",")

'LocID
Dim MyLocID,LocIDArray
MyLocID = request.form("LocationID")
LocIDArray=Split(MyLocID,",")

For i = 0 to uBound(LocDescArray)

set conn=server.createobject("adodb.connection")
conn.open "data Source=xxx;User ID=xxx;Password=xxx;"

sqlstmt = "UPDATE Location"
sqlstmt = sqlstmt & " SET "'"


sqlstmt = sqlstmt & "LocDesc='"& LocDescArray(i) & "', "
sqlstmt = sqlstmt & "Active='" & ActiveArray(i) & "'"
sqlstmt = sqlstmt & " WHERE LocationID='" & LocIDArray(i) &
"'"

response.write sqlstmt
response.write "<BR>"
Set RS = conn.execute(SQLstmt)
Next

Can you please help? Is there a better way to do this?

Thanks so much!

My System SpecsSystem Spec
Old 04-30-2009   #2 (permalink)
ekkehard.horner


 
 

Re: Trouble with Array

bcap schrieb:
Quote:

> Hi,
>
> I am trying to learn how to use an array for an update statement and
> seem to be getting spaces in my update.
>
> Here is what the update looks like:
>
> UPDATE Location SET LocDesc='Test1', Active='1' WHERE LocationID='13'
> UPDATE Location SET LocDesc=' Test2', Active=' 1' WHERE LocationID='
> 12'
> UPDATE Location SET LocDesc=' Test3', Active=' 1' WHERE LocationID='
> 5'
> UPDATE Location SET LocDesc=' Test4', Active=' 1' WHERE LocationID='
> 9'
> UPDATE Location SET LocDesc=' Test5', Active=' 1' WHERE LocationID='
> 3'
>
> (Notice, the first one worked as expected, but the following ones have
> spaces before the parameter starts)
>
> Here is my query:
>
> 'LocDesc
> Dim MyLocDesc,LocDescArray
> MyLocDesc = request.form("LocDesc")
> LocDescArray=Split(MyLocDesc,",")
>
> 'Active
> Dim MyActive,ActiveArray
> MyActive = request.form("Active")
> ActiveArray=Split(MyActive,",")
>
> 'LocID
> Dim MyLocID,LocIDArray
> MyLocID = request.form("LocationID")
> LocIDArray=Split(MyLocID,",")
>
> For i = 0 to uBound(LocDescArray)
>
> set conn=server.createobject("adodb.connection")
> conn.open "data Source=xxx;User ID=xxx;Password=xxx;"
>
> sqlstmt = "UPDATE Location"
> sqlstmt = sqlstmt & " SET "'"
>
>
> sqlstmt = sqlstmt & "LocDesc='"& LocDescArray(i) & "', "
> sqlstmt = sqlstmt & "Active='" & ActiveArray(i) & "'"
> sqlstmt = sqlstmt & " WHERE LocationID='" & LocIDArray(i) &
> "'"
>
> response.write sqlstmt
> response.write "<BR>"
> Set RS = conn.execute(SQLstmt)
> Next
>
> Can you please help? Is there a better way to do this?
>
> Thanks so much!
Assuming that the leading spaces are caused by MyLocDesc containing
text like

"FrsLoc, SecLoc, ThrLoc"

you could change
Quote:

> sqlstmt = sqlstmt & "LocDesc='"& LocDescArray(i) & "', "
> sqlstmt = sqlstmt & "Active='" & ActiveArray(i) & "'"
to

sqlstmt = sqlstmt & "LocDesc='"& Trim( LocDescArray(i) ) & "', "
sqlstmt = sqlstmt & "Active='" & Trim( ActiveArray(i) ) & "'"

This would deal with leading blanks caused by other agents too.

In general, you should consider using stored precedures/commands
instead of dynamically concatenated SQL to do the updates. As a first
step in that direction consider:

sSQLT = "UPDATE Location SET LocDesc='@L@', Active='1' WHERE LocationID='@I@'"
....
For i = 0 to uBound(LocDescArray)
....
sSQL = Replace( Replace( sSQLT, "@L@", Trim( LocDescArray(i) ) ), _
"@I@", Trim( ActiveArray(i) ) )
...
My System SpecsSystem Spec
Old 04-30-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: Trouble with Array


"bcap" <rayh@xxxxxx> wrote in message
news:13c442ae-ace2-4eb0-8ab9-c3150a2e4c34@xxxxxx
Quote:

> Hi,
>
> I am trying to learn how to use an array for an update statement and
> seem to be getting spaces in my update.
>
> Here is what the update looks like:
>
> UPDATE Location SET LocDesc='Test1', Active='1' WHERE LocationID='13'
> UPDATE Location SET LocDesc=' Test2', Active=' 1' WHERE LocationID='
> 12'
> UPDATE Location SET LocDesc=' Test3', Active=' 1' WHERE LocationID='
> 5'
> UPDATE Location SET LocDesc=' Test4', Active=' 1' WHERE LocationID='
> 9'
> UPDATE Location SET LocDesc=' Test5', Active=' 1' WHERE LocationID='
> 3'
>
> (Notice, the first one worked as expected, but the following ones have
> spaces before the parameter starts)
>
> Here is my query:
>
> 'LocDesc
> Dim MyLocDesc,LocDescArray
> MyLocDesc = request.form("LocDesc")
> LocDescArray=Split(MyLocDesc,",")
>
> 'Active
> Dim MyActive,ActiveArray
> MyActive = request.form("Active")
> ActiveArray=Split(MyActive,",")
>
> 'LocID
> Dim MyLocID,LocIDArray
> MyLocID = request.form("LocationID")
> LocIDArray=Split(MyLocID,",")
>
> For i = 0 to uBound(LocDescArray)
>
> set conn=server.createobject("adodb.connection")
> conn.open "data Source=xxx;User ID=xxx;Password=xxx;"
>
> sqlstmt = "UPDATE Location"
> sqlstmt = sqlstmt & " SET "'"
>
>
> sqlstmt = sqlstmt & "LocDesc='"& LocDescArray(i) & "', "
> sqlstmt = sqlstmt & "Active='" & ActiveArray(i) & "'"
> sqlstmt = sqlstmt & " WHERE LocationID='" & LocIDArray(i) &
> "'"
>
> response.write sqlstmt
> response.write "<BR>"
> Set RS = conn.execute(SQLstmt)
> Next
>
> Can you please help? Is there a better way to do this?
>
> Thanks so much!
The spaces must be in the comma delimited strings you are converting into
arrays. Use the Trim function to remove any leading or trailing spaces. For
example:

sqlstmt = sqlstmt & "LocDesc='"& Trim(LocDescArray(i)) & "', "
sqlstmt = sqlstmt & "Active='" & Trim(ActiveArray(i)) & "'"
sqlstmt = sqlstmt & " WHERE LocationID='" & Trim(LocIDArray(i)) & "'"

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 04-30-2009   #4 (permalink)
bcap


 
 

Re: Trouble with Array

Thanks for the help guys! Very much appreciated!!!!

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
trouble with count / length of array filled from filtered ls PowerShell
Fast copy method of sub array (=array range) possible? VB Script
How to create array without quotes? $array = (a,b,c) PowerShell
Stupid Array Tricks: Initializing an Array to a Certain Size PowerShell
how to assign values to array and how to create array via variable PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46