Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieving 2 or more values from a DB query
#1
Solved: 11 Years, 4 Months, 4 Weeks ago
I have stored the results of a DB query in a variable, here's my code:

Code:
Set adIDs = db.execute(AdIDsSql)
val2 = adIDs.GetString
msgbox val2

The message box then displays the following (on separate lines):
TRAIN1
TRAIN2

How can I now store each in it's own varible? What method would take into account a line return? Tried the following but it doesn't work:

adj1 = LTrim(val2)
adj2 = RTrim(val2)
Reply
#2
Solved: 11 Years, 4 Months, 4 Weeks ago
there can be two ways for this - either split the variable val2 and you can get seperate values in variable or
try to traverse through recordset, I mean do not use GetString, try recordsetobject.fields(0).

Reply
#3
Solved: 11 Years, 4 Months, 4 Weeks ago
Egun -
try this,
Code:
adj = Split(val2, VBLF)

For i = Lbound(adj) to Ubound(adj)
      msgbox adj(i)
Next
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#4
Solved: 11 Years, 4 Months, 4 Weeks ago
Hi Basanth,

When I tried this:
Code:
adj = Split(val2, VBLF)

For i = Lbound(adj) to Ubound(adj)
msgbox adj(i)
Next

All that prints out is the same as before:
TRAIN1
TRAIN2

How can I go through the array (read about the LBound and UBound function) and assign each element to a new value? So that TRAIN1 and TRAIN2 are stored in 2 separate variables.

Thank you.
Reply
#5
Solved: 11 Years, 4 Months, 4 Weeks ago
Egun -
You may need to research more about arrays. Look up for arrays and see what exactly it means. you will get more info about the above code and the usage of split function.

The values are already stored in a variable. the variable here is adj. the values are within the variable adj. Each value has a reference point created begining from the location Zero to the max you utilize. So adj(0) will give you the pointer to the first value and adj(1) will give you the pointer to the second one, so forth. This concept is called Arrays. The values stored in an array has a reference point begining from zero.

A simple example would be,
Code:
Dim a

a = Array(10, 20, 30) ' Defining a array
msgbox Isarray(a) ' Should return True

msgbox a(0) ' Should Return 10
msgbox a(1)  ' Should Return 20
msgbox a(2)  ' Should Return 30

Similary, When you Split it stores all the items in an array. Hence you dont have to individually store them in separate variables but just use the same variable with the reference.

Let me know if it helps.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#6
Solved: 11 Years, 4 Months, 4 Weeks ago
Hi Basanth,

I guess I am still confused at the disconnect here. It seems the 1st variable adj(0) has both values stored. I cannot separate them successfully. I understand the arrays concept but am not familiar with the Split function.

So I updated my code to print out the 2 values after the For statement:

Code:
adj = Split(val2, VBLF)
For i = Lbound(adj) to Ubound(adj)
msgbox adj(i)
Next

msgbox adj(0)
msgbox adj(1)
When I run this, msgbox adj(0) prints out:
TRAIN1
TRAIN2

and msgbox adj(1) throws an error:

Subscript out of range: '[number: 1]'
Line (37): "msgbox adj(1)".

I will look more into the Split function, but if you have further suggestions I appreciate the help...

-egun
Reply
#7
Solved: 11 Years, 4 Months, 4 Weeks ago
Hi Basanth,

I guess I am still confused at the disconnect here. It seems the 1st variable adj(0) has both values stored. I cannot separate them successfully. I understand the arrays concept but am not familiar with the Split function.

So I updated my code to print out the 2 values after the For statement:

adj = Split(val2, VBLF)
For i = Lbound(adj) to Ubound(adj)
msgbox adj(i)
Next

msgbox adj(0)
msgbox adj(1)

When I run this, msgbox adj(0) prints out:
TRAIN1
TRAIN2

and msgbox adj(1) throws an error:

Subscript out of range: '[number: 1]'
Line (37): "msgbox adj(1)".

I will look more into the Split function, but if you have further suggestions I appreciate the help...

-egun

Okay, From the Msgbox adj(0) it is clear that the split has not happened.

Lets try with this,
Code:
adj = Split(Val2, " ")
msgbox adj(0)

Okay, From the Msgbox adj(0) it is clear that the split has not happened.

Lets try with this,
Code:
adj = Split(Val2, " ")
msgbox adj(0)

Quickly, While you try the above, please try the below too,
Code:
adj = Split(Val2, VBCR)
msgbox adj(0)

Okay, From the Msgbox adj(0) it is clear that the split has not happened.

Lets try with this,
Code:
adj = Split(Val2, " ")
msgbox adj(0)


Okay, From the Msgbox adj(0) it is clear that the split has not happened.

Lets try with this,
Code:
adj = Split(Val2, " ")
msgbox adj(0)


Quickly, While you try the above, please try the below too,
Code:
adj = Split(Val2, VBCR)
msgbox adj(0)


Egun -
I have confirmed the below works fine and is the solution you are looking for.
Code:
adj = Split(val2, VBCR)
For i = Lbound(adj) to Ubound(adj)
msgbox adj(i)
Next
This should store the required values from adj(0), adj(1) etc...

Please try this and let me know if it works !! I am here for the next 10 mins hoping to see your reply.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#8
Solved: 11 Years, 4 Months, 4 Weeks ago
I will try your suggestion now with the VbCr
It worked!! Thank you so much, Basanth.... I appreciate your time in looking at this for me.

Thanks,
egun
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Query regarding "Call" statement anupam4j 1 2,071 09-26-2014, 01:46 PM
Last Post: vinod123
  Retrieving data from web table nsuresh316 1 3,159 03-03-2014, 08:29 AM
Last Post: basanth27
  Data Table Query Suma Parimal 4 4,837 01-21-2014, 12:50 PM
Last Post: Suma Parimal
  UFT Query invisible786 0 1,933 01-15-2014, 09:41 PM
Last Post: invisible786
  Retrieving data from Txt file into datatable kriday 3 5,850 07-23-2013, 11:04 AM
Last Post: vinod123

Forum Jump:


Users browsing this thread: 3 Guest(s)