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

x
?
Solved

Check value against value above and below

Posted on 2004-04-22
16
Medium Priority
?
231 Views
Last Modified: 2010-05-02
With the help of aelatik the below script was assembled.

Dim FSO, TEXTSTREAM, CSV_FILE, CSV_LINES, CSV_ITEMS, CSV_LINE
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set TEXTSTREAM = FSO.OpenTextFile("D:\pathtocsvfile\file.csv", 1, False, 0)
        CSV_FILE = TEXTSTREAM.ReadAll
    Set TEXTSTREAM = Nothing
    Set FSO = Nothing
        CSV_LINES = Split(CSV_FILE, vbCrLf)
       
        For Each CSV_LINE In CSV_LINES
            CSV_ITEMS = Split(CSV_LINE, ",")
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
          Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
        Next

Function Mail(M_NAME, M_TO, M_BODY)
    Set Mail = CreateObject("CDO.Message")
    Set CON = CreateObject("CDO.Configuration")
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.1.1"
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
        CON.Fields.Update
    Set Mail.Configuration = CON
        Mail.From = "Registration.Distribution@ourdomain.com"
        Mail.To = M_TO
        Mail.CC = ""
        Mail.Subject = "Spring 2004 final exam number & Fall Pre-Registration number"
        Mail.HTMLBody = M_BODY
        Mail.Send
    Set Mail = Nothing
    Set CON = Nothing
End Function

I need to take this one step farther.

I need the CSV_ITEMS(3) to be compared to the same value up one row and down one row, if either one is = then insert "T" after that value CSV_ITEMS(3), if not equal just insert CSV_ITEMS(3).

Thanks
CurtV
0
Comment
Question by:Curtv
16 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 10891441
      For Each CSV_LINE In CSV_LINES
            CSV_ITEMS = Split(CSV_LINE, ",")
            Dim X
            If CSV_ITEMS(3) = CSV_ITEMS(1) Or CSV_ITEMS(3) = CSV_ITEMS(2) Then CSV_ITEMS(3) = CSV_ITEMS(3) & "T"
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
        Next
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10891466
Correction:
If CSV_ITEMS(3) = CSV_ITEMS(2) Or CSV_ITEMS(3) = CSV_ITEMS(4) Then CSV_ITEMS(3) = CSV_ITEMS(3) & "T"

S

0
 
LVL 14

Expert Comment

by:aelatik
ID: 10891876
Curtv,

What is the exact goal you wish to accomplish with that check ? By the way, can you also post the CSV file your using ( with fake data ofcourse ).
Maybe there's a better way to do it.

If i'm not wrong you mean matching CSV_ITEMS(3) against the CSV_ITEM(3) from previous and next line. Is this correct ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Curtv
ID: 10892097
Aelatik

Man your active in the this group. Glad to be apart.

Short answer yes, previous and next lines. Better terminology.

The data will be somewhere in the format of:
rank,PIN,ssn,firstname,lastname,emailaddress
1,232,555-55-5555,me,melast,me@ourdomain.com

I don't have the data yet, I'm guessing.

As you can see this is for rankings. I need the "T" (tied) to be inserted if the rank below (next row down in csv), or above (row up in csv) are the same. It seems tricky to do this with while staying on the same data line.

Shauli

I need to check above and below. I use the term "row" meaning each line of data.

Thanks
CurtV
0
 
LVL 9

Expert Comment

by:SoftEng007
ID: 10892390
Once you have executed the line:
CSV_LINES = Split(CSV_FILE, vbCrLf)

you now have an array of X lines.
instead of using for each
use ubound(CSV_LINES) to get the total array count.

