Solved

Pulling data from a CSV file

Posted on 2007-03-20
6
195 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now