Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is wrong with my VB6? For each string in array

Posted on 2014-12-04
4
Medium Priority
?
165 Views
Last Modified: 2014-12-18
I am coding up someting that shoudl be simple. I am sure my code has an issue.

My code is below. THe issue I am having is that during the For i=0 there is an issue when inserting into the database it does not insert anything it just leaves it blank. My databasde value should be a string.

My goal is to have it run a query for each string that is in the array and use that string that it is currently on in the query.

Public pdffile As ADODB.Stream
Sub main()


    Const delimiter As String = ","

    Dim i As Integer
    Dim iterStr As String
    Dim mainlist As String
    Dim sublist As String
    Dim split_sublist() As String
    Dim pdfpath, pdfpath1 As String
    Dim sql As String
    Dim database As New ADODB.Connection
    Dim rs As ADODB.Recordset


    mainlist = AccountVariable("xrefidlist")
    sublist = AccountVariable("xrefidlist")

    split_sublist = Split(sublist, delimiter)

    For i = 0 To UBound(split_sublist)
        database.ConnectionString = "Provider=sqloledb; Data Source=192.168.2.121\EMMSDE;Initial Catalog=outlookreport; User Id=xxxxx; Password=xxxxx;"
        database.Open
        sql = "SELECT * FROM comlog"
        Set rs = New ADODB.Recordset

        rs.Open " select * from comlog", database, adOpenKeyset, adLockOptimistic
        rs.AddNew

        Set pdffile = New ADODB.Stream
        pdffile.Type = adTypeBinary
        pdffile.Open
        pdffile.LoadFromFile "C:\test.pdf"
        rs.Fields("location") = pdffile.Read
        rs.Fields("xrefid") = split_sublist(i)
        rs.Fields("sender") = "Testing VB6"
        rs.Fields("status") = "Unread"
        rs.Fields("fromemail") = "gashlin@gmail.com"
        pdffile.Close
        Set pdffile = Nothing
        rs.Update
        Set rs = Nothing

        database.Close

    Next i

    'clean mainlist the way you want.
    mainlist = Trim(mainlist)

totaloutput = Left(mainlist, Len(mainlist) -1)
AccountVariable("recievers") = totaloutput

End Sub

Open in new window

0
Comment
Question by:desiredforsome
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40481626
Move your database connection/open/close outside the loop - silly to keep opening and closing it on every iteration of the loop
Remove the SELECT - you are adding fields so no point doing a select.

Is it actually looping properly?
Have you checked split_sublist is what it should be?
Any error messages?
0
 

Author Comment

by:desiredforsome
ID: 40481729
actually it turns out my system froze up and that was the issue. HOwever, How would I go about moving my db connection outside the loop so that it can insert it freely. Would I do something like

DB.open...

Loop

After Loop

Db.close?
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40481739
    database.ConnectionString = "Provider=sqloledb; Data Source=192.168.2.121\EMMSDE;Initial Catalog=outlookreport; User Id=xxxxx; Password=xxxxx;"
    database.Open

    For i = 0 To UBound(split_sublist)

        Set rs = New ADODB.Recordset
        rs.AddNew

        Set pdffile = New ADODB.Stream
        pdffile.Type = adTypeBinary
        pdffile.Open
        pdffile.LoadFromFile "C:\test.pdf"
        rs.Fields("location") = pdffile.Read
        rs.Fields("xrefid") = split_sublist(i)
        rs.Fields("sender") = "Testing VB6"
        rs.Fields("status") = "Unread"
        rs.Fields("fromemail") = "gashlin@gmail.com"
        pdffile.Close
        Set pdffile = Nothing
        rs.Update

    Next i
    Set rs = Nothing
    database.Close

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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…

772 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