Link to home
Start Free TrialLog in
Avatar of desiredforsome
desiredforsome

asked on

VB6 ADO Return Records For Each Loop

Running a VB6 sql query and need to do something kind of crazy.

So I am running the following SQL query  SELECT username FROM MFUSERS WHERE USERNAME IN('test1', 'test2','test3')

I need it to create a string based off of what it has found.

So if it returns two rows and those two rows have TEST1 and TEST2 in them I need to concat to a string.

So for each record that it returns I would need it to build me a string as below example

Test1,test2

So delimited by comma

My Code Currentky

Sub Main()
    Dim finalvalue As String
    Dim database As New ADODB.Connection
Dim records As New ADODB.Recordset
Dim sql As String
Dim phones As String

database.ConnectionString="Provider=sqloledb; Data Source=192.168.2.121\EMMSDE;Initial Catalog=outlookreport; User Id=jsmith; Password=s5993153492;"
database.Open

sql = "SELECT * FROM OUTLOOKREPORT.DBO.MFUSERS WHERE USERNAME IN('" & AccountVariable("scriptrecip") & "'"
records.Open , sql, database, adLockReadOnly


For Each fld In records.Fields
finalvalue = finalvalue & records.Fields(0)
Next fld

End Sub

Open in new window

Avatar of Hakan Yılmaz
Hakan Yılmaz
Flag of Türkiye image

You have to iterate over records using records.MoveNext until end of recordset.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of desiredforsome
desiredforsome

ASKER

what is wend?
wend is end of while block
I think it could be possibly working however. It looks like my script is ignoring a part of it and it seems to happen after I open the DB. I cant figure this out.

The finalvalue = is not processing. Is there somethign I am doing wrong in my vb6?

Sub Main()
    Dim finalvalue As String
    Dim database As New ADODB.Connection
Dim records As New ADODB.Recordset
Dim sql As String
Dim phones As String

database.ConnectionString="Provider=sqloledb; Data Source=192.168.2.121\EMMSDE;Initial Catalog=outlookreport; User Id=jsmith; Password=s5993153492;"
database.Open

sql = "SELECT * FROM OUTLOOKREPORT.DBO.MFUSERS WHERE USERNAME IN('" & AccountVariable("scriptrecip") & "'"
records.Open , sql, database, adLockReadOnly


finalvalue = finalvalue & records.Fields(0) & ","
records.MoveNext

AccountVariable("test")=finalvalue
records.Close
database.Close


End Sub

Open in new window

does it raise an error or give an empty string?

do you know structure of your database table? you may be reading wrong column.
looks like something possibly to do with data source name too long is what debug tells me hmmm.
That error is about the connection string being incorrect...
I figured it out sorry lol. You WIN GUY!!! Final Code that worked below

Sub Main()

Dim finalvalue As String
Dim database As New ADODB.Connection
Dim records As New ADODB.Recordset
Dim sql As String
Dim phones As String
Dim totaloutput As String

database.ConnectionString="Provider=sqloledb; Data Source=192.168.2.121\EMMSDE;Initial Catalog=outlookreport; User Id=jsmith; Password=s5993153492;"
database.Open

sql = "SELECT * FROM OUTLOOKREPORT.DBO.MFUSERS WHERE USERNAME IN('" & AccountVariable("scriptrecip") & "')"
records.Open  sql, database, adLockReadOnly

While Not records.EOF

finalvalue = finalvalue & records.Fields(1) & ","

records.MoveNext
Wend
records.Close
database.Close

totaloutput = Left(finalvalue, Len(finalvalue) -1)

AccountVariable("test")=totaloutput




End Sub

Open in new window