Solved

Pulling data from a CSV file

Posted on 2007-03-20
6
197 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

770 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