Link to home
Start Free TrialLog in
Avatar of desiredforsome
desiredforsome

asked on

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

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

Avatar of Gary
Gary
Flag of Ireland image

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?
Avatar of desiredforsome
desiredforsome

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland 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