tags:

views:

32

answers:

1

Hi, Please find below the code..

Function Connect_to_db(Byval mfgprt)

    Dim cnn,rss
    Set cnn = CreateObject("ADODB.Connection")
    Set rss = CreateObject("ADODB.recordset")
    cnn.ConnectionString = "DSN=QTPDSN;Description=desc;UID=;PWD=;APP=QuickTest Professional;WSID=;DATABASE=;"

    cnn.open
    rss = cnn.Execute (""select  UnitPrice  from ProductProfilePrices  where MfPartNumber ='" + mfgprt + "'")

    Connect_to_db=rss(0)
End Function

In this function, if I change a col name unit price in Query with '*' then it will return more than one value..in that case how to use rss .....

As if i'll do it(replace unit price with '*'),then while running it populates an error..in rss data fetching.. please by doing same modify the code.....

Thanks, Galstar

+1  A: 

You can refer to the fields by name, but first rss should be an object, so use Set, also the string concatenator is & :

Set rss = cnn.Execute (""select  UnitPrice, Quantity  " _
& " from ProductProfilePrices  where MfPartNumber ='" & mfgprt & "'")

''Let us say that only one row is returned for mfgprt :

varUnitPrice = rss("UnitPrice")
varQuantity = rss("Quantity")

EDIT re comments

Connect_to_db "AAA", Val1, Val2
MsgBox Val1 & "  " & Val2


Function Connect_to_db(ByVal mfgprt, ByRef Val1, ByRef Val2)
Dim cnn, rss
Set cnn = CreateObject("ADODB.Connection")
Set rss = CreateObject("ADODB.recordset")
cnn.ConnectionString = "DSN=QTPDSN;Description=desc;" _
    & "UID=;PWD=;APP=QuickTest Professional;WSID=;DATABASE=;"

cnn.Open 
rss = cnn.Execute("select  UnitPrice, Quantity  " _
    & " from ProductProfilePrices  where MfPartNumber ='" & mfgprt & "'")

Val1 = rss(0)
Val2 = rss(1)
End Function
Remou
Thanks for the reply....then what to do with.."Connect_to_db=rss(0) as this is to return the value in function.....
galstar
If you wish to return more than one value to some other procedure, you can pass the names to be returned to this function or you can return an array (rss.GetRows). In the example above, rss(o) = rss("UnitPrice") rss(1) would be the next column (field) selected.
Remou
Hey i got the point what you want to convey that save values in rss as rss(0)=rss(unitprice) rss(1)=rss('Quantity') ..now how to return these value through function in diffrent variables... connect_to_db=rss shows error as this is not correct... Please help me ........firstly for saving i used to do,a=connect_to_db(x) as i want only one col value..but now i want two col's values.. plz give me some hints:(
galstar
Okay, I have added some notes.
Remou