Link to home
Start Free TrialLog in
Avatar of rinksno1
rinksno1

asked on

0 prefix gets truncated while reading value from CSV file

I have a problem in reading a csv file...

I have an employee database in access and an Employee CSV file which is weekly updated.
my application compares an employee id in the csv file with employee id field in the access database( datatype = text in access)...if the employee id is not found in the database then the employee details are inserted into the database....every thing is working fine but when i try compare the employee ID (in CSV file) with first character as zero eg.....02514,02545.... with that in the access database, the zero gets truncated and the employee id value compared is 2514,2545....i want to compare the whole
value i.e. 02514,02545....

what should i do..

rinks
Avatar of llcooljayce
llcooljayce
Flag of Canada image

20 points?
Avatar of Gruff82
Gruff82

Are your employee ID's set at 5 digits?

If they are you could check the length of id from the csv file and if it is less than 5 pad with zeros.

For example:

if len(csvID) < 5 then
  do while len(csvID) <> 5
ASKER CERTIFIED SOLUTION
Avatar of Gruff82
Gruff82

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I find the best way (if a little long winded) is to change the delimeter of the file to a pipe '|' (with a coded Replace statement, if you have to).  You can then load this into Excel as a text file and then use the Text To Columns function in Excel to split it.  When using the TTC function, you can specify the exact format of a column; you should set this to Text to prevent the leading zero being removed.

If you record this TTC function in a macro, you would only need to do it once...

(NB: You may be able to load a .CSV file as a text file and do the TTC routine, if changing the file format to pipe-delimeted is non-viable -- I only use pipe to avoid confusion.)

HTH
The only way I can think of that a leading zero would be lost is if the field it's being read into has been declared as numeric.  If that's the case, then there's no way I know of to avoid having that happen.  To retain leading zeros change the field type from numeric to text.
similar to  at Gruff82's solution is something like this
Dim EmployeeId As String
...
...

    EmployeeID=Right$("00000" & csvEmployee, 5)
...



Or

...
...
    EmployeeID=Format$(csvEmployee,"00000")

Or

Possibly store the employee number in quotes inside your CSV - "02514","02545"
How are you reading in the .csv file?

Personally, I'd either:
  Remove the leading zeros in the Access database (assuming they're not significant, ie there would never be an employee with ID 02514 and a different employee with ID 2514)
  Setup a calculated field in an Access query with a calculated field that removes the leading zeros and then base the match on that.
Avatar of rinksno1

ASKER

Humm...i need to concatinate but i am not comparing the length i am comparing the prefix 2's if it is there then concatinate 0
thankz

rinks