Link to home
Start Free TrialLog in
Avatar of mytfein
mytfein

asked on

Access 2003: Importing Excel file with a social security# column with a leading 0 that's dropping off

Hi EE,

I am importing a file with a social security number column.

When the column contains a leading 0, it gets left out in the table.

My code:
          first opens the excel file
          then automatically auto expand the file
          then loop thru the column names to dynamically create a table

Is there Excel vba to set the social column to social security mask
         or any other ideas not to lose the leading 0?

tx for your help, sandra
Public Sub e005_OpenExcelToCreateTEMPtable(strExcelPath As String)

Dim lngColCount    As Long
Dim lngRowCount    As Long
Dim lngMaxRowCount As Long
Dim lngPos         As Long

Dim strSql         As String
Dim strColName     As String

Call e090_LaunchExcel
'Set objExcel = CreateObject("Excel.Application")
  
Set objExcelActiveWkbs = objExcel.Workbooks

Set objExcelActiveWkb = objExcelActiveWkbs.Open(FileName:=strExcelPath)
Set objExcelActiveWS = objExcel.ActiveSheet

objExcel.Visible = True

lngRowCount = 1
lngColCount = 1
lngMaxRowCount = 1
lngRowCount = 1

With objExcelActiveWS
    
     With .Cells
          .Select
          .EntireColumn.AutoFit
     End With
     

     Debug.Print objExcelActiveWS.UsedRange.Rows.Count
     
'     If .Cells(1, 1).Value Like "*Year*" Then
'        pg_strAcademicYear = .Cells(2, 1).Value
'     Else
'        pg_strAcademicYear = Format(Date, "YYYY")
'     End If
     
     
     If z965_TableExists(pg_strTableName_Temp) Then
        CurrentDb.Execute "DROP TABLE " & pg_strTableName_Temp
        Application.RefreshDatabaseWindow   '<-- This has to happen here
     End If
     
     Call q715_DeleteFrom_Table(pg_strTableName_Columns_Excel)
     
     ' CurrentDb.Execute "DROP TABLE " & pg_strTableName_Students
     
     strSql = "CREATE TABLE " & pg_strTableName_Temp & "("
     

     For lngRowCount = 1 To lngMaxRowCount
         For lngColCount = 1 To .UsedRange.Columns.Count
             strColName = .Cells(lngRowCount, lngColCount).Value
             
             If Len(strColName) = 0 Then
                strColName = "F" & lngColCount
                .Cells(lngRowCount, lngColCount).Value = strColName
             Else
             
                strColName = "[" & strColName & "]"
                
                Call q117_InsertInto_Table_Columns(pg_strTableName_Columns_Excel, _
                                                   strColName)

             End If
             
             
             
             If lngColCount > 1 Then
               strSql = strSql & " ,"
            End If
            
            
            
           If strColName = "Date" Then
               strColName = strColName & lngColCount
               .Cells(lngRowCount, lngColCount).Value = strColName
           ElseIf strColName = "Time" Then
               strColName = strColName & lngColCount
               .Cells(lngRowCount, lngColCount).Value = strColName
           End If
            
           strSql = strSql & strColName
            
            
            If strColName Like "*Date*" Then
               strSql = strSql & " DATE"
            ElseIf strColName Like "*Count*" Or _
                   strColName Like "*_Numeric*" Then
                  strSql = strSql & " NUMBER"
            Else
                strSql = strSql & " TEXT"
            End If
             
         Next
     Next
     
     ' last field is AutoNumber
     strSql = strSql & " ,StudentAutoNum AutoIncrement"
     
     ' last field is CheckBox
     strSql = strSql & " ,StudentCheckBox YesNo"
          
     strSql = strSql & ")"
     
     Debug.Print strSql
  
     CurrentDb.Execute strSql
  
     Call t075_ConvertFieldToCheckBox(pg_strTableName_Temp, _
                                      "StudentCheckBox")
                                   
End With
     
objExcelActiveWkb.Save

Call e110_CloseExcel(True)
    
End Sub

Open in new window

Avatar of pritamdutt
pritamdutt
Flag of India image

There are two ways in which you could treat this information
1. You could treat Social Security Number as Text instead of Number, considering your application is not responsible for generating Social Security Numbers
2. You could prefix all Social Security Numbers with Zero to max the standard length of the SSN
with a query in access like

SELECT RIGHT ('000000' + 'SSN',5);

Hope this works for you..
Avatar of mytfein
mytfein

ASKER

HI Pritamdutt,

tx for writing,

i really was looking to do any logic to the excel file after its opened....
     in access, i tried this excel vba, but it does not work - the leading 0 is still missing

     i will try to create a test mdb and test excel file for testing....   tx, s

      If blnSetNumber_SSN = True Then
        With .Columns(strColumn)
             .Select
             .NumberFormat = "000000000"
        End With
     End If
Avatar of mytfein

ASKER

Hi Pritamdutt,

while create a test mdb and test excel file,
      i think i am realizing the following
                 if i format a cell with a special format,  it shows in the cell however the data
                          showing in the formula bar is the old data without the leading zero
                                   and therefore, when it imports into Access it imports without the leading zero

uploaded mdb and excel below

it may be that i would need to do your other suggestion select right, but i do not understand why
concatentating zeros to the ssn, and then taking the rightmost 5 characters....

could you pls advise, on the select right technique....tx, s
EE-ARG-03-MaR-MatchM4-frontend.mdb
EE-Match-Results2007.xls
ASKER CERTIFIED SOLUTION
Avatar of mytfein
mytfein

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
Avatar of mytfein

ASKER

found an idea via google and adapted it....
Avatar of mytfein

ASKER

Hi Everyone,

The above idea still dropped the leading zero, so i updated after importing per this idea:

http://www.accessmonster.com/Uwe/Forum.aspx/access-queries/53050/Convert-Number-to-Text-and-keep-leading-zeroes


UPDATE TBL_010_ThisYear_NRMP SET sSN = Format(SSN,"000000000");


tx, s