Solved

Pulling data from a CSV file

Posted on 2007-03-20
6
200 Views
Last Modified: 2013-11-25
What am I doing wrong in code? I want to pull data from a CSV file. I get an error that says 'Invalid procedure call or argument'.
==================================================================================
Public Function GetDataFromFile(ByVal OrderNumber As String)
   Dim f As Integer
    Dim strAll As String
    Dim strOrder() As String
    Dim strField() As String
    Dim strOurOrder As String
    Dim strTemp As String
       
    'throw the whole file into a string
    f = FreeFile
    Open "C:\Customers\Superior Advertising\UPSEXP.CSV" For Binary As #f
   
    strAll = Space$(LOF(f))
    Get #f, , strAll
    Close #f
    'trim off everything before the first instance of the order
    strAll = Mid$(strAll, InStrRev(strAll, OrderNumber))
   
    GetDataFromFile = Left$(strAll, InStr(strAll, vbCrLf) - 1)
End Function
==================================================================================
If I make a copy of the CSV file and open it in Notepad and change the file extension to .txt - then it works, it pulls data like it should.

All data in each line item is separated by a comma and quotes surround each field.
Example of some of the data:

"B143437","PROMARK SOLUTIONS  LLC",".","P.O. BOX 6878","","","JEFFERSON CITY","MO","65102","(573)619-4659"
"S143437","LAKEVIEW RESORT","ATTN: JAKE DRAKE","328 LAKEVIEW RESORT BLVD.","","","SUNRISE BEACH","MO","65079",""
"B143456","KAMIST PROMOTIONS  INC.",".","P O BOX 740017","","","BOYNTON BEACH","FL","334740017","(561)740-4945"
"S143456","DR. BRADLEY D. KAUFMAN","","925 EAST HENRIETTA ROAD","","","ROCHESTER","NY","14623",""

I copied four lines. Each line begins with 'S######' or 'B######'.

Thanks as always!







0
Comment
Question by:stltodaycom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 18757073
on which line? may be replace this line

GetDataFromFile = Left$(strAll, InStr(strAll, vbCrLf) - 1)

with

GetDataFromFile = """" & Left$(strAll, InStr(strAll, vbCrLf) - 1)
0
 

Author Comment

by:stltodaycom
ID: 18757250
This is the line of code that error's out:
GetDataFromFile = Left$(strAll, InStr(strAll, vbCrLf) - 1)

EDDYHT: I tried your suggestion but I still receive the same error: "invalid procedure call or argument".
0
 
LVL 15

Assisted Solution

by:cquinn
cquinn earned 25 total points
ID: 18757355
A better method of accessing the data in a CSV file is to open it as a recordset and use ADO/DAO to query the data
0
Independent Software Vendors: 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!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 18757360
what is this

InStr(strAll, vbCrLf)

gives you?

may be used vbCr or VbLf
0
 
LVL 45

Expert Comment

by:aikimark
ID: 18758790
There is also this possible solution.

'place this in the General Declarations of a module
Type Rec_struc
  OrderNum As String
  Name As String
  Contact As String
  Addr1 As String
  Addr2 As String
  Addr3 As String
  City As String
  State As String
  Zip As String
  Phone As String
End Type

Public Function GetDataFromFile(ByVal OrderNumber As String) As Rec_struc
    Dim f As Integer
    f = FreeFile

    Open "C:\Customers\Superior Advertising\UPSEXP.CSV" For Input As #f

   Do Until Eof(#f)
    With GetDataFromFile
       Input #f,  .OrderNum , .Name , .Contact , .Addr1 , .Addr2 , .Addr3 , .City , .State , .Zip , .Phone

      If .OrderNum = OrderNumber Then
        Exit Do
      End If

    End With

   Loop

   Close #f

End Function
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 100 total points
ID: 18827907
I agree with aikimark...  CSV is a text file so why not open file as text.

Only trap with code aikimark shows is.. what if not all fields are present for the data line ?  Some applications drop the last field.  


I prefer to read the entire line and then split it up myself. The disadvantage is that the quotes around text are included.

This returns the entire line to your application as your currect code.

'------------------------------------------------------------------
' code portions with assistance of ExpertsExchange !

Public Function GetDataFromFile(ByVal OrderNumber As String) As String
    Dim ffn As Integer, sData() As String, strAll As String
    ffn = FreeFile
    Const FileUsed = "C:\Customers\Superior Advertising\UPSEXP.CSV"
    'Const FileUsed = "C:\work\EE\testcsv.csv"

    Open FileUsed For Input As #ffn

    Do Until EOF(ffn)
   
      Line Input #ffn, strAll

      sData = Split(strAll, ",")
     
      'need to use quotes around OrderNumber to get comparision to work
      If sData(0) = Chr(34) & OrderNumber & Chr(34) Then
        'have the correct one
        GetDataFromFile = strAll
        Exit Do
      End If
     
   Loop

   Close #ffn

End Function


'-----------------------------
tested using your data

Private Sub Command1_Click()
    MsgBox GetDataFromFile("B143456"), vbInformation
End Sub
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

740 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