Solved

Handling Zip+4 in VBA import

Posted on 2013-01-21
6
413 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
Comment Utility
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.
Comment Utility
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
Comment Utility
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.
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

771 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

14 Experts available now in Live!

Get 1:1 Help Now