Solved

Handling Zip+4 in VBA import

Posted on 2013-01-21
6
427 Views
Last Modified: 2013-01-27
I'm trying to import data into my DB from an Excel SS or a CSV file. Here's the code I'm using:

FilePath = "C:\Users\residents.csv"
'DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel7, "TblNm_SS", FilePath, True
DoCmd.TransferText acLinkDelim, , "TblNm_SS", FilePath, True

Set InRS = DB.OpenRecordset("TblNm_SS")
Set OutRS = DB.OpenRecordset("PermTbl")

If InRS.EOF = False Then
    InRS.MoveFirst
    
    Do Until InRS.EOF = True
        
        I = 0
        With OutRS                                 'the input is an empty string write it
            .AddNew
            For I = 0 To InRS.Fields.Count - 1
                .Fields(I).Value = CStr(Nz(InRS.Fields(I).Value, ""))
            Next I
            .Update
        End With
    
        InRS.MoveNext
    Loop
End If

Open in new window


The problem is that I have a zip column that a few of the records have a few zip+4 like "44081-1234" which when viewing the linked table comes up as #Num and the properties say it is a long integer in the linked table.

The data set is small enough that I can just copy and paste to the staging table. But does anyone have any suggestions to get around this if I want to automate the import process?
0
Comment
Question by:Jim P.
  • 3
6 Comments
 
LVL 11

Expert Comment

by:ScriptAddict
ID: 38802515
Filter the table first to remove the -.  so it's only 440811234.  Or built it into the import that if the length of the zip field is a +4 then you pull that field apart before importing it.

Alternatively you could adjust the table to not have an issue with the - by changing it to a text field.  

-SA
0
 
LVL 38

Author Comment

by:Jim P.
ID: 38802535
The destination table in Access is already setup as a text field. The issue is that the VBA wants to handle the attached file as a long integer and or just an integer so I'm getting a Numeric Overflow error.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 38802536
Few options:

.Fields(I).Value = CStr(Nz(InRS.Fields(I).Value, ""))

Determine which field number it is and when you get to it, simply use REPLACE.  

Example, if the Zip Code field is the 6th field, then:

If i = 6 Then
   .Fields(I).Value = Replace(CStr(Nz(InRS.Fields(I).Value, "")), "-", "")
Else
   .Fields(I).Value = CStr(Nz(InRS.Fields(I).Value, ""))
End If

Or open the source data and do a replace on that field in excel.
0
 
LVL 38

Author Comment

by:Jim P.
ID: 38803600
leew,

Still doesn't work. My error is "Numeric field overflow." because it is still trying to handle it as in integer.
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 0 total points
ID: 38803611
Came up with a way around it. Handle it as a variant.

FileNum = FreeFile()

Open FilePath For Input Access Read Shared As #FileNum

Set RS = DB.OpenRecordset(TblName)    '<-- Change to your tablename

Line Input #FileNum, InputString     '<--- To throw away the header line
Do Until EOF(FileNum) = True
    Line Input #FileNum, InputString
    
    If Len(Trim(InputString)) = 0 And EOF(FileNum) = False Then
        Line Input #FileNum, InputString
    ElseIf EOF(FileNum) = True Then
        Exit Do
    End If
    
    TxtArray = Split(InputString, ",", -1, vbBinaryCompare)
    
    I = 0
    With RS                                 'the input is an empty string write it
        .AddNew
        For I = 0 To UBound(TxtArray) - 1
            .Fields(I).Value = Trim(TxtArray(I))
        Next I
        .Update
    End With
    I = 0
Loop

RS.Close
Close #FileNum

Open in new window


I do have to handle the a small problem when I save it from Excel to the CSV format. The original data is saved as Full Name "Smith, John D." and when exported from Excel that column is surrounded by quote marks. But that is easy enough to fix. I was going to have to break up the name anyway for normalization export to the the other system.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query design issue 2 24
Access 2010 Query Syntax 5 26
Convert Access formula to SQL 5 20
Find missing numbers in Access Table PrimaryKey 9 17
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

864 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

16 Experts available now in Live!

Get 1:1 Help Now