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
Solved

Pulling data from a CSV file

Posted on 2007-03-20
6
198 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

808 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