Solved

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

Posted on 2014-12-04
4
124 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
4 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
    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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now