Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6 ADO Return Records For Each Loop

Posted on 2014-12-02
9
Medium Priority
?
286 Views
Last Modified: 2014-12-02
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

0
Comment
Question by:desiredforsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476624
You have to iterate over records using records.MoveNext until end of recordset.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40476635
you are currently doing a for each field instead of "for each record"

records.Open , sql, database, adLockReadOnly
while not records.EOF
finalvalue = finalvalue & records.Fields(0) & ","
records.movenext
wend

Open in new window

0
 

Author Comment

by:desiredforsome
ID: 40476672
what is wend?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476703
wend is end of while block
0
 

Author Comment

by:desiredforsome
ID: 40476734
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

0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476750
does it raise an error or give an empty string?

do you know structure of your database table? you may be reading wrong column.
0
 

Author Comment

by:desiredforsome
ID: 40476756
looks like something possibly to do with data source name too long is what debug tells me hmmm.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40476786
That error is about the connection string being incorrect...
0
 

Author Comment

by:desiredforsome
ID: 40476797
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question