Solved

Pulling data from a CSV file

Posted on 2007-03-20
6
196 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

912 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

22 Experts available now in Live!

Get 1:1 Help Now