Solved

VB6 ADO Return Records For Each Loop

Posted on 2014-12-02
9
235 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
  • 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476703
wend is end of while block
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 142

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now