Solved

VB6 ADO Return Records For Each Loop

Posted on 2014-12-02
9
244 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

776 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