Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

replace text in CSV file through VBS to import into access

Posted on 2006-11-12
12
Medium Priority
?
582 Views
Last Modified: 2012-06-27
Hello

I have written a VBS file to Find " in my csv file. I need to get rid of all the " as I don't need them and it's hurting the quotes being used as a text qualifier when importing the file into access as a table (unparseable record) I think i'm missing something in the code because I keep getting an error.

First here is the code

Dim myXL
         Set myXL = CreateObject("excel.application")


         myXL.workbooks.Open "g:\Subgroup\Access Data\lbx\RAW DATA\oSPEC\tbl_atl_ospec.csv"


         myxl.displayalerts = false
         myXL.activeworkbook.Sheets("Sheet1").Select
         myXL.activeworkbook.Cells.Replace What:="""",
     Replacement:="", LookAt:=xlPart
     SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
         myxl.displayalerts = true    


         myXL.activeworkbook.Close False
         Set myXL = Nothing
The error is "expected Statment"
Code: 800a0400
Source: Microsoft VBScript Compiliation Error
It specifically references this line and "what:=""",

myXL.activeworkbook.Cells.Replace What:="""",

Can someone look over my code in its entirety and help me make sure that I'm doing this right

I am trying to remove all " in the file.

Thanks

Cory
0
Comment
Question by:cstraim
[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
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 44

Expert Comment

by:GRayL
ID: 17926235
Try using 3 double quotes instead of four.
0
 
LVL 58
ID: 17926237
Try,

        myXL.activeworkbook.Cells.Replace What:='""',

(single quotes on the outside).


The problem your having is that you need to supply the quote character within a string argument in code.

JimD.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17926825
cstraim:  confirm you are trying to get rid of single doublequotes in you csv file - or all of them?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 58
ID: 17926881
<<cstraim:  confirm you are trying to get rid of single doublequotes in you csv file - or all of them?>>

  I wasn't sure of that either.

  Is it all " (quote) or all "" (quote, quote) that is attempting to be removed.

JimD.
0
 
LVL 58

Expert Comment

by:harfang
ID: 17926932
Hello cstraim

This is not quite obvious from your post, but it seems you are missing the "line continuation characters". In VB an VBS, you cannot use a new line anywhere, as you could in most other programming languages. To indicate that the logical line continues on the next physical line, you need to end it with a space and a single underscore, for example like this:

         myXL.activeworkbook.Cells.Replace _
             What:="""", _
             Replacement:=""

Note that the remaining parameters are not needed in your case: you can use their default values.

Second problem: the activeworkbook does not have a Cells collection, only sheets do. You would neet do specify on which sheet you want to work:

        myXL.activesheet.Cells.Replace ' etc...

Third, VB, unlike VBA, does not work with object libraries, and hence cannot use named arguments (e.g. What:=). You need to specify the needed arguments in the correct order, without names.

Finally, you close without saving, so that your changes are lost.

As a side note, it woul probably be better to replace double quotes by double single quotes, because they probably have a meaning: " --> ''

So:

    Dim myXL
    Set myXL = CreateObject("excel.application")
    myXL.workbooks.Open "g:\Subgroup\Access Data\lbx\RAW DATA\oSPEC\tbl_atl_ospec.csv"
    myXL.activeworkbook.Sheets("Sheet1").Cells.Replace """", "''"
    myXL.activeworkbook.Close true
    myXL.Quit

[use ...Replace """", ""    to delete all double quotes, as in your sample]

Good luck!

(°v°)
0
 
LVL 26

Accepted Solution

by:
jerryb30 earned 1000 total points
ID: 17927475
Not a VB solution, but replacing simple strings in a text document is probably more efficient with a utility,.
I prefer gsar.exe. (Global Search and Replace)
http://home.online.no/~tjaberg/
gsar -s:x22 -r yourfile.csv yourfile.txt creates a text document without "", while preserving original files.
0
 
LVL 2

Expert Comment

by:DoubleDee
ID: 17928555
Interesting approach. Though, I would do it a little differently. Usually CSV files are simple text files.
If you have to use VBS you can do something like this, e.g.

Dim objFSO
Dim fsoFile
Dim strTemp

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fsoFile = objFSO.OpenTextFile("your textfile", 1) ' the "1" is the flag for reading
strTemp = fsoFile.ReadAll
fsoFile.Close
Set fsoFile = objFSO.OpenTextFile("your textfile", 2)  ' the "2" is the flag for writing

fsoFile.Write Replace(strTemp, """", "") ' This does the replacement of the quotes and puts it back to the file.
fsoFile.close

If you reference Windows Script Host to your Access application, you can do this in one go, by putting the code right before your import routine. If you're sure, that the delimiter is always set correctly, you can import the textfile via FSO line-by-line directly using the Split-function to put every line into an array, and then put it to the database.

DD
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17931471
cstraim:  Want to clarify a few things?
0
 

Author Comment

by:cstraim
ID: 17934783
WOW..thanks all..Im sorry i didnt post sooner.  I was in meetings and working on a high profile project that took all of my time.  I will be looking at this tomorrow.  Thanks for all of your posts.  I really REALLY appreciate it :)
0
 

Author Comment

by:cstraim
ID: 17948882
Thanks again to everyone.  my original question was answered by the simple search and replace tool that can be called using a batch file.  unfortunately many of you were totally right in the fact that I was trying to fix the unparseable record error in Access.    I have no idea how to do that programmatically..Ill have to open a new question.

I'm sorry i took so long to respond. I had to put this piece of my project on hold, because of some immediate fires that needed to be put out

Thanks again!!!
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 17949053
Hmm.  As I stated, I did not provide a VB solution.  The others did (although feedback on success or failure of method was not forthcoming.
Can you give a logic for your accept?
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 17949072
I did not refresh, so I did not see your post just above.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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