use special check for item 0 and item ubound(CSV_LINES)
0 item has no before and upper item has no below :)
use a loop for i =1 to ubound(CSV_LINES)-1
pull three lines in at a time  
CSV_ABOVE = Split(CSV_LINE(i-1, ",")
CSV_ITEMS = Split(CSV_LINE(i, ",")
CSV_BELOW= Split(CSV_LINE(i+1, ",")

'do your stuff --


0
 
LVL 19

Expert Comment

by:Shauli
ID: 10892812
My mistake, try this:

Dim CSV_LastLine as String, CSV_NextLine() As String, c As Long
On Error Resume Next           'this is for the first and last lines
        For c = LBound(CSV_LINES) To UBound(CSV_LINES)
            CSV_ITEMS = Split(CSV_LINES(c), ",")
            If CSV_ITEMS(3) = CSV_LastLine Or CSV_ITEMS(3) = CSV_NextLine(3) Then CSV_ITEMS(3) = CSV_ITEMS(3)  & "T"
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
             CSV_LastLine =  CSV_ITEMS(3)
             CSV_NextLine = Split(CSV_LINES(c+1)
        Next c


I haven't tested it. Just of the screen. So check carefully.

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10892935
CSV_NextLine = Split(CSV_LINES(c+1))
                                                       ^
Forgot the parenthesis

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10893722
Woh this line
 CSV_NextLine = Split(CSV_LINES(c+1), ",")

Sorry :(

S
0
 

Author Comment

by:Curtv
ID: 10895379
I am confused as to what part of the script Shauli's code is replacing?
0
 
LVL 19

Accepted Solution

by:
Shauli earned 1000 total points
ID: 10895410
Replace this part:

        For Each CSV_LINE In CSV_LINES
            CSV_ITEMS = Split(CSV_LINE, ",")
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
        Next

With this code:

Dim CSV_LastLine as String, CSV_NextLine() As String, c As Long
On Error Resume Next           'this is for the first and last lines
        For c = LBound(CSV_LINES) To UBound(CSV_LINES)
            CSV_ITEMS = Split(CSV_LINES(c), ",")
            If CSV_ITEMS(3) = CSV_LastLine Or CSV_ITEMS(3) = CSV_NextLine(3) Then CSV_ITEMS(3) = CSV_ITEMS(3)  & "T"
            Dim X
            X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
            X = X & "<BR><BR>Your <U>date here</U> web registration PIN is <b>F" &  CSV_ITEMS(3)  & vbCrLf
            X = X & "</B><BR><BR>Text text text<b>Text</B> text here <U>date underlined here</U> category." & vbCrLf
            X = X & "<BR><BR><BR>Thank You" & vbCrLf
            X = X & "<BR>Some More Text </P>" & vbCrLf
         Mail CSV_ITEMS(0), CSV_ITEMS(5), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(5)
             CSV_LastLine =  CSV_ITEMS(3)
             CSV_NextLine = Split(CSV_LINES(c+1), ",")
        Next c

S
0
 

Author Comment

by:Curtv
ID: 10895461
Wow fast response, unfortunately.

Line: 9
Char: 26
Error: Expected end of statement

Thanks
CurtV
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10895564
if you mean this line:
Dim CSV_LastLine as String, CSV_NextLine() As String, c As Long

Then try to replace with this line:

Dim CSV_LastLine, CSV_NextLine, c



0
 

Author Comment

by:Curtv
ID: 10895624
Line: 24
Char: 14
Expected end of statement

At the end of your code:
Next c

Also I had to take out the " " of the below line
X = "<p>Dear " & CSV_ITEMS(2) " " & CSV_ITEMS(1) & vbCrLf
Is there another way I can insert a space where the " " are"

Again Thanks
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10895640
Replace

Next c

with

Next


You missed the & before the " ", it should be:

X = "<p>Dear " & CSV_ITEMS(2) & " " & CSV_ITEMS(1) & vbCrLf

S


0
 

Author Comment

by:Curtv
ID: 10895821
Bingo

Thank you for your assistance.

CurtV
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10898284
You are welcome :)

S

ps. I think you need to accept  an answer if you got your solution :)

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

926 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