Trim a Field once a csv file has been uploaded to a table

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
769
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I think (hope) this is a simple query.

I have created a Macro to pull data from a stored ".csv" file into a table already saved in Access, by re writing over the current data.
The issue is the file once its in could do with a column (or the whole) table trimming to make it easier for me to create queries as I currently have to have another column that trims a field.

Is there anyway I can add a line in to trim [Field3] before the "StatusMsg"?

Code:
Sub ImportCSV(TableName, SourceFilepath, ImportMethod As String)

    Dim StatusMsg As Variant
    
    StatusMsg = SysCmd(acSysCmdSetStatus, "Clearing " & TableName)
    DoCmd.RunSQL "DELETE * FROM " & TableName & ";"
    
    StatusMsg = SysCmd(acSysCmdSetStatus, "Updating " & TableName)
    DoCmd.TransferText acImportDelim, ImportMethod, TableName, SourceFilepath, True
        
    StatusMsg = SysCmd(acSysCmdClearStatus)
End Sub

Sorry if this is an easy one, but my brain is going to sleep after reading so much on it.

thanks in advance
Gavin
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Sorry @ranman256,

Where would I put this as I get a compile error, expected End of Statement

Code:
    Dim StatusMsg As Variant
    
    StatusMsg = SysCmd(acSysCmdSetStatus, "Clearing " & TableName)
    DoCmd.RunSQL "DELETE * FROM " & TableName & ";"
    
    StatusMsg = SysCmd(acSysCmdSetStatus, "Updating " & TableName)
    DoCmd.TransferText acImportDelim, ImportMethod, TableName, SourceFilepath, True
    
    update EMPInfo_Cell set [EMP_ID] = Trim([EMP_ID])
    
    StatusMsg = SysCmd(acSysCmdClearStatus)
End Sub

thanks
 
Upvote 0
I thought TransferText into Access would automatically trim spaces (at least trailing spaces)? If your spaces are wrapped within rtf or html characters, Trim won't work.
I believe the suggestion applies to running the update in Access, not in your transfer code. Otherwise, you'd need a reference to the Access library in your Excel project and attempt to do it from there. Much easier to open Access and run that query.
 
Upvote 0
Solution
Appreciate the recognition of my post as a solution but I think ranman deserves it unless your issue had something to do with rtf formatting.
 
Upvote 0
Apologies @Micron & @ranman256,

I did create a query to adjust the data as needed and as SQL it shows

Code:
SELECT TABLENAME.THISONE, Trim([THISONE]) AS [trimdetails] FROM TABLENAME;"

Can I add this into the current code above or would I need to add a column called [trimdetails] to the exisitng importing table?

thanks
 
Upvote 0
No, follow ranmans code in post 2 and run that query where you have your update line.
However as that update line is sql and not vba, why not run it as you do the delete sql?
 
Upvote 0
I wanted to do it as sql as I believe I can do multiple tables in 1 go instead of creating individual queries?
this would need to be done across 8 tables
 
Upvote 0
" AS [trimdetails] " will create a column. However, that is just a select query - it won't actually change your data. You could use query design and change to update query to generate the update sql you need. Switch to sql view in the query then copy/paste and do as you did with the Delete in your posted code. Add that after the transfer line. Consider CurrentDb.Execute "DELETE * FROM " & TableName & ";", dbFailOnError to avoid the action warnings. You don't need the ; at the end of vba sql but it doesn't hurt to have it.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top