Results 1 to 36 of 36

Thread: short Array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651

    Unhappy short Array

    I need to write a short vb program which will look up my c:\customer directory and send select csv files by email to my customer.

    My directory c:\customer has multiple csv files all csv files have different names. Each csv file has a name and this name is unique to the customer. eg. If the file name = 123.csv then i need to email these files to a customer number 1.

    ie each customer has a corresponding unique number which identifies them.

    When the program reads through my directory and finds multiple CSV files named 123.csv then these multiple csv files need to be sent via email to customer 1.

    If csv file is named 456.csv then these csv files need to be sent via email to customer 2.

    I be told i need to create a text file which contains a list of unique codes with their corresponding customer email addresses.

    ie text file would read... "123, customer1, [email protected]"

    Im not sure where to start with my vb code.
    Can someone help me get up & running?
    thanks in advance
    Gilly

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Open your "codes" file, read in string by string and use Split with the comma delimiter to split the string into a UDT.

    Use Dir on the folder containing the csv's to find the current files.

    Once found, search the forums for email protocol type stuff.

    Make sure you put a textbox on the form that the user can enter the location of the csv files.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Create the text file manually. Once that is done, go onto the next steps.


    Create a VB program that reads in the text file and stores the information in an array.


    Use a loop to step through the array.
    For each line of the array, create a mail message.
    Also use the DIR function to get a list of files from the directory for that customer. Add each of these files to the e-mail message as an attachment.
    Send the mail message.


    OR do the loop the other way around:
    Use the DIR function to step through all files in the folder. For each file get the filename and use this to get the customers name and e-mail address from the array. Create the mail message, add the attachment and send the message.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    wow! that was a fast! you guys are so fast at responding, it's great!..

    I've created my text file, the location of the csv files will always be c:\customer.

    Can you provide me with example syntax as to how the array is done.
    I have already got the correct syntax for auto emailing. i just need help with reading from the directory & referring to the text file - before i email...

    realli appreciate it.
    Gilly

  5. #5

  6. #6
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    create a global UDT:

    VB Code:
    1. Type CUST
    2.     strCSV as String
    3.     strCUSTNAME as String
    4.     strCUSTEMAIL as String
    5. End Type
    6. Public Dim arrCUST() as CUST
    7.  
    8. Dim nFile as Integer
    9. Dim str as String
    10. Dim temparr() as String
    11. Dim i as Integer
    12.  
    13. nFile = FreeFile
    14. Open app.Path & "\Customers.txt" for Input as #nfile
    15.  
    16. While not EOF(nfile)
    17.     Line Input #nfile, str
    18.     temparr = Split(str, ",")
    19.     redim preserve arrCUST(i+1)
    20.     arrCUST(ubound(arrCUST)-1).strCSV = temparr(0)
    21.     arrCUST(ubound(arrCUST)-1).strCUSTNAME = temparr(1)
    22.     arrCUST(ubound(arrCUST)-1).strCUSTEMAIL = temparr(2)
    23. Loop
    24.  
    25. Close #nfile

    ... didn't test it, but it should be close.

    edit: I'll let someone else handle the Dir function. I have to get some of my own work done
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    ta guys,

    martinliss.. i need vb code which will look up c:\customer & select particular csv files from that directory based on their name, then email to relevant people.

    eg.
    If c:\customer contains 123.csv then i need to grab these files and email only 123.csv files to customer 1.

    If 456.csv exists then i need to grab these files & email to customer 2.


    My text file will act as a look up - which customer to send which files to...

    thanks for your help,
    Gilly

  8. #8
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    VB Code:
    1. strSearchName = "c:\customer\*.csv"
    2. f = Dir(strSearchName, vbReadOnly)
    3.     While f <> ""
    4.             MsgBox "Processing file: " & f
    5. 'further code
    6.  
    7.             f = Dir()
    8.     Wend

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    ober5861 , i get a syntax error on your line - red text

    Public Dim arrCUST() as CUST

    any idea whats wrong?
    Gilly

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    how do i create a global UDT?
    Gilly

  11. #11
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    VB Code:
    1. Type CUST
    2.     strCSV as String
    3.     strCUSTNAME as String
    4.     strCUSTEMAIL as String
    5. End Type

    That is the UDT (User Defined Type).

    Remove Dim from

    VB Code:
    1. Public Dim arrCUST() as CUST
    And it should work.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    sorry for the hassle,
    now i get a compile error "invalid outside procedure...points to this line
    VB Code:
    1. nFile = FreeFile
    Gilly

  13. #13
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    did you Dim nfile?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  14. #14
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    freefile is just a call to find the first open filenumber.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    i'm lost!! very weak at vb.
    Yes ive dim nfile.

    im not sure where to slot jordanchris' code into yours. heres what ive done..

    VB Code:
    1. Type CUST
    2.     strCSV As String
    3.     strCUSTNAME As String
    4.     strCUSTEMAIL As String
    5. End Type
    6. Public arrCUST() As CUST
    7.  
    8. Dim nfile As Integer
    9. Dim str As String
    10. Dim temparr() As String
    11. Dim i As Integer
    12. Dim strsearchname As String
    13. Dim nfile As Integer
    14.  
    15. strsearchname = "c:\customer\*.csv"
    16. f = Dir(strsearchname, vbReadOnly)
    17.     While f <> ""
    18.             MsgBox "Processing file: " & f
    19. 'further code
    20.  
    21.             f = Dir()
    22.     Wend
    23.  
    24. nfile = FreeFile
    25. Open App.Path & "\Customers.txt" For Input As #nfile
    26.  
    27. While Not EOF(nfile)
    28.     Line Input #nfile, str
    29.     temparr = Split(str, ",")
    30.     ReDim Preserve arrCUST(i + 1)
    31.     arrCUST(UBound(arrCUST) - 1).strCSV = temparr(0)
    32.     arrCUST(UBound(arrCUST) - 1).strCUSTNAME = temparr(1)
    33.     arrCUST(UBound(arrCUST) - 1).strCUSTEMAIL = temparr(2)
    34. Loop
    35.  
    36. Close #nfile
    Gilly

  16. #16
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    One thing - you can't have multiple files with the same name in the same directory, i.e.
    I've created my text file, the location of the csv files will always be c:\customer
    That is, you can only have one 123.csv file in that directory.

  17. #17

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    salvelinus, my mistake - i have a file called 123T.csv and 123D.csv - i need to email both of these files. ...both need to be sent to same customer.
    Gilly

  19. #19
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Originally posted by gilly
    i'm lost!! very weak at vb.
    Yes ive dim nfile.

    im not sure where to slot jordanchris' code into yours. heres what ive done..
    I'm not sure about the freefile thing... are you running VB6, Service Pack 5?

    JordanChris' code goes after mine... they are 2 seperate functions.

    And you will want to put my code in a sub by itself. And declare the UDT at the top of the module.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  20. #20
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Originally posted by gilly
    salvelinus, my mistake - i have a file called 123T.csv and 123D.csv - i need to email both of these files. ...both need to be sent to same customer.
    Then you may need to use something like Left() or Instr() to get only the matching part of the filename, i.e. "123", or use a wildcard. This might be simpler for you if you used a db instead of .csv files, but maybe that's not an option here.

  21. #21
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    It'd be easiest to use a wildcard with Dir:

    VB Code:
    1. Dir("C:\Customer\*123*.csv")
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    ober5861 will Private sub form_load() work at the top of your code? ive tried it, but it keeps telling me that ive got a compile error "invalid outside proc".
    It will not allow me to write private sub... after your dim xxx as string, or before the dim part....
    Gilly

  23. #23
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    VB Code:
    1. Private Sub Form_Load()
    2. Dim nFile as Integer
    3. Dim str as String
    4. Dim temparr() as String
    5. Dim i as Integer
    6.  
    7. nFile = FreeFile
    8. Open app.Path & "\Customers.txt" for Input as #nfile
    9.  
    10. While not EOF(nfile)
    11.     Line Input #nfile, str
    12.     temparr = Split(str, ",")
    13.     redim preserve arrCUST(i+1)
    14.     arrCUST(ubound(arrCUST)-1).strCSV = temparr(0)
    15.     arrCUST(ubound(arrCUST)-1).strCUSTNAME = temparr(1)
    16.     arrCUST(ubound(arrCUST)-1).strCUSTEMAIL = temparr(2)
    17. Loop
    18.  
    19. Close #nfile
    20.  
    21. End Sub

    The other stuff should (UDT definition and declaration of the UDT array) should be above all of that. Also, put "Option Explicit" as the very first line of your form module.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    im really sorry, i tried this, see below , & i get an error"cannot define public user type within a private module".

    ive got vb 6.0 sp 5.

    VB Code:
    1. Option Explicit
    2.  Type CUST
    3.     strCSV As String
    4.     strCUSTNAME As String
    5.     strCUSTEMAIL As String
    6. End Type
    7. Public arrCUST() As CUST
    8.  
    9. Private Sub Form_Load()
    10. Dim nFile As Integer
    11. Dim str As String
    12. Dim temparr() As String
    13. Dim i As Integer
    14.  
    15. nFile = FreeFile
    16. Open App.Path & "\codes.txt" For Input As #nFile
    17.  
    18. While Not EOF(nFile)
    19.     Line Input #nFile, str
    20.     temparr = Split(str, ",")
    21.     ReDim Preserve arrCUST(i + 1)
    22.     arrCUST(UBound(arrCUST) - 1).strCSV = temparr(0)
    23.     arrCUST(UBound(arrCUST) - 1).strCUSTNAME = temparr(1)
    24.     arrCUST(UBound(arrCUST) - 1).strCUSTEMAIL = temparr(2)
    25. Loop
    26.  
    27. Close #nFile
    28.  
    29. End Sub
    Gilly

  25. #25
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Then add a new module to your project and define it in there.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  26. #26
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    In a form:

    VB Code:
    1. Option Explicit
    2.  [b]Private [/b]Type CUST
    3.     strCSV As String
    4.     strCUSTNAME As String
    5.     strCUSTEMAIL As String
    6. End Type
    7. [b]Private [/b]arrCUST() As CUST
    8.  
    9. Private Sub Form_Load()
    10. Dim nFile As Integer
    11. Dim str As String
    12. Dim temparr() As String
    13. Dim i As Integer
    14.  
    15. nFile = FreeFile
    16. Open App.Path & "\codes.txt" For Input As #nFile
    17.  
    18. While Not EOF(nFile)
    19.     Line Input #nFile, str
    20.     temparr = Split(str, ",")
    21.     ReDim Preserve arrCUST(i + 1)
    22.     arrCUST(UBound(arrCUST) - 1).strCSV = temparr(0)
    23.     arrCUST(UBound(arrCUST) - 1).strCUSTNAME = temparr(1)
    24.     arrCUST(UBound(arrCUST) - 1).strCUSTEMAIL = temparr(2)
    25. [b]Wend [/b]'Loop
    26.  
    27. Close #nFile
    28.  
    29. End Sub

  27. #27
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    oops on the while loop. I meant to put a "Do" in front of the while. I always forget that for some reason.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    brillant thanks so much for all your help.
    I'll test it now..

    If possible id be grateful if someone could comment the code - just so i can learn from it.

    many thanks
    Gilly

  29. #29

  30. #30
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by gilly
    ...If possible id be grateful if someone could comment the code - just so i can learn from it.
    VB Code:
    1. Option Explicit
    2. ' Create a user-defined data type containing 3 elements
    3. Private Type CUST
    4.     strCSV As String
    5.     strCUSTNAME As String
    6.     strCUSTEMAIL As String
    7. End Type
    8. ' Dim an array of the CUST datatype
    9. Private arrCUST() As CUST
    10.  
    11. Private Sub Form_Load()
    12. Dim nFile As Integer
    13. Dim str As String
    14. Dim temparr() As String
    15. Dim i As Integer
    16.  
    17. ' Get a file number to be used for file I/O
    18. nFile = FreeFile
    19. ' Open a file that is assumed to be in the same folder as the exe
    20. Open App.Path & "\codes.txt" For Input As #nFile
    21.  
    22. ' Loop through all the lines in the file until end of file. I changed this
    23. ' to a Do loop because While/Wend is old fashioned.
    24. Do Until EOF(nFile)
    25.     ' Read a line into the str variable
    26.     Line Input #nFile, str
    27.     ' Split the line into pieces separated by spaces
    28.     temparr = Split(str, ",")
    29.     ' Increase the size of arrCUST as needed. It starts off at zero
    30.     ' (which will hold 1 record) because i is initially = 0
    31.     ' NOTE: I corrected the code from this point down
    32.     ReDim Preserve arrCUST(i)
    33.     ' Move the pieces of the line into arrCUST. UBound(arrCUST) calculates
    34.     ' to the highest available record
    35.     arrCUST(UBound(arrCUST)).strCSV = temparr(0)
    36.     arrCUST(UBound(arrCUST)).strCUSTNAME = temparr(1)
    37.     arrCUST(UBound(arrCUST)).strCUSTEMAIL = temparr(2)
    38.     ' Add 1 so that the ReDim can increase the array.
    39.     i = i + 1
    40. Loop
    41.  
    42. ' Close the file you opened
    43. Close #nFile
    44.  
    45. End Sub

  31. #31
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    OK, here's a small demo on how to use classes in this sort of situation.
    Alas, my code isn't as commented as well as Martins, I don't deserve god status

    My code also has a SAVE function in the class Customers, and an ADD function. This allows you to add more customers at run time and then save them back to the file, thought this may be handy for you.

    Some people may say that using class modules for this is overkill. But I am not a fan of large arrays of UDTs as if later down the line you have to change something then you can be pretty stuck and a rewrite is required. Class modules are slightly slower, but you won't see the difference, and they take up a little bit more resources. BUT on a plus they can have much more functionality as you can add code inside the class itself, like the Load and Save functions. This means that the code in your forms is kept to a minimum and is easier to read.
    This is what classes were designed for, as well as other things, and really do help your application.
    Watch, everyone will disagree with me now

    Hope this helps.

    Woka
    Attached Files Attached Files

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651

    script out of range??

    i get script out of range when i run this code...what is wrong?

    points to line ...
    arrCUST(UBound(arrCUST)).strCUSTEMAIL = temparr(2)

    Heres the code that im using...

    VB Code:
    1. Option Explicit
    2. ' Create a user-defined data type containing 3 elements
    3. Private Type CUST
    4.     strCSV As String
    5.     strCUSTNAME As String
    6.     strCUSTEMAIL As String
    7. End Type
    8. ' Dim an array of the CUST datatype
    9. Private arrCUST() As CUST
    10.  
    11. Private Sub Form_Load()
    12. Dim nFile As Integer
    13. Dim str As String
    14. Dim temparr() As String
    15. Dim i As Integer
    16. Dim strsearchname As String
    17. Dim F As String
    18.  
    19. ' Get a file number to be used for file I/O
    20. nFile = FreeFile
    21. ' Open a file that is assumed to be in the same folder as the exe
    22. Open App.Path & "\accountcodes.txt" For Input As #nFile
    23.  
    24. ' Loop through all the lines in the file until end of file.
    25. Do Until EOF(nFile)
    26.     ' Read a line into the str variable
    27.     Line Input #nFile, str
    28.     ' Split the line into pieces separated by spaces
    29.     temparr = Split(str, ",")
    30.     ' Increase the size of arrCUST as needed. It starts off at zero
    31.     ' (which will hold 1 record) because i is initially = 0
    32.    
    33.     ReDim Preserve arrCUST(i)
    34.     ' Move the pieces of the line into arrCUST. UBound(arrCUST) calculates
    35.     ' to the highest available record
    36.     arrCUST(UBound(arrCUST)).strCSV = temparr(0)
    37.     arrCUST(UBound(arrCUST)).strCUSTNAME = temparr(1)
    38.     arrCUST(UBound(arrCUST)).strCUSTEMAIL = temparr(2)
    39.     ' Add 1 so that the ReDim can increase the array.
    40.     i = i + 1
    41. Loop
    42.  
    43. ' Close the file you opened
    44. Close #nFile
    45.  
    46.  
    47. strsearchname = "c:\output\*.csv"
    48. F = Dir(strsearchname, vbReadOnly)
    49.     While F <> ""
    50.             MsgBox "Processing file: " & F
    51. 'further code
    52.  
    53.             F = Dir()
    54.     Wend
    55.  
    56. End Sub
    Gilly

  33. #33
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    The code assumes that the lines that you are reading have at least three parts, each separated by a comma. That assumption was made because it looked like you were trying to get three pieces of information from each line.

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Location
    N.Ireland
    Posts
    651
    WOW !! It found the files!

    I just need to figure out how to email each file depending on the customer. I get an error using the code below ..." failure on opening attachment" runtime error 32012" and it stops at .send. dont think it like *.csv

    What other method can i use?


    VB Code:
    1. MAPISession1.SignOn
    2.  
    3.            
    4.     With MAPIMessages1
    5.         .SessionID = MAPISession1.SessionID
    6.         .Compose
    7.         .RecipAddress = "myemail@com"
    8.         .ResolveName
    9.        
    10.  
    11.        
    12.         .AddressResolveUI = True
    13.        
    14.         .MsgSubject = "customer files
    15.         .MsgNoteText = "Please find attached files
    16.  
    17. ' Now add an attachment
    18.         .AttachmentIndex = 0
    19.         .AttachmentPosition = 0
    20.         .AttachmentPathName = sFolderPath & "\*.csv"
    21.        
    22.  
    23.         .Send
    24.     End With
    25. MAPISession1.SignOff
    Last edited by gilly; Nov 21st, 2003 at 12:56 PM.
    Gilly

  35. #35
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Errrrr...
    VB Code:
    1. .AttachmentPathName = sFolderPath & "\*.csv"
    Should that be just a path without the .CSV bit on the end?
    Isn't there another property to add attachments?

    Woka

  36. #36
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    This line is good:
    .AttachmentPathName = sFolderPath & "\*.csv"

    BUT the * is no good.

    Make sure sFolderPath does not end in a "\", as you are adding another one.
    Make sure the * is replaced with the actual name of the file you are adding. Using DIR is a good approach to get the actual filename and to make sure the file actually exists.


    And if you want to attach multiple files, you will need to do more work. The .AttachmentPosition needs to be changed for each attachment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width